Re: [Tutor] Unable to get the gateway IP of wlan interface using python code

2018-11-12 Thread David Rock

> On Nov 12, 2018, at 13:37, srinivasan  wrote:
> 
> 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.

Is there a requirement to use only what comes in the standard libraries, or can 
you use things from pypi?
Getting interface details is exactly why netifaces was created

https://pypi.org/project/netifaces/

damocles:src drock$ python3
Python 3.7.0 (default, Oct 28 2018, 22:17:08) 
[Clang 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import netifaces
>>> gws = netifaces.gateways()
>>> gws
{'default': {2: ('192.168.69.1', 'en0')}, 2: [('192.168.69.1', 'en0', True)]}
>>> gws['default']
{2: ('192.168.69.1', 'en0’)}

— 
David Rock
da...@graniteweb.com




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unable to get the gateway IP of wlan interface using python code

2018-11-12 Thread Bill Campbell
On Tue, Nov 13, 2018, srinivasan wrote:
>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  dev wlp1s0 | cut
>-d\  -f3'*
>*f = os.popen(cmd)*
>*return str(f.read().strip())*

This command should get the gateway IP.

Linux: cmd = "ip route list | awk '/^default/{print $3}'"

or perhaps

Linux: cmd = "netstat -rn | awk '/^0.0.0.0/{print $2}'"

OSX: cmd = "netstat -rn | awk '/^default/{print $2}'"

I don't have a freebsd system available to test this, but I think
this pattern should work:

re.compile(r'^(default|0\.0\.0\.0)\s+(\S+)')

Bill
-- 
INTERNET:   b...@celestial.com  Bill Campbell; Celestial Software LLC
URL: http://www2.celestial.com/ PO Box 820; 6641 E. Mercer Way
Mobile: (206) 947-5591  Mercer Island, WA 98040-0820
Fax:(206) 232-9186  Skype: jwccsllc

On the free market, everyone earns according to his productive
value in satisfying consumer desires. Under statist distribution,
everyone earns in proportion to the amount he can plunder from
the producers. -- Murray N. Rothbard
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2018-11-12 Thread srinivasan
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  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 
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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor