-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2.py
189 lines (138 loc) · 5.47 KB
/
p2.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import paramiko, base64
import time
import argparse
import telnetlib
import subprocess
import errno
from socket import error as socket_error
parser = argparse.ArgumentParser(description='prime carambola')
parser.add_argument('-c', '--configure', help='configure', action='store_true')
parser.add_argument('-u', '--update', help='upgrade', action='store_true')
parser.add_argument('-e', '--eth', help='ethernet', type=int, default=1)
parser.add_argument('-s', '--seatalk', help='seatalk', action='store_true')
parser.add_argument('-n', '--nmea0183', help='nmea0183', action='store_true')
parser.add_argument('-w', '--wifi', help='wifi', action='store_true')
parser.add_argument('-t', '--telnet', help='telnet', action='store_true')
parser.add_argument('--ssh', help='ssh', action='store_true')
parser.add_argument('--n2k', help='n2k', action='store_true')
args = parser.parse_args()
if args.configure:
print 'configure'
if args.eth:
print 'ethernet ' + str(args.eth)
if args.seatalk:
print 'seatalk'
def upgradeTelnet():
prompt = "root@carambola2:/# "
print 'Connecting ...'
tn = telnetlib.Telnet('192.168.1.1')
print 'Connected ...'
tn.write('uptime\r\n')
print tn.read_until(prompt)
tn.write('ls -l /tmp\r\n')
print tn.read_until(prompt)
tn.write("rm -f /tmp/openwrt-ar71xx-generic-carambola2-squashfs-sysupgrade.bin\r\n")
print tn.read_until(prompt)
tn.write("wget http://192.168.1.2/carambola2/openwrt-ar71xx-generic-carambola2-squashfs-sysupgrade.bin -P /tmp\r\n")
print tn.read_until(prompt)
tn.write('ls -l /tmp\r\n')
print tn.read_until(prompt)
tn.write('uptime\n')
print 'Starting upgrade ...'
tn.write("sysupgrade -v /tmp/openwrt-ar71xx-generic-carambola2-squashfs-sysupgrade.bin\r\n")
print tn.read_until("Saving config files...")
print tn.read_until("Sending TERM to remaining processes ...")
print tn.read_until("Performing system upgrade...")
print tn.read_until("Upgrade completed")
tn.write('\x1d')
tn.close()
print "done ..."
def upgrade():
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
if args.wifi:
client.connect('192.168.10.1', username='root', password='admin')
else:
client.connect('192.168.1.1', username='root', password='admin')
stdin, stdout, stderr = client.exec_command('uptime')
print stdout.read()
# tn.write("rm -f /tmp/openwrt-ar71xx-generic-carambola2-squashfs-sysupgrade.bin\r\n")
print "error"
stdin, stdout, stderr = "" # client.exec_command('rm -f /tmp/openwrt-ramips-rt305x-carambola-squashfs-sysupgrade.bin')
print stdout.read()
stdin, stdout, stderr = ""
# stdin, stdout, stderr = client.exec_command('wget http://192.168.1.2/carambola/openwrt-ramips-rt305x-carambola-squashfs-sysupgrade.bin -P /tmp')
print stdout.read()
# stdin, stdout, stderr = client.exec_command('sysupgrade -v /tmp/openwrt-ramips-rt305x-carambola-squashfs-sysupgrade.bin')
stdin, stdout, stderr = ""
# Wait for the command to terminate
rebooting = 0
while not stdout.channel.exit_status_ready() or rebooting:
# Only print data if there is data to read in the channel
if stdout.channel.recv_ready():
buffer = stdout.channel.recv(1024)
print buffer
if "Upgrade completed" in buffer:
print "Upgrade completed!!!"
rebooting = 1
break
finally:
print "closing connection"
client.close()
def configure():
cmd = 'cd /www && lua /usr/sbin/vyacht-init-setup.lua --eth=1'
if args.eth == 0:
cmd = 'cd /www && lua /usr/sbin/vyacht-init-setup.lua --eth=0'
if(args.eth == 2):
cmd = 'cd /www && lua /usr/sbin/vyacht-init-setup.lua --eth=2'
if args.n2k:
cmd = cmd + " --n2k";
if args.seatalk:
cmd = cmd + " --seatalk";
if args.nmea0183:
cmd = cmd + " --nmea0183";
elif args.seatalk:
cmd = cmd + " --seatalk";
print cmd
retry = 0
while retry < 10:
try:
print 'trying to connect'
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
if args.wifi:
client.connect('192.168.10.1', username='root', password='admin')
else:
client.connect('192.168.1.1', username='root', password='admin')
print 'connected'
stdin, stdout, stderr = client.exec_command(cmd)
print stdout.read()
break
except socket_error as serr:
print 'exception caught ' + `serr.errno`
#if serr.errno != errno.ECONNREFUSED:
# raise serr
time.sleep(10)
except:
time.sleep(10)
print 'exception caught'
finally:
client.close()
retry += 1
if args.update:
if args.ssh:
subprocess.call(['/usr/bin/ssh-keygen', '-f',
'/home/bo/.ssh/known_hosts', '-R', '192.168.1.1'])
upgrade()
else:
upgradeTelnet()
print '... end'
print 'waiting for reboot'
if args.update and args.configure:
time.sleep(60)
if args.configure:
subprocess.call(['/usr/bin/ssh-keygen', '-f',
'/home/bo/.ssh/known_hosts', '-R', '192.168.1.1'])
configure()
print 'DONE!'