Unable to get the gateway IP of wlan interface using python code

MRAB python at mrabarnett.plus.com
Mon Nov 12 16:17:16 EST 2018


On 2018-11-12 19:37, srinivasan wrote:
> Dear Python Experts,
> 
[snip]
> 
> *SECOND METHOD:*
> 
> When I use the subprocess I see the below issue:
> 
> 
> 
>      def get_gateway_ip(self):
>          """
>              Get the IP address to the WIFI module from the AP
>          """
> 
>          #cmd = 'ip route show 0.0.0.0/0 dev wlp1s0 | cut -d\  -f3'
>          # f = os.popen(cmd)
>          # return str(f.read().strip())
> 
> 
>          p = subprocess.Popen('ip route show 0.0.0.0/0 dev wlp1s0 | cut -d\
> -f3', stdout=subprocess.PIPE)
>          result = p.communicate()[0]
>          print(result)
> 
> 
[snip]
> 
> Error:
> 
> root:~/qa/test_library# python3 wifi.py
> Enabling wifi
> Verify wifi connectivity
> True
> Get gateway wifi ip
> Traceback (most recent call last):
>    File "wifi.py", line 134, in <module>
>      print(m.get_gateway_ip())
>    File "wifi.py", line 76, in get_gateway_ip
>      p = subprocess.Popen('ip route show 0.0.0.0/0 dev wlp1s0 | cut -d\
> -f3', stdout=subprocess.PIPE)
>    File "/usr/lib/python3.5/subprocess.py", line 676, in __init__
>      restore_signals, start_new_session)
>    File "/usr/lib/python3.5/subprocess.py", line 1289, in _execute_child
>      raise child_exception_type(errno_num, err_msg)
> FileNotFoundError: [Errno 2] No such file or directory: 'ip route show
> 0.0.0.0/0 dev wlp1s0 | cut -d\\  -f3'
> root:~/qa/test_library#
> 
> 
> As I am stuck with this issue from past 2 days, wondering for any clues
> 
'Popen' thinks that the string you gave it is the path to a command.

It's not; it's a command line with arguments.
You can tell it that the string is a command line by also passing the 
keyword argument 'shell=True':

p = subprocess.Popen(r'ip route show 0.0.0.0/0 dev wlp1s0 | cut -d\ 
-f3', stdout=subprocess.PIPE, shell=True)



More information about the Python-list mailing list