SSH中的Python多个命令

时间:2013-06-25 06:21:33

标签: python shell ssh awk

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

请帮忙。一组有效的命令将不胜感激。

由于

3 个答案:

答案 0 :(得分:0)

抱歉,我没有可以测试的目标,但是如果你用三引号而不是单引号构造你的字符串会更容易 - 这将减少所需的转义并帮助你找到你的问题。我还会考虑再次使用“”.join([])构造字符串以便于阅读。

答案 1 :(得分:0)

答案 2 :(得分:0)

除此之外,你应该使用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