用python获取MAC地址和IP地址
# ifconfig eth0
eth0 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
RX bytes:503195543 (479.8 MiB) TX bytes:30327237 (28.9 MiB)
Mac地址是50:E5:49:3A:EA:90
IP地址是172.28.10.71
现用python的方式来获取它们
def get_max_address():
import uuid
node=uuid.getnode()
mac=uuid.UUID(int=node).hex[-12:]
return mac
输出结果:
50e5493aea90
def get_ip():
args='''ifconfig|grep 'inet addr:'|awk '{print $2}'|awk -F':' '{print $2}'|grep -v "127.0.0.1"'''
t=subprocess.Popen(args,shell=True,stdout=subprocess.PIPE).communicate()[0]
return t.split('\n')[0]
输出结果为:
172.28.10.71
参考文章:
https://docs.Python.org/2.6/library/uuid.html
相关文章