Skip to content

Commit 621e3fa

Browse files
Nambrokbenjamreis
andcommitted
Add IPv6 support to netmask and gateway fields
Co-authored-by: BenjiReis <[email protected]> Signed-off-by: Damien Thenot <[email protected]>
1 parent a8e86a7 commit 621e3fa

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

XSConsoleData.py

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -980,21 +980,58 @@ def ManagementIP(self, inDefault = None):
980980

981981
def ManagementNetmask(self, inDefault = None):
982982
retVal = inDefault
983-
984983
for pif in self.derived.managementpifs([]):
985-
retVal = pif['netmask']
986-
if retVal:
987-
break
984+
ipv6 = pif['primary_address_type'].lower() == 'ipv6'
985+
configuration_mode = pif['ipv6_configuration_mode'] if ipv6 else pif['ip_configuration_mode']
986+
if configuration_mode.lower().startswith('static'):
987+
# For static IP the API address is correct
988+
retVal = pif['IPv6'][0].split('/')[1] if ipv6 else pif['netmask']
989+
elif configuration_mode.lower().startswith('dhcp'):
990+
# For DHCP, find the gateway address by parsing the output from the 'route' command
991+
if 'bridge' in pif['network']:
992+
device = pif['network']['bridge']
993+
else:
994+
device = pif['device']
995+
996+
device = ShellUtils.MakeSafeParam(device)
997+
998+
ipre = r'[0-9a-f.:]+'
999+
ifRE = re.compile(r'\s*inet\s+' + ipre + '\s+netmask\s+(' + ipre + r')\s+broadcast\s+(' + ipre + r')\s*$',
1000+
re.IGNORECASE)
1001+
1002+
ifconfig = commands.getoutput("/sbin/ifconfig '"+device+"'").split("\n")
1003+
for line in ifconfig:
1004+
match = ifRE.match(line)
1005+
if match:
1006+
retVal = match.group(1)
1007+
break
9881008

9891009
return retVal
9901010

9911011
def ManagementGateway(self, inDefault = None):
9921012
retVal = inDefault
9931013

9941014
for pif in self.derived.managementpifs([]):
995-
retVal = pif['gateway']
996-
if retVal:
997-
break
1015+
ipv6 = pif['primary_address_type'].lower() == 'ipv6'
1016+
configuration_mode = pif['ipv6_configuration_mode'] if ipv6 else pif['ip_configuration_mode']
1017+
if configuration_mode.lower().startswith('static'):
1018+
# For static IP the API address is correct
1019+
retVal = pif['ipv6_gateway'] if ipv6 else pif['gateway']
1020+
elif configuration_mode.lower().startswith('dhcp'):
1021+
# For DHCP, find the gateway address by parsing the output from the 'route' command
1022+
if 'bridge' in pif['network']:
1023+
device = pif['network']['bridge']
1024+
else:
1025+
device = pif['device']
1026+
routeRE = re.compile(r'([0-9.]+)\s+([0-9.]+)\s+([0-9.]+)\s+UG\s+\d+\s+\d+\s+\d+\s+'+device,
1027+
re.IGNORECASE)
1028+
1029+
routes = commands.getoutput("/sbin/route -n").split("\n")
1030+
for line in routes:
1031+
match = routeRE.match(line)
1032+
if match:
1033+
retVal = match.group(2)
1034+
break
9981035

9991036
return retVal
10001037

0 commit comments

Comments
 (0)