-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathdhcp43.py
executable file
·110 lines (87 loc) · 4.45 KB
/
dhcp43.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
#!/usr/bin/env python
# usage: dhcp43.py [-h] [-s SERVER_ADDRESS] [-v VLAN_NAME] files [files ...]
#
# This script will generate the hex needed to configure EXOS's built-in DHCP
# server with option 43 for ZTP. It will also either provide the command to use,
# or configure the option on the specified VLAN.
#
# positional arguments:
# files Files to be downloaded. If the '-s' option is used,
# this may be simply be a file name. If the '-s' option
# is not used, this should be a full URL. (IE,
# tftp://192.168.1.10/config.xsf)
#
# optional arguments:
# -h, --help show this help message and exit
# -s SERVER_ADDRESS, --server_address SERVER_ADDRESS
# IP Address of TFTP server for sub-option 100. May be
# omitted if a URL is used for sub-option 101.
# -v VLAN_NAME, --vlan_name VLAN_NAME
# VLAN to configure option 43 on. If this is included,
# the option 43 config will be added to the DHCP server
# configuration on this switch for this VLAN. If not,
# the config command will simply be printed.
import binascii
import argparse
import sys
try:
from exsh import clicmd
env_is_exos = True
except ImportError:
env_is_exos = False
pass
class ArgParser(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write('error: %s\n' % message)
self.print_help()
sys.exit(2)
def main():
parser = ArgParser(prog='dhcp43.py',
description="This script will generate the hex needed to configure EXOS's built-in DHCP server with option 43 for ZTP. "
"It will also either provide the command to use, or configure the option on the specified VLAN.",
usage="%(prog)s [-h] [-s SERVER_ADDRESS] [-v VLAN_NAME] files")
parser.add_argument('-s', '--server-address',
help='IP Address of TFTP server for sub-option 100. May be omitted if a URL is used for sub-option 101.',
type=str, default="")
parser.add_argument('-v', '--vlan-name',
help='VLAN to configure option 43 on. If this is included, the option 43 config will be added to the DHCP '
'server configuration on this switch for this VLAN. If not, the config command will simply be printed.',
type=str, default="<vlan_name>")
parser.add_argument('files',
help='File(s) to be downloaded. If the \'-s\' option is used, this may be simply be a file name. '
'If multiple files are given, they should be separated by spaces. '
'If the \'-s\' option is not used, this should be a full URL. (IE, tftp://192.168.1.10/config.xsf)',
type=str, nargs='+')
args = parser.parse_args()
vlan = args.vlan_name
server = args.server_address
files = args.files
# Convert the IP address to hex, if it exists
hex = []
if len(server):
ip = server.split('.')
hex.append(binascii.hexlify(chr(100))) #sub-option 100
hex.append(binascii.hexlify(chr(4))) #length = 4 bytes
for i in ip: #convert each byte of the IP to hex
hex.append(binascii.hexlify(chr(int(i))))
# Convert the filenames/URLs to hex
for i in files:
hex.append(binascii.hexlify(chr(101))) #sub-option 101
hex.append(binascii.hexlify(chr(len(i)))) #length of filename/URL in bytes
hex.append(binascii.hexlify(i)) #filename converted to hex
#Generate the command needed to configure this:
hex = ''.join(hex) #join all the hex into one string
hex = ':'.join(hex[i:i+2] for i in range(0, len(hex), 2)) #delimit the bytes with ':', like EXOS expects
#Create the command
cmd = 'configure vlan {0} dhcp-options code 43 hex {1}'.format(vlan, hex)
#execute it if running on EXOS and a VLAN name was given, otherwise print it
if env_is_exos and vlan is not "<vlan_name>":
clicmd(cmd)
else:
print cmd
if __name__ == '__main__':
try:
main()
except SystemExit:
#catch SystemExit to prevent EXOS shell from exiting to login prompt
pass