[英]Pass input/variables to command/script over SSH using Python Paramiko
[英]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
我想實現傳遞本地Python變量“ DISK”的功能,該變量包含sda,sdb,sdc等,而不是僅包含靜態值。
以下行產生錯誤:
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 / +未創建的DISK中產生以下錯誤和文件:
DEBUG COMMAND = bash:-c:第0行:尋找匹配的“”“時出現意外的EOF
bash:-c:第1行:語法錯誤:文件意外結束
您的問題與Paramiko無關。
這只是Python中字符串的瑣碎連接:
ssh.exec_command(
'/bin/su root -c "smartctl -a /dev/' + DISK + ' > /tmp/' + DISK + '"',
get_pty=True)
有關更好的選擇,請參見如何將字符串作為變量插入字符串?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.