简体   繁体   中英

How to write a python script to get the IP and IPs the of the routers to which my Windows OS computer is connected and also the device name?

For example, I have connected to a LAN using the ethernet cable and to the internet using the WiFi. My computer has got 2 more interfaces that are currently not used.

The script should check for only active interfaces and return their IPs, router IPs, and the device names.

ip配置画面

I would use netifaces . Like this:

import netifaces as ni

for iface in ni.interfaces():
    print('interface : {0}'.format(iface))
    try:
        addrs = ni.ifaddresses(iface)
        for k, v in addrs[ni.AF_INET][0].items():
            print('   {0} : {1}'.format(k, v))
    except:
        pass

gways = ni.gateways()
print('gateway: {0}'.format(gways['default'][ni.AF_INET][0]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM