-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcisco43calc.py
68 lines (62 loc) · 2.18 KB
/
cisco43calc.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
import sys
import ipaddress
from ipaddress import AddressValueError
import socket
# Function to validate and convert IP addresses to hexadecimal.
def ip_to_hex(wlc_ip):
try:
wlc_ip = ipaddress.IPv4Address(wlc_ip)
wlc_ip = str(wlc_ip)
wlc_ip = socket.inet_aton(wlc_ip).hex()
return wlc_ip
except AddressValueError:
return None
# Function to print the list of IP addresses in the required format.
def print_hex_ips(iplist):
hexlist = ''.join(iplist)
hexcount = '{:02X}'.format(int(hex(len(iplist)*4), 16))
print ('\n' + 'Your DHCP Option 43 value is: ' + 'f1' + hexcount + hexlist)
# Function to handle case when script is executed with command line arguments.
def arg_mode(ip_args):
iplist = []
for wlc_ip in ip_args:
hex_ip = ip_to_hex(wlc_ip)
if hex_ip is None:
print('Error: One or more specified arguments are not Valid IPv4 Addresses')
return
if hex_ip not in iplist:
iplist.append(hex_ip)
print_hex_ips(iplist)
# Function to handle case when script is executed without command line arguments.
def interactive_mode(user_input):
count = 0
iplist = []
while count < user_input:
count += 1
wlc_ip = input('WLC #' + str(count) + ' IP Address: ')
hex_ip = ip_to_hex(wlc_ip)
if hex_ip is None:
print ('Not a valid IP Address. Try again.')
count -= 1
elif hex_ip in iplist:
print ('IP Address already entered. Try again.')
count -= 1
else:
iplist.append(hex_ip)
print_hex_ips(iplist)
# Main function to handle the overall logic.
def main():
if len(sys.argv) > 1:
arg_mode(sys.argv[1::])
else:
try:
user_input = int(input('\n' + 'Number of WLCs in network: '))
if 0 < user_input < 17:
interactive_mode(user_input)
else:
print ('Please enter a number between 1 and 16.')
except ValueError:
print ('Not a valid number. Try again.')
# Execution starts here.
if __name__ == "__main__":
main()