A primitive way of doing this would be to use the commandline tools available on your OS. Run the tool using the subprocess
module (not os.system()
!), collect the output, and parse it.
On Windows, the command you want is ipconfig /all
.
On most Unices, including Linux, OSX and BSD, it's ifconfig
.
There may be a better way of doing this without shelling out to a command-line utility, but I don't know it... yet.
Example output of ipconfig /all
on Windows XP:
D:\Documents and Settings\LAYip>ipconfig /all
Windows IP Configuration
Host Name . . . . . . . . . . . . : <redacted>
Primary Dns Suffix . . . . . . . : <redacted>
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : <redacted>
<redacted>
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix . : <redacted>
Description . . . . . . . . . . . : Intel(R) 82579LM Gigabit Network Con
nection #2
Physical Address. . . . . . . . . : 5C-26-0A-60-8D-C7
Dhcp Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
IP Address. . . . . . . . . . . . : xxx.xxx.28.29
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : xxx.xxx.28.254
DHCP Server . . . . . . . . . . . : xxx.xxx.23.13
DNS Servers . . . . . . . . . . . : xxx.xxx.23.13
xxx.xxx.23.11
Lease Obtained. . . . . . . . . . : Thursday, 12 April 2012 9:14:41 AM
Lease Expires . . . . . . . . . . : Friday, 20 April 2012 9:14:41 AM
Ethernet adapter VirtualBox Host-Only Network:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : VirtualBox Host-Only Ethernet Adapter
Physical Address. . . . . . . . . : 08-00-27-00-28-E6
Dhcp Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.56.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
Output of ifconfig
under Linux:
lws@helios:~$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:25:22:db:8c:b6
inet addr:10.1.1.2 Bcast:10.1.1.255 Mask:255.255.255.0
inet6 addr: fe80::225:22ff:fedb:8cb6/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:322333 errors:0 dropped:0 overruns:0 frame:0
TX packets:296952 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:40005876 (40.0 MB) TX bytes:162343969 (162.3 MB)
Interrupt:40 Base address:0x4000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:362 errors:0 dropped:0 overruns:0 frame:0
TX packets:362 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:31806 (31.8 KB) TX bytes:31806 (31.8 KB)