diff --git a/tsshbatch.py b/tsshbatch.py index fe11aaf..33ae8d8 100755 --- a/tsshbatch.py +++ b/tsshbatch.py @@ -17,7 +17,7 @@ PROGNAME = "tsshbatch.py" BASENAME = PROGNAME.split(".py")[0] PROGENV = BASENAME.upper() -RCSID = "$Id: tsshbatch.py,v 1.138 2013/10/20 21:33:04 tundra Exp $" +RCSID = "$Id: tsshbatch.py,v 1.139 2013/10/20 22:59:21 tundra Exp $" VERSION = RCSID.split()[2] CPRT = "(c)" @@ -57,6 +57,8 @@ # Constants And Literals ##### + +COMMENT = "#" FAILURE = "FAILURE: %s" INDENTWIDTH = 8 OPTIONSLIST = "H:Nehkn:p:v" @@ -70,7 +72,7 @@ USAGE = \ PROGVER + "\n" +\ HOMEPAGE + "\n\n" +\ - "Usage: tsshbatch.py [-ehkv] [-n name] [-p pw] [-H 'host host ..' | serverlistfile] command arg arg arg \n" +\ + "Usage: tsshbatch.py [-Nehkv] [-n name] [-p pw] [-H 'host host ..' | serverlistfile] command arg arg arg \n" +\ " where,\n" +\ "\n" +\ " -H '...' List of targeted hosts passed as a single argument\n" +\ @@ -89,7 +91,7 @@ eBADARG = "Invalid command line: %s!" eBADFILE = "Cannot open '%s'!" eBADSUDO = "sudo Failed, Check Password Or Command! sudo Error Report: %s" -eFEWARGS = "Too few command line arguments!" +eFXERROR = "File Transfer Error While Attempting %s: %s" eNOCONNECT = "Cannot Connect: %s" eNOLOGIN = "Cannot Login! (Login/Password Bad?)" @@ -136,9 +138,41 @@ ##### -# Process A Command On A Host +# Tranfer Files To A Host ##### +def HostFileTransfer(host, user, pw, filelist): + + ssh = paramiko.SSHClient() + + # Connect and run the command, reporting results as we go + + try: + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + + if KEYEXCHANGE: + ssh.connect(host) + else: + ssh.connect(host, username=user, password=pw) + + sftp = ssh.open_sftp() + for srcfile in filelist: + + for destfile in filelist[srcfile]: + sftp.put(srcfile, destfile) + + sftp.close() + + except: + + attempted = "%s -> %s:%s" % (srcfile, host, destfile) + ErrorExit(eFXERROR % (attempted, sys.exc_info()[1][1])) + + ssh.close() + +# End of 'HostFileTransfer()' + + def HostCommand(host, user, pw, command): ssh = paramiko.SSHClient() @@ -207,7 +241,7 @@ # End of 'HostCommand()' - + ##### # Print Report ##### @@ -244,6 +278,10 @@ REPORTERR = True # Report stderr output from remote host UNAME = "" # Login name +# Place To Keep File Transfer Requests + +File_Transfer_List ={} + # Handle any options set in the environment OPTIONS = sys.argv[1:] @@ -260,18 +298,10 @@ except getopt.GetoptError, (errmsg, badarg): ErrorExit(eBADARG % errmsg) -# Unless the -H option is selected, this program -# requires a minimum of 2 command line arguments -# to run (hostlist file and command). If the user -# specifies -H, then all we need is the command. - -MINARGS = 2 - for opt, val in opts: if opt == "-H": HOSTS = val.split() - MINARGS = 1 if opt == "-N": PROMPTUSERNAME = True @@ -296,11 +326,6 @@ PrintStdout(RCSID) sys.exit() -# Make sure we have enough args to do the job - -if len(args) < MINARGS: - ErrorExit(eFEWARGS) - ##### # Go Do The Requested Work @@ -370,10 +395,17 @@ if CMD.startswith(SUDO) and not PWORD: PWORD = getpass.getpass(pSUDO) -# Iterate over the list of hosts, executing the command +# Iterate over the list of hosts, executing the command. +# Accomodate commenting out hosts in a list. for host in HOSTS: - host = host.strip() + + host = host.split(COMMENT)[0].strip() if host: - HostCommand(host, UNAME, PWORD, CMD) + + if File_Transfer_List: + HostFileTransfer(host, UNAME, PWORD, File_Transfer_List) + + if CMD: + HostCommand(host, UNAME, PWORD, CMD)