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

srinivasan srinivasan.rns at gmail.com
Mon Nov 12 14:37:06 EST 2018


Dear Python Experts,

*First method:*

I need to get the IP address basically the gateway IP in my setup I get it
as "192.168.178.1" when I run the below standalone python code.


*    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 <http://0.0.0.0/0> dev wlp1s0 | cut
-d\  -f3'*
*        f = os.popen(cmd)*
*        return str(f.read().strip())*


I am trying to log the IP in the robot framework script and ensure that my
router is able to ping but "*${SSID_GATEWAY_IP}*" doesn't seem to get
collected from the above python code and pass this value to my custom
method "*Wait Until Device Is Pingable"*

*    ${RET} =    Wait Until Device Is Pingable     ${SSID_GATEWAY_IP}*
*    Should Be True    ${RET}*

But whenever I hardcode the "*${SSID_GATEWAY_IP}  192.168.178.1*" in the
robot framework, it seems to be working with "*Wait Until Device Is
Pingable     ${SSID_GATEWAY_IP}*"

But the below robot framework script doesn't seems to work with the return
value received from the above python script, could you please do the
needful?

*Get Gateway IP of SSID*
* ${SSID_GATEWAY_IP} =     Get Gateway Ip*
* Log  ${SSID_GATEWAY_IP}*
*    ${RET} =    Wait Until Device Is Pingable     ${SSID_GATEWAY_IP}*
*    Should Be True    ${RET}*


*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)

        #list = os.popen('ip route show 0.0.0.0/0 dev wlp1s0 | cut -d\
-f3').read()

        # p = Popen(cmd, shell=True, stdout=PIPE)
        # out, err = p.communicate()
        # #return (p.returncode, out, err)
        # return out
        # #print('returncode: %s' % result[0])
        # #print('output: %s' % result[1])
        # #print('error: %s' % result[2])

        #return self._helper.execute_cmd_output_string(cmd)


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

Kindly do the needful as early as possible

Many Thanks in adavnce



More information about the Python-list mailing list