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

Bill Campbell bill at celestial.net
Mon Nov 12 18:54:19 EST 2018


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 <http://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:   bill at 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



More information about the Python-list mailing list