run linux grep command from python subprocess

后端 未结 2 1345
孤独总比滥情好
孤独总比滥情好 2021-01-21 06:14

I know there are posts already on how to use subprocess in python to run linux commands but I just cant get the syntax correct for this one. please help. This is the command I n

2条回答
  •  长发绾君心
    2021-01-21 07:01

    This has been gone over many, many times before; but here is a simple pure Python replacement for the inefficient postprocessing.

    from subprocess import Popen, PIPE
    eth1 = subprocess.Popen(['/sbin/ifconfig', 'eth1'], stdout=PIPE)
    out, err = eth1.communicate()
    for line in out.split('\n'):
        line = line.lstrip()
        if line.startswith('inet addr:'):
            ip = line.split()[1][5:]
    

提交回复
热议问题