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