Skip to content

Instantly share code, notes, and snippets.

@atomd-zz
Created January 22, 2013 06:08
Show Gist options
  • Select an option

  • Save atomd-zz/4592494 to your computer and use it in GitHub Desktop.

Select an option

Save atomd-zz/4592494 to your computer and use it in GitHub Desktop.
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(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
print get_ip_address('lo')
# OUT: 127.0.0.1
print get_ip_address('eth0')
# OUT: 192.168.11.90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment