我正在尝试使用os.popen()通过SSH在Python中执行这4个命令。
问题出在awk命令上,尤其是它的引号。
下面是我要转换的内容:
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>" }'\'')'
下面是我尝试过的方法,但它在最后一个命令中出现了一些引号错误。问题似乎出在awk的引用上。另外,我希望将它们合并到一个SSH实例中。
os.popen("ssh 127.0.0.1 'echo \"<td>\" $(uname -ri) \"</td>\"'").read()
os.popen("ssh 127.0.0.1 free | grep \"Mem:\" | awk '{print \"<td>\" $2/1024 \" MB(\"int($4*100/$2)\"%)</td>\"}'").read()
os.popen("ssh 127.0.0.1 free | grep \"Swap:\" | awk '{ print \"<td>\" int($3*100/$2) \"%\" }'").read()
os.popen("ssh 127.0.0.1 '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()
请帮帮忙。如果有一组有效的命令,我们将不胜感激。
谢谢
发布于 2013-06-25 14:32:17
对不起,我没有一个可以测试的目标,但是如果你用三重引号而不是单引号来构造字符串,你会发现它容易得多-这将减少所需的转义,并帮助你找到你的问题。为了提高可读性,我还会考虑再次使用"".join( )构造字符串。
发布于 2013-06-25 14:44:05
用于ssh python模块的东西(如paramiko、pexpect...)我的建议是pexpect使用示例代码Python: How can remote from my local pc to remoteA to remoteb to remote c using Paramiko,Python - Pxssh - Getting an password refused error when trying to login to a remote server
发布于 2013-06-25 16:24:34
此外,您应该使用subprocess
而不是os.popen
,在shell命令的上下文中,可以很容易地使用
def shellquote(*strs):
r"""Input: strings, output: String enclosed with '' and every ' replaced with '\''. For use with the shell."""
# just take everything except '; replace ' with '\''
# ''' is seen by the shell as ''\'''\'''\'''. Ugly, but not harmful.
return " ".join([
"'"+st.replace("'","'\\''")+"'"
for st in strs
])
您可以轻松地使用它来补偿SSH连接另一端的shell操作。
import subprocess
def sshcall(*args):
return subprocess.check_output(['ssh', '127.0.0.1'] + list(args))
sshcall('echo', sq("<td>") + '$(uname -ri)' + sq("</td>"))
# '<td>3.9.3-9.g0b5d8f5-desktop i386</td>\n'
sshcall('free | grep "Mem:" | awk ' + sq('{print "<td>" $2/1024 " MB(" int($4*100/$2) "%)</td>" }'))
# '<td>3017.93 MB(20%)</td>\n'
sshcall('free | grep "Swap:" | awk ' + sq('{ print "<td>" int($3*100/$2) "%" }'))
# '<td>3%\n'
最后一行我将作为您的示例。
您只需向远程系统请求数据并自己传输数据,就可以大大简化这一过程。在这种情况下,您不必担心shell引用,因为大部分转换都是在本地完成的。该连接仅用于查询原始信息:
un = sshcall('uname -ri')
fr = sshcall('free')
cpu = sshcall('cat /proc/cpuinfo')
或者甚至只使用一个连接-
un, fr, cpu = sshcall('uname -ri; echo XXXXX; free; echo XXXXX; cat /proc/cpuinfo').split("XXXXX")
可以使用以下命令分析和输出数据
import re
do_re1 = lambda r, s: re.search(r, s).group(1).strip()
nums = lambda r, s: [int(x) for x in do_re1(r, s).split()]
print "<td>" + un.strip() + "</td>"
swapnums = nums("Swap:(.*)\n", fr)
memnums = nums("Mem:(.*)\n", fr)
print "<td> %f MB (%f %%) </td>" + % (memnums[0] / 1024.0, memnums[2] * 100.0 / memnums[0])
# swapnums: similiar
numcpus = len(re.findall("rocessor.*:(.*)\n", cpu))
freqs = [i.group(1).strip() for i in re.finditer("MHz.*:(.*)\n", cpu)]
cachesizes = [i.group(1).strip() for i in re.finditer("cache size.*:(.*)\n", cpu)]
# output them as you want
https://stackoverflow.com/questions/17290454
复制相似问题