[在Paramiko中将Python变量传递给命令内部

问题描述 投票:1回答:1

使用此命令,我想保存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)

edit:也尝试过此方法:

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 /中产生以下错误和文件,并且未创建DISK:

DEBUG COMMAND = bash:-c:第0行:寻找匹配的“”“时出现意外的EOFbash:-c:第1行:语法错误:文件意外结束

python python-2.7 ssh paramiko
1个回答
1
投票

您的问题与Paramiko无关。

这只是Python中字符串的琐碎串联:

stdin, stdout, stderr = ssh.exec_command('/bin/su root -c "smartctl -a /dev/' + DISK + ' > /tmp/' + DISK + '"', get_pty=True)

有关更好的选择,请参见How to insert string into a string as a variable?

© www.soinside.com 2019 - 2024. All rights reserved.