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 tftp)
19 \t-s | --server: IP address of tftp server
20 \t-w | --write: initiate loading of redboot (default no modification to flash)
21 \t-v | --verbose: display additional information
22 \t-V | --version: display version information
35 import SimpleHTTPServer
41 #password = getpass.getpass()
42 password = "password1"
44 imagefile = "redboot.bin"
55 httpd = SocketServer.TCPServer((server,PORT),SimpleHTTPServer.SimpleHTTPRequestHandler)
56 thread.start_new_thread(httpd.serve_forever,())
61 tn.write("cat /proc/mtd\n")
63 buf = tn.read_until("Returned 0", 3)
67 flashsize = int(buf[i+6:].split()[0],16)
69 def image_dump(tn, dumpfile):
72 buf = tn.read_until("Returned 0")
73 i = buf.find("Platform:")
77 platform=buf[i+9:].split()[0]
79 tn.write("ifconfig br0\n");
80 buf = tn.read_until("Returned 0")
84 print "No MAC address found! (use -f option)"
86 dumpfile = "%s-%s.bin" % (platform, buf[i+4:i+21].replace(':',''))
90 print "Dumping flash contents (%dMB) to %s\n" % (flashsize/1048576, dumpfile)
91 f = open(dumpfile, "wb")
93 for addr in range(flashsize/dumplen):
98 tn.write("flash_dump -r 0x%x -l %d -4\n" % (addr*dumplen, dumplen))
103 buf = tn.read_until("\n")
104 if buf.strip() == "Returned 0":
107 if s and s[0][-1] == ':':
110 print "Format error: %x != %x"%(a,count)
113 f.write(binascii.a2b_hex(string.join(s[1:],'')))
118 def telnet_option(sock,cmd,option):
119 #print "Option: %d %d" % (ord(cmd), ord(option))
120 if cmd == telnetlib.DO:
122 elif cmd == telnetlib.WILL:
124 sock.sendall(telnetlib.IAC + c + option)
126 def telnet_timeout():
127 print "Fatal error: telnet timeout!"
131 print __doc__ % os.path.basename(sys.argv[0])
136 opts, args = getopt.getopt(sys.argv[1:], "hdf:u:p:P:s:vVw", \
137 ["help", "dump", "file=", "user=", "pass=", "proto=","server=", "verbose", "version", "write"])
138 except getopt.GetoptError:
139 # print help information and exit:
144 if o in ("-h", "--help"):
147 if o in ("-V", "--version"):
148 print "%s: 0.6" % sys.argv[0]
150 if o in ("-d", "--no-dump"):
152 if o in ("-f", "--file"):
154 if o in ("-s", "--server"):
156 if o in ("-u", "--user"):
158 if o in ("-p", "--pass"):
160 if o in ("-P", "--proto"):
162 if o in ("-w", "--write"):
164 if o in ("-v", "--verbose"):
167 # make sure we have enough arguments
175 # create a telnet session to the router
177 tn = telnetlib.Telnet(HOST)
178 except socket.error, msg:
179 print "Unable to establish telnet session to %s: %s" % (HOST, msg)
182 tn.set_option_negotiation_callback(telnet_option)
184 buf = tn.read_until("Username: ", 3)
189 buf = tn.read_until("Password: ", 3)
192 tn.write(password+"\n")
195 buf = tn.read_until("> ", 3)
202 image_dump(tn, dumpfile)
204 # write image file image
206 server = tn.get_socket().getsockname()[0]
208 cmd = "load -u %s://%s:%d/%s -r 0\n" % (proto, server, PORT, imagefile)
210 cmd = "load -u %s://%s/%s -r 0\n" % (proto, server, imagefile)
216 tn.write("unlock 0 0x%x\n" % flashsize)
217 buf = tn.read_until("Returned 0")
220 buf = tn.read_until("Returned 0")