码迷,mamicode.com
首页 > 编程语言 > 详细

用python获取MAC地址和IP地址

时间:2015-06-29 00:47:55      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:mac

# 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





本文出自 “Linux SA John” 博客,请务必保留此出处http://john88wang.blog.51cto.com/2165294/1668818

用python获取MAC地址和IP地址

标签:mac

原文地址:http://john88wang.blog.51cto.com/2165294/1668818

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!