2013-06-25 39 views
1

我想用os.popen()在Python上通过SSH执行这4个命令。Python中的多个命令

问题在于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() 

请帮忙。一套可用的命令将不胜感激。

感谢

回答

0

对不起,我没有目标,我可以考到,但你会发现这是一个容易得多,如果您建造在三引号字符串,而不是单一的 - 这将削减逸出需要,并帮助你找到你的问题。为了便于阅读,我还会考虑再次使用“”.join([])构造字符串。您使用SSH Python模块

+0

数据我想这os.popen('''ssh 127.0.0.1'echo“​​”$(uname -ri)“”; free | grep“Mem:”| awk'{print“​​”$ 2/1024“MB(”int $ 4 * 100/$ 2)“%)“}''''').read() – user2359303

+0

但是,这给出了一个错误sh:1:语法错误:”(“unexpected – user2359303

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连接的另一端补偿shel​​l的行为。

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