0

我正在尝试使用 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()
4

1 回答 1

0

我认为最好使用适当的库来执行此操作。就我个人而言,我曾与 paramiko 合作过,这非常简单。

选择: http ://wiki.python.org/moin/SecureShell

于 2013-07-11T10:54:20.423 回答