diff --git a/tsshbatch.py b/tsshbatch.py index f870bbc..53aa7f3 100755 --- a/tsshbatch.py +++ b/tsshbatch.py @@ -12,7 +12,7 @@ PROGNAME = "tsshbatch.py" BASENAME = PROGNAME.split(".py")[0] PROGENV = BASENAME.upper() -RCSID = "$Id: tsshbatch.py,v 1.122 2011/12/30 20:39:48 tundra Exp $" +RCSID = "$Id: tsshbatch.py,v 1.123 2011/12/30 22:53:26 tundra Exp $" VERSION = RCSID.split()[2] CPRT = "(c)" @@ -54,7 +54,7 @@ FAILURE = "FAILURE" INDENTWIDTH = 8 -OPTIONSLIST = "H:hkn:p:v" +OPTIONSLIST = "H:ehkn:p:v" PADWIDTH = 30 SEPARATOR = " ---> " SUCCESS = "SUCCESS" @@ -68,8 +68,9 @@ " where,\n" +\ "\n" +\ " -H '...' List of targeted hosts passed as a single argument\n" +\ + " -e Suppress output remote host error reporting\n" +\ " -h Display help\n" +\ - " -k Turn on key-exchange authentication\n" +\ + " -k Use key exchange-based authentication\n" +\ " -n name Specify login name\n" +\ " -p pw Specify login password\n" +\ " -v Display extended program version information\n" @@ -95,6 +96,16 @@ ##### +# Print Message(s) To stderr +##### + +def PrintStderr(msg, TERMINATOR="\n"): + sys.stderr.write(msg + TERMINATOR) + +# End of 'PrintStderr()' + + +##### # Print Message(s) To stdout ##### @@ -110,7 +121,7 @@ def ErrorExit(msg): - PrintStdout(msg) + PrintStderr(msg) sys.exit(1) # End Of 'ErrorExit()' @@ -150,8 +161,11 @@ stdin.write("%s\n" % pw) stdin.flush() - PrintReport([host, SUCCESS] + stdout.readlines() + stderr.readlines()) - + PrintReport([host, SUCCESS] + stdout.readlines()) + + if REPORTERR: + PrintReport(["", ""] + stderr.readlines(), HANDLER=PrintStderr) + # Catch authentication problems explicitly except paramiko.AuthenticationException: @@ -172,16 +186,22 @@ ##### # Expects input as [host, success/failure message, result1, result2, ...] +# Uses print handler to stdout by default but can be overriden at call +# time to invoke any arbitrary handler function. -def PrintReport(results): +def PrintReport(results, HANDLER=PrintStdout): - PrintStdout(SEPARATOR + results[0] + + # Any empty hostname here means that we're reporting stderr output + # and do not need to print the header again. + + if results[0]: + HANDLER(SEPARATOR + results[0] + TRAILER + (PADWIDTH - len(results[0])) * " " + results[1]) for r in results[2:]: # Command Results - PrintStdout(INDENTWIDTH * " " + r.strip()) + HANDLER(INDENTWIDTH * " " + r.strip()) # End of 'PrintReport()' @@ -197,6 +217,7 @@ HOSTS = "" # List of hosts to target KEYEXCHANGE = False # Do key exchange-based auth? PWORD = "" # Password +REPORTERR = True # Report stderr output from remote host UNAME = "" # Login name # Handle any options set in the environment @@ -228,6 +249,9 @@ HOSTS = val.split() MINARGS = 1 + if opt == "-e": + REPORTERR = False + if opt == "-h": PrintStdout(USAGE) sys.exit()