3 # Copyright 2008, 2009 (C) Jose Vasconcellos <jvasco@verizon.net>
5 # A script that can communicate with jungo-based routers
6 # (such as MI424-WR, USR8200 and WRV54G) to backup the installed
7 # firmware and replace the boot loader.
9 # Tested with Python 2.5 on Linux and Windows
11 """Usage: %s [options] <IP_address> [image.bin | url]
13 \t-h | --help: usage statement
14 \t-d | --dump: create a flash dump
15 \t-f | --file: use <filename> to store dump contents
16 \t-u | --user: provide username (default admin)
17 \t-p | --pass: provide password (default password1)
18 \t --port: set port for http (default 8080)
19 \t-q | --quiet: don't display unnecessary information
20 \t-r | --reboot: reboot target on successful transfer
21 \t-V | --version: display version information
23 If no image (or url) is given, a flash dump is created.
24 A built-in http server is used when an image file is provided.
37 import SimpleHTTPServer
43 #password = getpass.getpass()
44 password
= "password1"
58 def start_server(server
):
59 httpd
= SocketServer
.TCPServer((server
,PORT
),SimpleHTTPServer
.SimpleHTTPRequestHandler
)
60 thread
.start_new_thread(httpd
.serve_forever
,())
65 tn
.write("cat /proc/mtd\n")
67 buf
= tn
.read_until("Returned 0", 3)
71 return int(buf
[i
+6:].split()[0],16)
72 # use different command
73 tn
.write("flash_layout\n")
74 buf
= tn
.read_until("Returned 0", 3)
75 i
= buf
.rfind('Range ')
77 return int(buf
[i
+17:].split()[0],16)
78 print "Can't determine flash size!"
80 print "Unable to obtain flash size!"
83 def image_dump(tn
, dumpfile
):
86 buf
= tn
.read_until("Returned 0",2)
87 i
= buf
.find("Platform:")
93 platform
=line
[:i
].split()[-1]
95 tn
.write("rg_conf_print /dev/%s/mac\n" % device
);
96 buf
= tn
.read_until("Returned 0",3)
102 print "No MAC address found! (use -f option)"
104 dumpfile
= "%s-%s.bin" % (platform
, buf
[i
:i
+17].replace(':',''))
108 print "Dumping flash contents (%dMB) to %s" % (flashsize
/1048576, dumpfile
)
109 f
= open(dumpfile
, "wb")
112 for addr
in range(t
):
114 sys
.stdout
.write('\r%d%%'%(100*addr
/t
))
117 tn
.write("flash_dump -r 0x%x -l %d -4\n" % (addr
*dumplen
, dumplen
))
122 buf
= tn
.read_until("\n")
123 if buf
.strip() == "Returned 0":
126 if s
and s
[0][-1] == ':':
129 print "Format error: %x != %x"%(a
,count
)
132 f
.write(binascii
.a2b_hex(string
.join(s
[1:],'')))
139 def telnet_option(sock
,cmd
,option
):
140 #print "Option: %d %d" % (ord(cmd), ord(option))
141 if cmd
== telnetlib
.DO
:
143 elif cmd
== telnetlib
.WILL
:
145 sock
.sendall(telnetlib
.IAC
+ c
+ option
)
147 def telnet_timeout():
148 print "Fatal error: telnet timeout!"
152 print __doc__
% os
.path
.basename(sys
.argv
[0])
157 opts
, args
= getopt
.getopt(sys
.argv
[1:], "hdf:qp:P:rvV", \
158 ["help", "dump", "file=", "user=", "pass=", "port=",
159 "quiet=", "reboot", "verbose", "version"])
160 except getopt
.GetoptError
:
161 # print help information and exit:
166 if o
in ("-h", "--help"):
169 elif o
in ("-V", "--version"):
170 print "%s: 0.10" % sys
.argv
[0]
172 elif o
in ("-d", "--no-dump"):
174 elif o
in ("-f", "--file"):
176 elif o
in ("-u", "--user"):
178 elif o
in ("-p", "--pass"):
182 elif o
in ("-q", "--quiet"):
184 elif o
in ("-r", "--reboot"):
186 elif o
in ("-v", "--verbose"):
189 # make sure we have enough arguments
194 if args
[1].split(':')[0] in ("tftp", "http", "ftp"):
202 # create a telnet session to the router
204 tn
= telnetlib
.Telnet(HOST
)
205 except socket
.error
, msg
:
206 print "Unable to establish telnet session to %s: %s" % (HOST
, msg
)
209 tn
.set_option_negotiation_callback(telnet_option
)
211 buf
= tn
.read_until("Username: ", 3)
216 buf
= tn
.read_until("Password: ", 3)
219 tn
.write(password
+"\n")
222 buf
= tn
.read_until("> ", 3)
226 flashsize
= get_flash_size()
229 image_dump(tn
, dumpfile
)
232 splitpath
= os
.path
.split(imagefile
)
234 # create load command
236 cmd
= "load -u %s -r 0\n" % (url
)
238 server
= tn
.get_socket().getsockname()[0]
239 cmd
= "load -u http://%s:%d/%s -r 0\n" % (server
, PORT
, splitpath
[1])
241 if not os
.access(imagefile
, os
.R_OK
):
242 print "File access error: %s" % (imagefile
)
245 # make sure we're in the directory where the image is located
247 os
.chdir(splitpath
[0])
252 print "Unlocking flash..."
253 tn
.write("unlock 0 0x%x\n" % flashsize
)
254 buf
= tn
.read_until("Returned 0",5)
257 print "Writing new image..."
260 buf
= tn
.read_until("Returned 0",10)
262 # wait till the transfer completed
263 buf
= tn
.read_until("Download completed successfully",20)
265 print "Flash update complete!"
This page took 0.060862 seconds and 5 git commands to generate.