diff --git a/tsshbatch.py b/tsshbatch.py index 3bbd454..9c104ef 100755 --- a/tsshbatch.py +++ b/tsshbatch.py @@ -17,7 +17,7 @@ PROGNAME = "tsshbatch.py" BASENAME = PROGNAME.split(".py")[0] PROGENV = BASENAME.upper() -CVSID = "$Id: tsshbatch.py,v 1.160 2013/10/26 21:55:51 tundra Exp $" +CVSID = "$Id: tsshbatch.py,v 1.161 2013/10/28 03:37:09 tundra Exp $" VERSION = CVSID.split()[2] CPRT = "(c)" @@ -58,28 +58,34 @@ ##### -ABORTING = "Aborting ..." -COMMENT = "#" -CONSUCCESS = "SUCCESS: Connection Established" -FAILURE = "FAILURE: %s" -HOSTSEP = ":" -HOSTNOISE = "[%s]" +ABORTING = 'Aborting ...' +COMMENT = '#' +COMMANDS = 'Commands' +CONSUCCESS = 'SUCCESS: Connection Established' +FAILURE = 'FAILURE: %s' +GETFILES = 'Files To GET' +HOSTSEP = ':' +HOSTNOISE = '[%s]' +HOSTLIST = 'Hosts' INDENTWIDTH = 8 -OPTIONSLIST = "G:H:NP:Sef:hkn:p:yv" +OPTIONSLIST = 'G:H:NP:Sef:hkn:p:tyv' PADWIDTH = 12 PATHSEP = os.sep -SEPARATOR = " ---> " -STDIN = "-" +PUTFILES = 'Files To PUT' +SEPARATOR = ' ---> ' +STDIN = '-' SUDO = 'sudo' SUDOPROMPT = 'READINGSUDOPW' SUDOARGS = '-S -p %s' % SUDOPROMPT SUDOPWHINT = '(Default: login password): ' -TRAILER = ": " +SYMTABLE = 'Symbol Table' +TESTRUN = 'Test Run For' +TRAILER = ': ' USAGE = \ PROGVER + "\n" +\ HOMEPAGE + "\n\n" +\ - "Usage: tsshbatch.py [-NSehkvy -G 'file dest' -P 'file dest' -f cmdfile -n name -p pw ] -H 'host ..' | hostlistfile [command arg ... ]\n" +\ + "Usage: tsshbatch.py [-NSehkvxy -G 'file dest' -P 'file dest' -f cmdfile -n name -p pw ] -H 'host ..' | hostlistfile [command arg ... ]\n" +\ " where,\n" +\ "\n" +\ " -G 'file dest' GET file on host and write local dest directory\n" +\ @@ -93,6 +99,7 @@ " -k Use key exchange-based authentication\n" +\ " -n name Specify login name\n" +\ " -p pw Specify login password\n" +\ + " -t Run in test mode, don't actually execute commands\n" +\ " -v Display extended program version information\n" +\ " -y Turn on 'noisy' reporting for additional detail\n" @@ -144,12 +151,13 @@ #### GETSUDOPW = False # Prompt for sudo password -HOSTS = [] # List of hosts to target +Hosts = [] # List of hosts to target KEYEXCHANGE = False # Do key exchange-based auth? NOISY = False # Print output with extra detail PROMPTUSERNAME = False # Don't use $USER, prompt for username PWORD = "" # Password REPORTERR = True # Report stderr output from remote host +TESTMODE = False # Run program in test mode, don't actually execute commands UNAME = "" # Login name @@ -480,7 +488,7 @@ ProcessTXRQ(val, Get_Transfer_List) if opt == "-H": - HOSTS = val.split() + Hosts = val.split() if opt == "-N": PROMPTUSERNAME = True @@ -510,6 +518,9 @@ if opt == "-p": PWORD = val + if opt == "-t": + TESTMODE = True + if opt == "-v": PrintStdout(CVSID) sys.exit() @@ -527,7 +538,7 @@ # containing the list of targeted hosts and the remaining # arguments form the command. -if not HOSTS: +if not Hosts: # Even if we are only doing file transfers and no command # is specified, we have to have at least one argument here @@ -536,7 +547,7 @@ if not args: ErrorExit(eNOHOSTS) - HOSTS = ReadFile(args[0], HOSTS) + Hosts = ReadFile(args[0], Hosts) command = " ".join(args[1:]) # If hosts were passed on the command line, all the arguments @@ -636,16 +647,50 @@ # Do The Requested Work ##### -# Iterate over the list of hosts, executing any file transfers and -# commands. Accomodate commenting out hosts in a list. +# If we're running testmode, just report the final list of +# hosts and commands that would be run -for host in HOSTS: +if TESTMODE: - if Get_Transfer_List: - HostFileTransfer(host, UNAME, PWORD, Get_Transfer_List, GET=True) + symtbl = [] + gets = [] + puts = [] - if Put_Transfer_List: - HostFileTransfer(host, UNAME, PWORD, Put_Transfer_List, GET=False) + # Unroll and format dictionary structures - if Commands: - HostCommands(host, UNAME, PWORD, SUDOPW, Commands) + for symbol in SymbolTable: + symtbl.append(symbol + (PADWIDTH - len(symbol)) * " "+ SEPARATOR + SymbolTable[symbol]) + + for xfers, unrolled in ((Get_Transfer_List, gets), (Put_Transfer_List, puts)): + + for source in xfers: + for dest in xfers[source]: + unrolled.append(source + (PADWIDTH*3 - len(source)) * " "+ SEPARATOR + dest) + + for prompt, description, items in ((TESTRUN, " ".join(sys.argv), ["\n"]), + (SYMTABLE, "", symtbl + ["\n"]), + (GETFILES, "", gets + ["\n"]), + (PUTFILES, "", puts + ["\n"]), + (HOSTLIST, "", Hosts + ["\n"]), + (COMMANDS, "", Commands + ["\n"]) + ): + + PrintReport([prompt, description] + items) + + +# Otherwise, actually do the work by iterating over the list of hosts, +# executing any file transfers and commands. Accomodate commenting +# out hosts in a list. + +else : + + for host in Hosts: + + if Get_Transfer_List: + HostFileTransfer(host, UNAME, PWORD, Get_Transfer_List, GET=True) + + if Put_Transfer_List: + HostFileTransfer(host, UNAME, PWORD, Put_Transfer_List, GET=False) + + if Commands: + HostCommands(host, UNAME, PWORD, SUDOPW, Commands)