-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig_script
executable file
·143 lines (120 loc) · 3.15 KB
/
config_script
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
#!/usr/bin/env python3
# Usage:
# ./config_script [Switch IP] [new IP] [new netmask] [Switch name] [Switch location] [new password]
import sys
import os
import pexpect
import time
firmware_filepath = '/home/cbd/Downloads/PT.1.14.stk'
config_filepath = '/home/cbd/Downloads/hp1820_8G.cfg'
old_password = ''
switch_ip = ''
username = 'admin'
is_debugging = False
def print_ret(child):
if is_debugging:
print('Get: %s %s' % (child.before.decode('UTF-8'), child.after.decode('UTF-8')))
def login():
child = pexpect.spawn('./sshhp ' + username + '@' + switch_ip)
child.expect('Password: ')
print_ret(child)
child.sendline(old_password)
print("login success")
return child
def update_firmware():
child = login()
# update version PT.1.14
child.sendline('uploadcode')
child.expect('Code file location')
child.sendline(firmware_filepath)
child.expect('Upload succeeded', 3600)
print_ret(child)
child.sendline('exit')
# login again
time.sleep(30)
child = login()
# login again
child.sendline('exit')
time.sleep(30)
child = login()
# login again
child.sendline('exit')
time.sleep(30)
child = login()
child.sendline('activatecode')
child.expect('Switch is rebooting')
print_ret(child)
# time.sleep(90)
def gencert(child):
child.sendline('gencert')
child.expect('Generating a new cert')
child.expect('#', 120)
def uploadconfig(child):
child.sendline('uploadconfig')
child.expect('config file location')
child.sendline(config_filepath)
child.expect('#')
def setinfo(child, name, location):
child.sendline('setinfo')
child.expect('Switch Name: ')
child.sendline(name)
child.expect('Location: ')
child.sendline(location)
child.expect('Contact: ')
child.sendline('')
child.expect('#')
def setaccount(child, username, oldpass, newpass):
child.sendline('setaccount')
child.expect('New username: ')
child.sendline(username)
child.expect('Current password: ')
child.sendline(oldpass)
child.expect('New password: ')
child.sendline(newpass)
child.expect('Retype new password: ')
child.sendline(newpass)
child.expect('#')
def sethttps(child):
child.sendline('sethttps')
child.expect('https only')
child.sendline('https')
# child.expect('#')
def setnetwork(child, manage_vlan, ip, netmask):
child.sendline('setnetwork')
child.expect('management vlan id')
child.sendline(manage_vlan)
child.expect('dhcp or static?')
child.sendline('static')
child.expect('IP: ')
child.sendline(ip)
child.expect('subnet mask: ')
child.sendline(netmask)
child.expect('gateway address: ')
child.sendline('')
# child.expect('#', 120)
if __name__ == "__main__":
switch_ip = sys.argv[1]
update_firmware()
child = login()
gencert(child)
print("gencert OK")
uploadconfig(child)
print("uploadconfig OK")
setinfo(child, sys.argv[4], sys.argv[5])
print("setinfo OK")
setaccount(child, sys.argv[5], old_password, sys.argv[6])
print("setaccount OK")
sethttps(child)
print("sethttps OK")
time.sleep(15)
username = sys.argv[5]
old_password = sys.argv[6]
child = login()
setnetwork(child, '99', sys.argv[2], sys.argv[3])
print("setnetwork OK")
time.sleep(15)
switch_ip = sys.argv[2]
child = login()
child.sendline('write')
child.expect('#')
child.sendline('exit')