使用 Paramiko 在命令内部传递 Python 变量
Pass Python variable inside command with Paramiko
使用此命令,我想保存 sda 驱动器的 smartctl
命令的输出。
现在,因为我使用 RAID,所以我有多个驱动器,而不仅仅是 sda。
以下命令工作正常:
stdin, stdout, stderr = ssh.exec_command('/bin/su root -c "smartctl -a /dev/sda > /tmp/smartctl_output1"', get_pty=True) # ORIGINAL
我想实现我传递包含 sda、sdb、sdc 等的本地 Python 变量 "DISK" 而不是仅静态值。
以下行产生错误:
stdin, stdout, stderr = ssh.exec_command('/bin/su root -c "smartctl -a /dev/" + DISK + " > /tmp/" + DISK', get_pty=True)
编辑: 也试过这个:
stdin, stdout, stderr = ssh.exec_command('/bin/su root -c "smartctl -a /dev/' + DISK + ' > /tmp/' + DISK, get_pty=True)
stdin.write(ROOTPASS)
stdin.write('\n')
DEBUG1=stdout.read()
print "DEBUG COMMAND= " + DEBUG1
在 /tmp/ 中产生以下错误和文件 + 未创建磁盘:
DEBUG COMMAND= bash: -c: line 0: unexpected EOF while looking for matching `"'
bash: -c: line 1: syntax error: unexpected end of file
你的问题与Paramiko无关
这只是 Python:
中字符串的简单串联
ssh.exec_command(
'/bin/su root -c "smartctl -a /dev/' + DISK + ' > /tmp/' + DISK + '"',
get_pty=True)
如需更好的选择,请参阅
使用此命令,我想保存 sda 驱动器的 smartctl
命令的输出。
现在,因为我使用 RAID,所以我有多个驱动器,而不仅仅是 sda。
以下命令工作正常:
stdin, stdout, stderr = ssh.exec_command('/bin/su root -c "smartctl -a /dev/sda > /tmp/smartctl_output1"', get_pty=True) # ORIGINAL
我想实现我传递包含 sda、sdb、sdc 等的本地 Python 变量 "DISK" 而不是仅静态值。
以下行产生错误:
stdin, stdout, stderr = ssh.exec_command('/bin/su root -c "smartctl -a /dev/" + DISK + " > /tmp/" + DISK', get_pty=True)
编辑: 也试过这个:
stdin, stdout, stderr = ssh.exec_command('/bin/su root -c "smartctl -a /dev/' + DISK + ' > /tmp/' + DISK, get_pty=True)
stdin.write(ROOTPASS)
stdin.write('\n')
DEBUG1=stdout.read()
print "DEBUG COMMAND= " + DEBUG1
在 /tmp/ 中产生以下错误和文件 + 未创建磁盘:
DEBUG COMMAND= bash: -c: line 0: unexpected EOF while looking for matching `"'
bash: -c: line 1: syntax error: unexpected end of file
你的问题与Paramiko无关
这只是 Python:
中字符串的简单串联ssh.exec_command(
'/bin/su root -c "smartctl -a /dev/' + DISK + ' > /tmp/' + DISK + '"',
get_pty=True)
如需更好的选择,请参阅