rgfwwq 发表于 2015-6-29 09:15:08

用python获取MAC地址和IP地址

# ifconfig eth0
eth0      Link encap:EthernetHWaddr 50:E5:49:3A:EA:90
          inet addr:172.28.10.71Bcast:172.28.10.255Mask:255.255.255.0
          inet6 addr: fe80::52e5:49ff:fe3a:ea90/64 Scope:Link
          UP BROADCAST RUNNING MULTICASTMTU:1500Metric: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的方式来获取它们

1
2
3
4
5
def get_max_address():
    import uuid
    node=uuid.getnode()
    mac=uuid.UUID(int=node).hex[-12:]
    return mac





输出结果:
50e5493aea90

1
2
3
4
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()
    return t.split('\n')





输出结果为:
172.28.10.71








参考文章:
https://docs.python.org/2.6/library/uuid.html

页: [1]
查看完整版本: 用python获取MAC地址和IP地址