用python擷取MAC地址和IP地址

來源:互聯網
上載者:User

標籤:mac

# 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           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地址

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.