def main(): global progname progname = sys.argv[0] host = def_host quiet = False try: opts, args = getopt.getopt(sys.argv[1:], "?qvh:", \ ["help", "quiet", "version", "host=" ]) except err: error(str(err)) for o, a in opts: if o in ("-?", "--help"): show_usage() sys.exit() elif o in ("-v", "--version"): show_version() sys.exit() elif o in ("-q", "--quiet"): quiet = True elif o in ("-h", "--host"): host = a else: assert False, "unhandled option" if len(args) > 0: error("too many arguments") tclient = tftp.TftpClient(host) if quiet: tgt_reset = tgt_reset_quiet else: tgt_reset = tgt_reset_noisy try: tclient.put(tgt_reset, 'script', tftp.TFTP_MODE_NETASCII) except Exception as err: error(err)
def main(): global progname progname = sys.argv[0] host = def_host try: opts, args = getopt.getopt(sys.argv[1:], "?vh:", \ ["help", "version", "host=" ]) except err: error(str(err)) for o, a in opts: if o in ("-?", "--help"): show_usage() sys.exit() elif o in ("-v", "--version"): show_version() sys.exit() elif o in ("-h", "--host"): host = a else: assert False, "unhandled option" if len(args) == 0: error("missing argument") script = '' for cmd in args: script += cmd + '\n' tclient = tftp.TftpClient(host) try: tclient.put(script, 'script', tftp.TFTP_MODE_NETASCII) except Exception as err: error(err)
def main(): global progname progname = sys.argv[0] host = def_host addr = def_addr reset = False nrst = False quiet = False init = False erase = False verbose = False power = False target = def_target try: opts, args = getopt.getopt(sys.argv[1:], "?rnqivVpea:h:t:", \ ["help", "reset", "nrst", "quiet", "init" , "verbose", "version", \ "power", "erase", \ "addr=", "host=", "target="]) except err: error(str(err)) for o, a in opts: if o in ("-?", "--help"): show_usage() sys.exit() elif o in ("-V", "--version"): show_version() sys.exit() elif o in ("-r", "--reset"): reset = True elif o in ("-n", "--nrst"): nrst = True reset = True elif o in ("-q", "--quiet"): quiet = True elif o in ("-i", "--init"): init = True elif o in ("-v", "--verbose"): verbose = True elif o in ("-e", "--erase"): erase = True elif o in ("-p", "--power"): power = True elif o in ("-a", "--addr"): addr = int(a, 0) elif o in ("-h", "--host"): host = a elif o in ("-t", "--target"): target = a else: assert False, "unhandled option" if len(args) == 0: error("you must specify a file to transfer") if len(args) > 1: error("too many arguments") in_fname = args[0] dirname, fname = os.path.split(in_fname) basename, extension = os.path.splitext(fname) fsize = 0 try: f = open(in_fname, mode='rb') bin_data = f.read() f.close() except Exception as err: error("can't open file: '%s'" % in_fname) fsize = len(bin_data) print(" - File: '{0}'".format(fname)) print(" - Size: {0:d} bytes".format(fsize)) print(" - Remote host: {0}".format(host)) print(" - Target: '{0}'".format(target)) print(" - Upload address: 0x{0:08x}".format(addr)) sys.stdout.flush() options = {} # options['blksize'] = 1428 options['blksize'] = 1024 + 128 + 64 timeout = 2 port = 69 tclient = tftp.TftpClient(host, port) if quiet: power_on = power_on_quiet if nrst: tgt_reset = tgt_nrst_quiet else: tgt_reset = tgt_reset_quiet else: power_on = power_on_noisy if nrst: tgt_reset = tgt_nrst_noisy else: tgt_reset = tgt_reset_noisy if power: try: print(" - Power on") sys.stdout.flush() script = power_on tclient.put(script, "script", tftp.TFTP_MODE_NETASCII) except Exception as err: script_error(err, script) if init: try: print(" - Configuring remote target") sys.stdout.flush() script = tgt_config.format(target) if verbose: print(" \"{0}\"".format(script)) tclient.put(script, "script", tftp.TFTP_MODE_NETASCII) except Exception as err: script_error(err, script) else: try: print(" - Connecting to remote target") sys.stdout.flush() script = "connect\n halt\n" if verbose: print(" \"{0}\"".format(script)) tclient.put(script, "script", tftp.TFTP_MODE_NETASCII) except Exception as err: script_error(err, script) if erase: try: print(" - Erasing 0x{0:08x} ({1:d} bytes)".format(addr, fsize)) sys.stdout.flush() script = "erase 0x{0:08x} {1:d}\n".format(addr, fsize) if verbose: print(" \"{0}\"".format(script)) tclient.put(script, 'script', tftp.TFTP_MODE_NETASCII, \ 4 + (fsize / 16384)) except Exception as err: script_error(err, script) # try: print(" - Loading binary file...") sys.stdout.flush() t0 = time.time() tclient.put(bin_data, "0x{0:08x}".format(addr), \ tftp.TFTP_MODE_OCTET, timeout, options) dt = time.time() - t0 if (dt > 0): print(" - {0:d} bytes transferred in {1:.2f} seconds"\ " ({2:.0f} bytes/sec)".format(fsize, dt, fsize/dt)) else: print(" - {0:d} bytes transferred".format(fsize)) sys.stdout.flush() # except Exception as err: # error(err) if reset: try: print(" - Reseting remote target") tclient.put(tgt_reset, 'script', tftp.TFTP_MODE_NETASCII) sys.stdout.flush() except Exception as err: script_error(err, script)
def main(): usage = "" parser = OptionParser(usage=usage) parser.add_option('-p', '--port', default=69, help='remote port to use (default: 69)') parser.add_option('-f', '--filename', help='remote filename') parser.add_option('-a', '--ascii', action='store_true', default=False, help='ASCII mode') parser.add_option('-b', '--blksize', help='udp packet size to use (default: 512)') parser.add_option('-s', '--tsize', action='store_true', default=False, help="ask client to send tsize option in download") parser.add_option('-t', '--timeout', default=1, help="option to set the server's timeout interval") (options, args) = parser.parse_args() if len(args) != 2: parser.error("wrong number of arguments") host = args[0] path = args[1] if options.filename: filename = options.filename else: filename = os.path.basename(path) if options.ascii: mode = tftp.TFTP_MODE_NETASCII else: mode = tftp.TFTP_MODE_OCTET port = int(options.port) timeout = int(options.timeout) tftp_options = {} if options.blksize: tftp_options['blksize'] = int(options.blksize) if options.tsize: tftp_options['tsize'] = 0 if not os.path.exists(path): print >> sys.stderr, "File not found: %s" % path return 1 try: f = open(path, "rb") data = f.read() f.close() except Exception as err: sys.stderr.write("%s\n" % str(err)) sys.exit(1) tclient = tftp.TftpClient(host, port) try: tclient.put(data, filename, mode, timeout, tftp_options) except Exception as err: sys.stderr.write("%s\n" % str(err)) sys.exit(1) except KeyboardInterrupt: pass