-
Notifications
You must be signed in to change notification settings - Fork 19
/
netinterface.py
187 lines (161 loc) · 5.75 KB
/
netinterface.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# SPDX-License-Identifier: GPL-2.0-only
import util
import netutil
def getText(nodelist):
rc = ""
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
return rc.strip().encode()
def getTextOrNone(nodelist):
rc = ""
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
return rc == "" and None or rc.strip().encode()
class NetInterface:
""" Represents the configuration of a network interface. """
Static = 1
DHCP = 2
Autoconf = 3
def __init__(self, mode, hwaddr, ipaddr=None, netmask=None, gateway=None,
dns=None, domain=None, vlan=None):
assert mode is None or mode == self.Static or mode == self.DHCP
if ipaddr == '':
ipaddr = None
if netmask == '':
netmask = None
if gateway == '':
gateway = None
if dns == '':
dns = None
elif isinstance(dns, str):
dns = [ dns ]
if mode == self.Static:
assert ipaddr
assert netmask
self.mode = mode
self.hwaddr = hwaddr
if mode == self.Static:
self.ipaddr = ipaddr
self.netmask = netmask
self.gateway = gateway
self.dns = dns
self.domain = domain
else:
self.ipaddr = None
self.netmask = None
self.gateway = None
self.dns = None
self.domain = None
self.vlan = vlan
# Initialise IPv6 to None.
self.modev6 = None
self.ipv6addr = None
self.ipv6_gateway = None
def __repr__(self):
hw = "hwaddr = '%s' " % self.hwaddr
if self.mode == self.Static:
ipv4 = "Static;" + \
"ipaddr='%s';netmask='%s';gateway='%s';dns='%s';domain='%s'>" % \
(self.ipaddr, self.netmask, self.gateway, self.dns, self.domain)
elif self.mode == self.DHCP:
ipv4 = "DHCP"
else:
ipv4 = "None"
if self.modev6 == self.Static:
ipv6 = "Static;" + \
"ipaddr='%s';gateway='%s'>" % \
(self.ipv6addr, self.ipv6_gateway)
elif self.modev6 == self.DHCP:
ipv6 = "DHCP"
elif self.modev6 == self.Autoconf:
ipv6 = "autoconf"
else:
ipv6 = "None"
vlan = ("vlan = '%d' " % self.vlan) if self.vlan else ""
return "<NetInterface: %s%s ipv4:%s ipv6:%s>" % (hw, vlan, ipv4, ipv6)
def get(self, name, default=None):
retval = default
if hasattr(self, name):
attr = getattr(self, name)
if attr is not None:
retval = attr
return retval
def getInterfaceName(self, iface):
return ("%s.%d" % (iface, self.vlan)) if self.vlan else iface
def addIPv6(self, modev6, ipv6addr=None, ipv6gw=None):
assert modev6 is None or modev6 == self.Static or modev6 == self.DHCP or modev6 == self.Autoconf
if ipv6addr == '':
ipv6addr = None
if ipv6gw == '':
ipv6gw = None
if modev6 == self.Static:
assert ipv6addr
self.modev6 = modev6
if modev6 == self.Static:
self.ipv6addr = ipv6addr
self.ipv6_gateway = ipv6gw
else:
self.ipv6addr = None
self.ipv6_gateway = None
def valid(self):
if (self.mode == self.Static) and ((self.ipaddr is None) or (self.netmask is None)):
return False
if (self.modev6 == self.Static) and (self.ipv6addr is None):
return False
return self.mode or self.modev6
def isStatic(self):
""" Returns true if a static interface configuration is represented. """
return self.mode == self.Static
def isVlan(self):
return self.vlan is not None
def getBroadcast(self):
bcast = None
rc, output = util.runCmd2(['/bin/ipcalc', '-b', self.ipaddr, self.netmask],
with_stdout=True)
if rc == 0:
bcast = output[10:].strip()
return bcast
def writeRHStyleInterface(self, iface):
""" Write a RedHat-style configuration entry for this interface to
file object f using interface name iface. """
assert self.modev6 is None
assert self.mode
iface_vlan = self.getInterfaceName(iface)
f = open('/etc/sysconfig/network-scripts/ifcfg-%s' % iface_vlan, 'w')
f.write("DEVICE=%s\n" % iface_vlan)
f.write("ONBOOT=yes\n")
if self.mode == self.DHCP:
f.write("BOOTPROTO=dhcp\n")
f.write("PERSISTENT_DHCLIENT=1\n")
else:
# CA-11825: broadcast needs to be determined for non-standard networks
bcast = self.getBroadcast()
f.write("BOOTPROTO=none\n")
f.write("IPADDR=%s\n" % self.ipaddr)
if bcast is not None:
f.write("BROADCAST=%s\n" % bcast)
f.write("NETMASK=%s\n" % self.netmask)
if self.gateway:
f.write("GATEWAY=%s\n" % self.gateway)
if self.vlan:
f.write("VLAN=yes\n")
f.close()
def waitUntilUp(self, iface):
if not self.isStatic():
return True
if not self.gateway:
return True
rc = util.runCmd2(['/usr/sbin/arping', '-f', '-w', '120', '-I',
self.getInterfaceName(iface), self.gateway])
return rc == 0
@staticmethod
def getModeStr(mode):
if mode == NetInterface.Static:
return 'static'
if mode == NetInterface.DHCP:
return 'dhcp'
if mode == NetInterface.Autoconf:
return 'autoconf'
return 'none'