What I have done so far, using PyQt classes:
all_Addresses = QNetworkInterface.allAddresses() #list-of-QHostAddress for addr in all_Addresses: print(addr.toString())
Output:
172.16.0.186 - Virtual Interface IP address 192.168.10.2 - Physical interface IP address. I want this one. 127.0.0.1
Using socket
:
import socket print(socket.gethostbyname(socket.gethostname()))
Output:
172.16.0.186 - When openVPN is on 192.168.10.2 - When its off
Using the ipconfig Command The ipconfig command displays the basic IP addressing information for each network interface on the Windows system.
Type "show running-config" or "show config" and press the "Enter" key. The IP address or addresses assigned to interfaces on the switch are displayed in the command line interface output.
Each network interface must have its own unique IP address. The IP address that you give to a host is assigned to its network interface, sometimes referred to as the primary network interface. If you add a second network interface to a machine, it must have its own unique IP number.
You should use netifaces. It is designed to be cross-platform and contains specialised code for Windows together with a variety of generic versions that work on different UNIX/UNIX-like platforms.
As of netifaces version 0.10.0, Python3 is supported.
>>> from netifaces import AF_INET, AF_INET6, AF_LINK, AF_PACKET, AF_BRIDGE >>> import netifaces as ni >>> ni.interfaces() ['lo', 'eth0', 'eth1', 'vboxnet0', 'dummy1'] >>> >>> ni.ifaddresses('eth0')[AF_LINK] # NOTE: AF_LINK is an alias for AF_PACKET [{'broadcast': 'ff:ff:ff:ff:ff:ff', 'addr': '00:02:55:7b:b2:f6'}] >>> ni.ifaddresses('eth0')[AF_INET] [{'broadcast': '172.16.161.7', 'netmask': '255.255.255.248', 'addr': '172.16.161.6'}] >>> >>> # eth0 ipv4 interface address >>> ni.ifaddresses('eth0')[AF_INET][0]['addr'] '172.16.161.6' >>>>
No compiler required for most MS Windows installs. If you get warnings about installing MS Visual C++ for Windows, be very careful because you need to match the version of compiler used for your python with that used for the module.
>>> import netifaces as ni >>> ni.interfaces() ['lo', 'eth0', 'eth1', 'vboxnet0', 'dummy1'] >>> ni.ifaddresses('eth0') { 17: [ { 'broadcast': 'ff:ff:ff:ff:ff:ff', 'addr': '00:02:55:7b:b2:f6' } ], 2: [ { 'broadcast': '172.16.161.7', 'netmask': '255.255.255.248', 'addr': '172.16.161.6' } ], 10: [ { 'netmask': 'ffff:ffff:ffff:ffff::', 'addr': 'fe80::202:55ff:fe7b:b2f6%eth0' } ] } >>> >>> print(ni.ifaddresses.__doc__) Obtain information about the specified network interface. Returns a dict whose keys are equal to the address family constants, e.g. netifaces.AF_INET, and whose values are a list of addresses in that family that are attached to the network interface. >>> >>> # for the IPv4 address of eth0 >>> ni.ifaddresses('eth0')[2][0]['addr'] '172.16.161.6'
The numbers used to index protocols are from /usr/include/linux/socket.h
(in Linux)... EDIT: my 3.2 kernel has them here: /usr/src/linux-headers-3.2.0-4-common/include/linux/socket.h
#define AF_INET 2 /* Internet IP Protocol */ #define AF_INET6 10 /* IP version 6 */ #define AF_PACKET 17 /* Packet family */
The good news is that you don't have to remember all those header constants, they are included with netifaces:
>>> from netifaces import AF_INET, AF_INET6, AF_LINK, AF_PACKET, AF_BRIDGE >>> import netifaces as ni
Uses the Linux SIOCGIFADDR ioctl to find the IP address associated with a network interface, given the name of that interface, e.g. "eth0
". The address is returned as a string containing a dotted quad.
import socket import fcntl import struct def get_ip_address(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcntl.ioctl( s.fileno(), 0x8915, # SIOCGIFADDR struct.pack('256s', ifname[:15]) )[20:24]) >>> get_ip_address('lo') '127.0.0.1' >>> get_ip_address('eth0') '38.113.228.130'
For more
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With