# ifconfig eth0eth0 link encap:ethernet hwaddr 50:e5:49:3a:ea:90 inet addr :172.28.10.71 bcast:172.28.10.255 mask:255.255.255.0 inet6 addr: fe80::52e5:49ff:fe3a:ea90/64 Scope:Link up broadcast running multicast mtu:1500 Metric:1 RX packets:3457606 errors:0 dropped:0 overruns:0 frame:0 TX packets:255283 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 &nbSp rx bytes:503195543 (479.8 MIB) TX bytes:30327237 (28.9 MIB)
MAC address is 50:e5:49:3a:ea:90
IP address is 172.28.10.71
Now use Python to get them.
Def get_max_address (): Import uuid Node=uuid.getnode () mac=uuid. UUID (Int=node). HEX[-12:] Return mac
Output Result:
50e5493aea90
Def get_ip (): args= ' ifconfig|grep ' inet addr: ' |awk ' {print $} ' |awk-f ': ' {print $} ' |grep-v ' 127.0.0.1 ' T=su Bprocess. Popen (args,shell=true,stdout=subprocess. PIPE). Communicate () [0] return t.split (' \ n ') [0]
The output is:
172.28.10.71
Reference article:
Https://docs.python.org/2.6/library/uuid.html
This article is from the Linux SA John blog, so be sure to keep this source http://john88wang.blog.51cto.com/2165294/1668818
Get MAC address and IP address in Python