Get MAC address and IP address in Python

Source: Internet
Author: User
Tags uuid

# 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.