如何在subprocess.Popen中使用列表将某些值存储在python的另一个列表中?

时间:2019-05-10 17:41:51

标签: python list for-loop subprocess popen

我有2个mac地址存储在一个list(maclist)中,并且使它们从文件中被“获取”。我认为for循环可以正常工作,但是输出(iplist)中没有任何内容。

该文件同时包含mac地址和ip,因此使用mac可以获取ip。

iplist = []
leasefile="/var/lib/misc/dnsmasq.leases"

p9 = subprocess.Popen(["cut", "-f", "2,3,4", "-s", "-d", " ", leasefile], stdout=subprocess.PIPE)

for line in p9.stdout:
    for i in range(len(maclist)):
        p10 = subprocess.Popen(["grep", str(maclist[i])], stdin=p9.stdout,
            stdout=subprocess.PIPE)

        p11 = subprocess.Popen(["cut", "-f", "2", "-s", "-d", " "], encoding="utf8", 
            stdin=p10.stdout, stdout=subprocess.PIPE)

        for line2 in p11.stdout:
            iplist.append(line2.rstrip('\n'))
    print(iplist)

0 个答案:

没有答案