8/27/2016 - 9:59 AM

get the ip address from linux with python

get the ip address from linux with python

#get the mac address
import uuid
def get_mac_address(): 
    mac=uuid.UUID(int = uuid.getnode()).hex[-12:] 
    return ":".join([mac[e:e+2] for e in range(0,11,2)])

#get the ip address
import socket
def get_inet_ipaddress():
  myname = socket.getfqdn(socket.gethostname())
  myaddr = socket.gethostbyname(myname)
  #the name of computer
  print myname
  #the ineter ip of the computer
  print myaddr

import socket
import fcntl
import struct
def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])

#>>> get_ip_address('lo')
#>>> get_ip_address('eth0')