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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
import os
import re
import socket
import struct
def readlines(fd):
"""Version of readlines safe against EINTR.
"""
import errno
lines = []
while 1:
try:
line = fd.readline()
except IOError, ex:
if ex.errno == errno.EINTR:
continue
else:
raise
if line == '': break
lines.append(line)
return lines
def readline(fd):
"""Version of readline safe against EINTR.
"""
while 1:
try:
return fd.readline()
except IOError, ex:
if ex.errno == errno.EINTR:
continue
else:
raise
##### Networking-related functions
"""Bridge for network backend.
When bridging is used, eth0 may not have an IP address,
as it may have been moved onto the bridge.
"""
NBE_BRIDGE = 'nbe-br'
def get_current_ipaddr(dev='eth0'):
"""Return a string containing the primary IP address for the given
network interface (default 'eth0').
"""
fd = os.popen( '/sbin/ifconfig ' + dev + ' 2>/dev/null' )
lines = readlines(fd)
for line in lines:
m = re.search( '^\s+inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*',
line )
if m:
return m.group(1)
if dev == 'eth0':
return get_current_ipaddr(NBE_BRIDGE)
return None
def get_current_ipmask(dev='eth0'):
"""Return a string containing the primary IP netmask for the given
network interface (default 'eth0').
"""
fd = os.popen( '/sbin/ifconfig ' + dev + ' 2>/dev/null' )
lines = readlines(fd)
for line in lines:
m = re.search( '^.+Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*',
line )
if m:
return m.group(1)
if dev == 'eth0':
return get_current_ipmask(NBE_BRIDGE)
return None
def get_current_ipgw(dev='eth0'):
"""Return a string containing the IP gateway for the given
network interface (default 'eth0').
"""
fd = os.popen( '/sbin/route -n' )
lines = readlines(fd)
for line in lines:
m = re.search( '^\S+\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)' +
'\s+\S+\s+\S*G.*' + dev + '.*', line )
if m:
return m.group(1)
if dev == 'eth0':
return get_current_ipgw(NBE_BRIDGE)
return None
def inet_aton(addr):
"""Convert an IP addr in IPv4 dot notation into an int.
"""
b = socket.inet_aton(addr)
return struct.unpack('!I', b)[0]
def inet_ntoa(n):
"""Convert an int into an IP addr in IPv4 dot notation.
"""
b = struct.pack('!I', n)
return socket.inet_ntoa(b)
def add_offset_to_ip(addr, offset):
"""Add a numerical offset to an IP addr in IPv4 dot notation.
"""
n = inet_aton(addr)
n += offset
return inet_ntoa(n)
def check_subnet( ip, network, netmask ):
n_ip = inet_aton(ip)
n_net = inet_aton(network)
n_mask = inet_aton(netmask)
return (n_ip & n_mask) == (n_net & n_mask)
|