-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifconfig.py
36 lines (27 loc) · 911 Bytes
/
ifconfig.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
35
36
import re
import subprocess
def ip_mask(ifname):
"""get IP address and netmask for an interface if it has them
The return is a tuple containing the address and netmask as strings. If
either cannot be determined, None is returned in its place. If there is an
error running the ifconfig command, None is returned instead of the tuple.
Example:
get_ip_mask("wlan0")
normally returns something like:
("10.1.1.100", "255.255.255.0")
but could return something like:
("10.1.1.100", None)
"""
try:
out = subprocess.check_output(["ifconfig", ifname])
except:
return None
m = re.search("inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)", out)
if not m:
return None
ip = m.group(1)
m = re.search("Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)", out)
if not m:
return None
mask = m.group(1)
return (ip, mask)