Python Forum
use subprocess on linux\pi wwith a "grep " command
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
use subprocess on linux\pi wwith a "grep " command
#1
Hello,
(maybe someone have a simpler solution )
I need to know the status of my modem "UNKNOWN\UP\DOWN"
so I'm running this command:
ip a | grep "wwan"
and this is the response I'm getting:

3: wwan0: <POINTOPOINT,MULTICAST,NOARP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    inet 10.164.47.168/28 scope global wwan0
then I will search for "UNKNOWN\UP\DOWN" in the response and know what to do

I have try to use
result = subprocess.run(['ip', 'a', '| grep wwan' ], stdout=subprocess.PIPE)
but I get this
Command "| grep wwan" is unknown, try "ip address help".
what did I do wrong?
something in the order of the args?

Thanks ,

*** if someone have a better way to check the status - it will also be great

Thanks,
Reply
#2
You can try this
import subprocess as sp
p = sp.run('ip a | grep wwan', shell=True, capture_output=True)
print(p.stdout)
Without shell=True, you can also combine processes
ip = sp.Popen(['ip', 'a'], stdout=sp.PIPE)
grep = sp.Popen(['grep', 'wwan'], stdin=ip.stdout, stdout=sp.PIPE)
lines = list(grep.stdout)
Reply
#3
[andre@andre-Fujitsu-i5 ~]$ ip -j link show wlan0 | python -m json.tool
[
{
"ifindex": 3,
"ifname": "wlan0",
"flags": [
"BROADCAST",
"MULTICAST",
"UP",
"LOWER_UP"
],
"mtu": 1500,
"qdisc": "noqueue",
"operstate": "UP",
"linkmode": "DORMANT",
"group": "default",
"txqlen": 1000,
"link_type": "ether",
"address": "74:e5:f9:4e:73:70",
"broadcast": "ff:ff:ff:ff:ff:ff"
}
]
The tool ip supports json output, which can be processed with Python.
You need the operstate.

import json
from argparse import ArgumentParser
from subprocess import PIPE, run


def get_state(interface):
    cmd = ["ip", "-j", "link", "show", interface]
    proc = run(cmd, encoding="utf8", stdout=PIPE, stderr=PIPE)
    if proc.returncode != 0:
        return proc.stderr

    return json.loads(proc.stdout)[0]["operstate"]


def get_args():
    parser = ArgumentParser()
    parser.add_argument("interface", help="Name of interface to get state from.")
    return parser.parse_args()


def main():
    args = get_args()
    return get_state(args.interface)


if __name__ == "__main__":
    print(main())
Output:
[andre@andre-Fujitsu-i5 ~]$ python get_state.py wlan0 UP [andre@andre-Fujitsu-i5 ~]$ python get_state.py eno1 Device "eno1" does not exist. [andre@andre-Fujitsu-i5 ~]$ python get_state.py enp3s0 UP [andre@andre-Fujitsu-i5 ~]$ python get_state.py lo UNKNOWN [andre@andre-Fujitsu-i5 ~]$
Gribouillis likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 723 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Using subprocess to execute complex command with many arguments medatib531 5 1,845 Apr-27-2023, 02:23 PM
Last Post: medatib531
  How to use a variable in linux command in python code? ilknurg 2 1,597 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  how to run linux command with multi pipes by python !! evilcode1 2 6,326 Jan-25-2021, 11:19 AM
Last Post: DeaD_EyE
  How to grep corresponding value in another df Mekala 2 1,847 Oct-13-2020, 02:52 PM
Last Post: Mekala
  Error when running mktorrent subprocess command pythonnewbie138 4 3,837 Sep-16-2020, 01:55 AM
Last Post: pythonnewbie138
  Select correct item from list for subprocess command pythonnewbie138 6 3,292 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138
  Subprocess command working for one cmd and for cmd one not wrking PythonBeginner_2020 0 4,132 Mar-25-2020, 01:52 PM
Last Post: PythonBeginner_2020
  grep value files enigma619 0 1,499 Mar-04-2020, 02:21 PM
Last Post: enigma619
  execute linux command with arguments sivareddy 2 2,240 Feb-10-2020, 03:23 PM
Last Post: sivareddy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020