简体   繁体   English

在 Python 中使用管道执行 shell 命令

[英]Execute shell command with pipes in Python

I'm new to Python, tried googling, but no help..我是 Python 新手,尝试过谷歌搜索,但没有帮助..
I need to call such commands in pipes ( Get the oldest pending mail from mailq ):我需要在管道中调用这样的命令(从 mailq 获取最旧的待处理邮件):

mailq |grep "^[A-F0-9]" |sort -k5n -k6n |head -n 1

The command works in shell.该命令在 shell 中有效。

In Python I wrote the following:在 Python 中,我写了以下内容:

 p = subprocess.Popen( 'mailq |grep \"^[A-F0-9]\" |sort -k5n -k6n |head -n 1', shell=True,
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
 response = p.communicate()[0]

But I get such output:但我得到这样的输出:

sort: write failed: standard output: Broken pipe\\nsort: write error\\n排序:写入失败:标准输出:管道损坏\\n排序:写入错误\\n

Wondering what is causing such error?想知道是什么导致了这样的错误?

I think this should work:我认为这应该有效:

p = subprocess.Popen( 'mailq |grep \"^[A-F0-9]\" |sort -k5n -k6n |head -n 1', shell=True,
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
response = p.stdout.readlines(-1)[0]
print response

prints the first line of the response打印响应的第一行

Instead of making shell take care of splitting your command to several processes and piping them, do that yourself.与其让 shell 负责将您的命令拆分为多个进程并通过管道传输它们,不如自己做。 See here how to pipe one subprocess stream to another subprocess.请参阅此处如何将一个子流程流通过管道传输到另一个子流程。

That way you can look up outputs of each step (for example by routing stdout to your stdout, just to debug) and figure out if your whole workflow is OK.这样您就可以查找每个步骤的输出(例如,通过将 stdout 路由到您的 stdout,只是为了调试)并确定您的整个工作流程是否正常。

It would look a bit like this:它看起来有点像这样:

mail_process = subprocess.Popen('mailq', stdin=PIPE, stdout=PIPE, stderr=STDOUT)
grep_process = subprocess.Popen(['grep', '\"^[A-F0-9]"'], stdin=mail_process.stdout, stdout=PIPE, stderr=STDOUT]
...
head_process = subprocess.Popen(["head", ...], ...)
head_process.communicate()[0]

I'd suggest you use subprocess as written here: http://kendriu.com/how-to-use-pipes-in-python-subprocesspopen-objects我建议你使用这里写的子进程: http : //kendriu.com/how-to-use-pipes-in-python-subprocesspopen-objects

ls = subprocess.Popen('ls /etc'.split(), stdout=subprocess.PIPE)
grep = subprocess.Popen('grep ntp'.split(), stdin=ls.stdout, stdout=subprocess.PIPE)
output = grep.communicate()[0]

This is the pythonic way of using pipes.这是使用管道的pythonic方式。

Python3蟒蛇3

shell = subprocess.run(["./snmp.sh","10.117.11.55","1.3.6.1.4.1.43356.2.1.2.1.1.0"],check=True,capture_output=True)
print(shell)

Shell贝壳

#!/bin/bash
args=("$@")

snmpwalk -v 1 -c public ${args[0]} ${args[1]}
output = subprocess.check_output(["awk",'{print$4}'"],input=shell.stdout,capture_output=True)
print(output) 

I was getting an error like this我收到这样的错误

Output = [Errno 2] No such file or directory: "awk '{print $4}'"输出 = [Errno 2] 没有这样的文件或目录:“awk '{print $4}'”

The place where I fixed the error was just adding a pipe at the end of the sh file.我修复错误的地方只是在 sh 文件的末尾添加了一个管道。

Shell贝壳

#!/bin/bash
args=("$@")

snmpwalk -v 1 -c public ${args[0]} ${args[1]} | awk '{print $4}'

Hope it helps somebody希望它可以帮助某人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM