2006-10-04 02:43:05 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2006-10-09 02:44:27 +00:00
|
|
|
import sys, logging, os
|
2006-10-04 02:43:05 +00:00
|
|
|
from optparse import OptionParser
|
2006-12-14 02:29:46 +00:00
|
|
|
import tftpy
|
2006-10-04 02:43:05 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
usage=""
|
|
|
|
parser = OptionParser(usage=usage)
|
|
|
|
parser.add_option('-H',
|
|
|
|
'--host',
|
|
|
|
help='remote host or ip address')
|
|
|
|
parser.add_option('-p',
|
|
|
|
'--port',
|
|
|
|
help='remote port to use (default: 69)',
|
|
|
|
default=69)
|
|
|
|
parser.add_option('-f',
|
|
|
|
'--filename',
|
|
|
|
help='filename to fetch')
|
2008-07-30 18:04:01 +00:00
|
|
|
parser.add_option('-u',
|
|
|
|
'--upload',
|
|
|
|
help='filename to upload')
|
2006-10-04 02:43:05 +00:00
|
|
|
parser.add_option('-b',
|
|
|
|
'--blocksize',
|
|
|
|
help='udp packet size to use (default: 512)',
|
|
|
|
default=512)
|
|
|
|
parser.add_option('-o',
|
|
|
|
'--output',
|
2006-10-09 02:44:27 +00:00
|
|
|
help='output file (default: same as requested filename)')
|
2008-07-30 18:04:01 +00:00
|
|
|
parser.add_option('-i',
|
|
|
|
'--input',
|
|
|
|
help='input file (default: same as upload filename)')
|
2006-12-09 21:42:02 +00:00
|
|
|
parser.add_option('-d',
|
|
|
|
'--debug',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='upgrade logging from info to debug')
|
2006-12-15 03:04:40 +00:00
|
|
|
parser.add_option('-q',
|
|
|
|
'--quiet',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help="downgrade logging from info to warning")
|
2008-10-05 00:42:27 +00:00
|
|
|
parser.add_option('-t',
|
|
|
|
'--tsize',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help="ask client to send tsize option in download")
|
2006-10-04 02:43:05 +00:00
|
|
|
options, args = parser.parse_args()
|
2008-07-30 18:04:01 +00:00
|
|
|
if not options.host or (not options.filename and not options.upload):
|
2006-12-15 03:04:40 +00:00
|
|
|
sys.stderr.write("Both the --host and --filename options "
|
|
|
|
"are required.\n")
|
|
|
|
parser.print_help()
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if options.debug and options.quiet:
|
|
|
|
sys.stderr.write("The --debug and --quiet options are "
|
|
|
|
"mutually exclusive.\n")
|
2006-10-04 02:43:05 +00:00
|
|
|
parser.print_help()
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
class Progress(object):
|
|
|
|
def __init__(self, out):
|
|
|
|
self.progress = 0
|
|
|
|
self.out = out
|
|
|
|
def progresshook(self, pkt):
|
2008-10-05 00:42:27 +00:00
|
|
|
if isinstance(pkt, tftpy.TftpPacketDAT):
|
|
|
|
self.progress += len(pkt.data)
|
2009-04-11 01:59:25 +00:00
|
|
|
self.out("Transferred %d bytes" % self.progress)
|
2008-10-05 00:42:27 +00:00
|
|
|
elif isinstance(pkt, tftpy.TftpPacketOACK):
|
|
|
|
self.out("Received OACK, options are: %s" % pkt.options)
|
2006-10-05 03:38:28 +00:00
|
|
|
|
2006-12-09 21:42:02 +00:00
|
|
|
if options.debug:
|
|
|
|
tftpy.setLogLevel(logging.DEBUG)
|
2006-12-15 03:04:40 +00:00
|
|
|
elif options.quiet:
|
|
|
|
tftpy.setLogLevel(logging.WARNING)
|
2006-12-09 21:42:02 +00:00
|
|
|
else:
|
|
|
|
tftpy.setLogLevel(logging.INFO)
|
2006-10-04 02:43:05 +00:00
|
|
|
|
2006-10-05 03:38:28 +00:00
|
|
|
progresshook = Progress(tftpy.logger.info).progresshook
|
|
|
|
|
|
|
|
tftp_options = {}
|
|
|
|
if options.blocksize:
|
|
|
|
tftp_options['blksize'] = int(options.blocksize)
|
2008-10-05 00:42:27 +00:00
|
|
|
if options.tsize:
|
2008-10-09 01:31:34 +00:00
|
|
|
tftp_options['tsize'] = 0
|
2006-10-05 03:38:28 +00:00
|
|
|
|
|
|
|
tclient = tftpy.TftpClient(options.host,
|
2006-10-05 16:38:33 +00:00
|
|
|
int(options.port),
|
2006-10-05 03:38:28 +00:00
|
|
|
tftp_options)
|
2009-04-11 02:48:51 +00:00
|
|
|
try:
|
|
|
|
if options.filename:
|
|
|
|
if not options.output:
|
|
|
|
options.output = os.path.basename(options.filename)
|
|
|
|
tclient.download(options.filename,
|
|
|
|
options.output,
|
|
|
|
progresshook)
|
|
|
|
elif options.upload:
|
|
|
|
if not options.input:
|
|
|
|
options.input = os.path.basename(options.upload)
|
|
|
|
tclient.upload(options.upload,
|
|
|
|
options.input,
|
|
|
|
progresshook)
|
|
|
|
except tftpy.TftpException, err:
|
|
|
|
sys.stderr.write("%s\n" % str(err))
|
|
|
|
sys.exit(1)
|
2006-10-04 02:43:05 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|