簡體   English   中英

使用python通過SSH執行命令

[英]Executing a command over SSH with python

我正在嘗試使用python通過SSH執行所有多個命令。

每次,我都會收到“意外(”錯誤)。

請為此提供正確的語法。 謝謝。

os.popen('''ssh -o BatchMode=yes -o StrictHostKeyChecking=no '''+host+''' 'echo "<td>" $(uname -ri) "</td>"; free | grep "Mem:" | awk '\''{ print "<td>" $2/1024 " MB (" int($4*100/$2) "%) </td>" }'\''; free | grep "Swap:" | awk '\''{ print "<td>" int($3*100/$2) "%" }'\''; echo "</td><td>" $(cat /proc/cpuinfo | grep "processor" | wc -l) "@" $(cat /proc/cpuinfo | grep "MHz" | sort -u | awk '\''{ print $4 }'\'') "Mhz" $(cat /proc/cpuinfo | grep "cache size" | sort -u | awk '\''{ print "(" $4 " " $5 ")</td>" }'\'')'" ''').read()

截至目前,我正在使用:

 data1=os.popen('''ssh -o BatchMode=yes -o StrictHostKeyChecking=no '''+host+''' 'echo "<td>" $(uname -ri) "</td>";' ''').read().rstrip()
 data2=os.popen('''ssh -o BatchMode=yes -o StrictHostKeyChecking=no '''+host+''' free | grep "Mem:" | awk '{print "<td>" $2/1024 " MB("int($4*100/$2)"%)</td>"}' ''').read().rstrip()
 data3=os.popen('''ssh -o BatchMode=yes -o StrictHostKeyChecking=no '''+host+''' free | grep "Swap:" | awk '{ print "<td>" int($3*100/$2) "%" }' ''').read().rstrip()
 data4=os.popen('''ssh -o BatchMode=yes -o StrictHostKeyChecking=no '''+host+''' cat /proc/cpuinfo | grep "processor" | wc -l ''').read().rstrip()
 data5=os.popen('''ssh -o BatchMode=yes -o StrictHostKeyChecking=no '''+host+''' cat /proc/cpuinfo | grep "MHz" | sort -u | awk '{ print $4 }' ''').read().rstrip()
 data6=os.popen('''ssh -o BatchMode=yes -o StrictHostKeyChecking=no '''+host+''' cat /proc/cpuinfo | grep "cache size" | sort -u | awk '{ print "(" $4 " " $5 ")</td>" }' ''').read().rstrip()

我認為最好使用適當的庫來執行此操作。 我個人曾與paramiko合作,這非常簡單。

選擇: http : //wiki.python.org/moin/SecureShell

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM