#!/usr/bin/env python # Copyright (c) 2004 James Kretchmar. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in # the documentation and/or other materials provided with the # distribution. # # * Redistributions in any form must be accompanied by information on # how to obtain complete source code for this software and any # accompanying software that uses this software. The source code # must either be included in the distribution or be available for no # more than the cost of distribution plus a nominal fee, and must be # freely redistributable under reasonable conditions. For an # executable file, complete source code means the source code for # all modules it contains. It does not include source code for # modules or files that typically accompany the major components of # the operating system on which the executable file runs. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR # NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import sys from ftplib import FTP version="1.0" def usage(): """Print program usage info""" print "ftpshove version " + version print "" print "usage: ftpshove user@host[:path] ..." def getpass(prompt = "Password: "): """Retrieve a password without echoing to the terminal""" import termios, sys fd = sys.stdin.fileno() old = termios.tcgetattr(fd) new = termios.tcgetattr(fd) new[3] = new[3] & ~termios.ECHO try: termios.tcsetattr(fd, termios.TCSADRAIN, new) passwd = raw_input(prompt) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old) print return passwd ## argument processing args=sys.argv if (len(args)<3): usage() sys.exit(1) ## user@host firstarg=args[1] i=firstarg.find('@') if i == -1 : usage() sys.exit(1) user=firstarg[:i] rest=firstarg[i+1:] i=rest.find(':') if i == -1: host=rest path="" else: host=rest[:i] path=rest[i+1:] ## everything else is files to upload args.pop(0) args.pop(0) filenames=args ## prompt for the password password = getpass() print "Connecting to " + host + " as " + user s=FTP(host) try: s.login(user, password) except: print "Failed login" sys.exit(2) try: if path != "": s.cwd(path) except: print "Unable to change to directory '" + path + "' on server" sys.exit(2) for filename in filenames: print 'Uploading ' + filename + '....', f=open(filename, 'r') s.storlines('STOR ' + filename, f) f.close() print 'done.' print 'Session complete' s.close()