Skip to content

Commit 40b433e

Browse files
authored
Update updateip.py
use ip_interface() instead of ip_address() where appropriate
1 parent 8109c12 commit 40b433e

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

Diff for: restconf_update_ipaddress/updateip.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- updates the IP address on the interface
99
- displays the final IP information on the interface
1010
11-
This script has been tested with Python 3.5, however may work with other versions.
11+
This script has been tested with Python 3.7, however may work with other versions.
1212
1313
This script targets the RESTCONF DevNet Sandbox that leverages a CSR1000v as
1414
a target. To execute this script against a different device, update the
@@ -120,7 +120,7 @@ def print_interface_details(url_base, interface, username, password, cidr):
120120
netmask = intf[0]["ietf-ip:ipv4"]["address"][0]["netmask"]
121121
if cidr:
122122
nma = ipaddress.ip_address(netmask)
123-
netmask = str("{0:b}".format(nma._ip).count('1'))
123+
netmask = str("{0:b}".format(int(nma)).count('1'))
124124
print("IP Address: ", intf[0]["ietf-ip:ipv4"]["address"][0]["ip"], "/",
125125
netmask)
126126
except KeyError:
@@ -156,14 +156,17 @@ def get_ip_info(cidr):
156156
ip = {}
157157
try:
158158
if cidr:
159-
(ipa_t, ipl) = input("What IP address/prefixlen do you want to set? ").split('/')
160-
ipa = ipaddress.ip_address(ipa_t)
161-
ip["address"] = ipa.compressed
162-
ip["mask"] = ipa._make_netmask(int(ipl))[0].compressed
159+
ipa_t = input("What IP address/prefixlen do you want to set? ")
160+
ipi = ipaddress.ip_interface(ipa_t)
161+
ip["address"] = ipi.ip.compressed
162+
ip["mask"] = ipi.netmask.compressed
163163
else:
164164
ipa_t = input("What IP address do you want to set? ")
165-
ip["address"] = ipaddress.ip_address(ipa_t).compressed
166-
ip["mask"] = input("What Subnet Mask do you want to set? ")
165+
ipi = ipaddress.ip_interface(ipa_t)
166+
ip["address"] = ipi.ip.compressed
167+
ipm_t = input("What Subnet Mask do you want to set? ")
168+
ipm = ipaddress.ip_address(ipm_t)
169+
ip["mask"] = ipm.compressed
167170
except Exception as e:
168171
print(e, file=sys.stderr)
169172
sys.exit(1)

0 commit comments

Comments
 (0)