-
Notifications
You must be signed in to change notification settings - Fork 8
/
getaddrs.py
34 lines (27 loc) · 834 Bytes
/
getaddrs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/python
"""
Usage: getaddrs.py [interface name]
"""
# http://code.activestate.com/recipes/439094-get-the-ip-address-associated-with-a-network-inter/
import fcntl
import socket
import struct
import sys
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])
def get_hardware_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15]))
return ':'.join(['%02x' % ord(char) for char in info[18:24]])
try:
scriptname, myif = sys.argv
except ValueError:
print __doc__
sys.exit(1)
print get_hardware_address(myif)
print get_ip_address(myif)