3 # Copyright 2008 (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> [redboot.bin]
13 \t-h | --help: usage statement
14 \t-d | --no-dump: don't 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-P | --proto: set transfer protocol (default http)
19 \t --port: set port for http (default 8080)
20 \t-s | --server: IP address of tftp server
21 \t-w | --write: initiate loading of redboot (default no modification to flash)
22 \t-q | --quiet: don't display unnecessary information
23 \t-v | --verbose: display progress information
24 \t-V | --version: display version information
37 import SimpleHTTPServer
43 #password = getpass.getpass()
44 password = "password1"
46 imagefile = "redboot.bin"
59 httpd = SocketServer.TCPServer((server,PORT),SimpleHTTPServer.SimpleHTTPRequestHandler)
60 thread.start_new_thread(httpd.serve_forever,())
66 tn.write("cat /proc/mtd\n")
68 buf = tn.read_until("Returned 0", 3)
72 flashsize = int(buf[i+6:].split()[0],16)
74 print "Can't find mtd0!"
76 print "Can't access /proc/mtd!"
78 def image_dump(tn, dumpfile):
81 buf = tn.read_until("Returned 0")
82 i = buf.find("Platform:")
88 platform=line[:i].split()[-1]
90 tn.write("ifconfig -v %s\n" % device);
91 buf = tn.read_until("Returned 0")
93 i = buf.find("mac = 0")
97 print "No MAC address found! (use -f option)"
99 dumpfile = "%s-%s.bin" % (platform, buf[i:i+17].replace(':',''))
103 print "Dumping flash contents (%dMB) to %s" % (flashsize/1048576, dumpfile)
104 f = open(dumpfile, "wb")
107 for addr in range(t):
109 sys.stdout.write('\r%d%%'%(100*addr/t))
112 tn.write("flash_dump -r 0x%x -l %d -4\n" % (addr*dumplen, dumplen))
117 buf = tn.read_until("\n")
118 if buf.strip() == "Returned 0":
121 if s and s[0][-1] == ':':
124 print "Format error: %x != %x"%(a,count)
127 f.write(binascii.a2b_hex(string.join(s[1:],'')))
134 def telnet_option(sock,cmd,option):
135 #print "Option: %d %d" % (ord(cmd), ord(option))
136 if cmd == telnetlib.DO:
138 elif cmd == telnetlib.WILL:
140 sock.sendall(telnetlib.IAC + c + option)
142 def telnet_timeout():
143 print "Fatal error: telnet timeout!"
147 print __doc__ % os.path.basename(sys.argv[0])
152 opts, args = getopt.getopt(sys.argv[1:], "hdf:u:qp:P:s:vVw", \
153 ["help", "dump", "file=", "user=", "pass=", "proto=", "proto=",
154 "quiet=", "server=", "verbose", "version", "write"])
155 except getopt.GetoptError:
156 # print help information and exit:
161 if o in ("-h", "--help"):
164 if o in ("-V", "--version"):
165 print "%s: 0.7" % sys.argv[0]
167 if o in ("-d", "--no-dump"):
169 if o in ("-f", "--file"):
171 if o in ("-s", "--server"):
173 if o in ("-u", "--user"):
175 if o in ("-p", "--pass"):
177 if o in ("-P", "--proto"):
181 if o in ("-w", "--write"):
183 if o in ("-q", "--quiet"):
185 if o in ("-v", "--verbose"):
188 # make sure we have enough arguments
196 # create a telnet session to the router
198 tn = telnetlib.Telnet(HOST)
199 except socket.error, msg:
200 print "Unable to establish telnet session to %s: %s" % (HOST, msg)
203 tn.set_option_negotiation_callback(telnet_option)
205 buf = tn.read_until("Username: ", 3)
210 buf = tn.read_until("Password: ", 3)
213 tn.write(password+"\n")
216 buf = tn.read_until("> ", 3)
223 image_dump(tn, dumpfile)
226 if not os.access(imagefile, os.R_OK):
227 print "File access error: %s" % (imagefile)
230 splitpath = os.path.split(imagefile)
231 # make sure we're in the directory where the image is located
233 os.chdir(splitpath[0])
235 # write image file image
237 server = tn.get_socket().getsockname()[0]
239 cmd = "load -u %s://%s:%d/%s -r 0\n" % (proto, server, PORT, splitpath[1])
241 cmd = "load -u %s://%s/%s -r 0\n" % (proto, server, splitpath[1])
247 print "Unlocking flash..."
248 tn.write("unlock 0 0x%x\n" % flashsize)
249 buf = tn.read_until("Returned 0")
252 print "Writing new image..."
255 buf = tn.read_until("Returned 0")