在python中,将子流程附加到列表似乎只是附加子流程对象位置?
在控制台上使用以下命令打印wlan0 NIC的本地MAC地址。我想将其集成到一个脚本中,在该脚本中,列表的第0个子列表将用exer中的本地MAC填充在python中,将子流程附加到列表似乎只是附加子流程对象位置?,python,subprocess,Python,Subprocess,在控制台上使用以下命令打印wlan0 NIC的本地MAC地址。我想将其集成到一个脚本中,在该脚本中,列表的第0个子列表将用exer中的本地MAC填充 ifconfig wlan0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' 正在使用的列表Localization从名为scanned的dict中获取其第一和第二个子列表 因此,我希望第0个子列表中有本地MAC,第1和第2个子列表中的每个条目都有一个条目。我尝试过以下代码: for
ifconfig wlan0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
正在使用的列表Localization从名为scanned的dict中获取其第一和第二个子列表
因此,我希望第0个子列表中有本地MAC,第1和第2个子列表中的每个条目都有一个条目。我尝试过以下代码:
for s in scanned:
localisation[0].append(subprocess.Popen("ifconfig wlan0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'", shell=True))
但我只是
<subprocess.Popen object at 0x2bf0f50>
对于列表中的每个条目。尽管有正确数量的条目
我还有一个问题,出于某种原因,程序将代码输出打印到屏幕上,这是我不希望发生的
我做错了什么?从某种意义上说,popen对象是一个文件对象
from subprocess import *
handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE)
handle.stdin.write('echo "Heeey there"')
print handle.stdout.read()
handle.flush()
并非所有输出都被重定向的原因是stderr=PIPE,否则无论发生什么情况,它都会回显到控制台中。将其重定向到管道是一个好主意
另外,除非您知道为什么需要它,否则使用shell=True
通常是个坏主意。。在这种情况下(我不认为)你不需要它
aa最后,您需要将要执行的命令划分为一个列表,至少是一个2项或更多项的列表。例如:
c = ['ssh', '-t', 'user@host', "service --status-all"]
这将是ssh-tuser@root“service--status all”
通常,请注意“service--status all”
的未拆分部分,因为在我的示例中,这是作为一个整体发送到SSH客户端的参数
不要尝试,尝试:
c = ["ifconfig", "wlan0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'"]
甚至:
from uuid import getnode as get_mac
mac = get_mac()
这是我的尝试:
使用check\u output
给出该命令的输出
In [3]: import subprocess
In [4]: subprocess.check_output("ip link show wlan0 | grep link | awk '{print $2}'",shell=True).strip()
Out[4]: '48:5d:60:80:e5:5f'
使用
ip link show
而不是ifconfig保存sudo命令。仅调用Popen
仅返回一个新的Popen
实例:
In [54]: from subprocess import Popen,PIPE
In [55]: from shlex import split #use shlex.split() to split the string into correct args
In [57]: ifconf=Popen(split("ifconfig wlan0"),stdout=PIPE)
In [59]: grep=Popen(split("grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'"),
stdin=ifconf.stdout,stdout=PIPE)
In [60]: grep.communicate()[0]
Out[60]: '00:29:5e:3b:cc:8a\n'
In [64]: grep.communicate?
Type: instancemethod
String Form:<bound method Popen.communicate of <subprocess.Popen object at 0x8c693ac>>
File: /usr/lib/python2.7/subprocess.py
Definition: grep.communicate(self, input=None)
Docstring:
Interact with process: Send data to stdin. Read data from
stdout and stderr, until end-of-file is reached. Wait for
process to terminate. The optional input argument should be a
string to be sent to the child process, or None, if no data
should be sent to the child.
communicate() returns a tuple (stdout, stderr).
使用communicate()
从特定Popen
实例的stdin
、stderr
读取数据:
In [54]: from subprocess import Popen,PIPE
In [55]: from shlex import split #use shlex.split() to split the string into correct args
In [57]: ifconf=Popen(split("ifconfig wlan0"),stdout=PIPE)
In [59]: grep=Popen(split("grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'"),
stdin=ifconf.stdout,stdout=PIPE)
In [60]: grep.communicate()[0]
Out[60]: '00:29:5e:3b:cc:8a\n'
In [64]: grep.communicate?
Type: instancemethod
String Form:<bound method Popen.communicate of <subprocess.Popen object at 0x8c693ac>>
File: /usr/lib/python2.7/subprocess.py
Definition: grep.communicate(self, input=None)
Docstring:
Interact with process: Send data to stdin. Read data from
stdout and stderr, until end-of-file is reached. Wait for
process to terminate. The optional input argument should be a
string to be sent to the child process, or None, if no data
should be sent to the child.
communicate() returns a tuple (stdout, stderr).
[64]中的:grep.communicate?
类型:instancemethod
字符串形式:
文件:/usr/lib/python2.7/subprocess.py
定义:grep.communication(self,input=None)
文档字符串:
与进程交互:向stdin发送数据。从中读取数据
stdout和stderr,直到到达文件末尾。等待
进程终止。可选输入参数应为
要发送到子进程的字符串,如果没有数据,则为无
应该送给孩子。
communicate()返回一个元组(stdout、stderr)。
要替换python中的纯shell脚本,您可能需要查看模块--sh.grep(sh.ifconfig('wlan0')、'-o'、'-E',r'([[:xdigit:]]{1,2}:]{5}[:xdigit:]{1,2}')
您应该使用python正则表达式而不是grep