diff --git a/tsshbatch.py b/tsshbatch.py index 64d12db..12e9cb2 100755 --- a/tsshbatch.py +++ b/tsshbatch.py @@ -20,7 +20,7 @@ CMDINCL = PROGENV + "CMDS" HOSTINCL = PROGENV + "HOSTS" -CVSID = "$Id: tsshbatch.py,v 1.204 2014/12/04 23:34:35 tundra Exp $" +CVSID = "$Id: tsshbatch.py,v 1.205 2014/12/08 15:50:42 tundra Exp $" VERSION = CVSID.split()[2] CPRT = "(c)" PROGDATE = "2011-2014" @@ -72,7 +72,7 @@ HOSTNOISE = '[%s]' HOSTLIST = 'Hosts' INDENTWIDTH = 8 -OPTIONSLIST = 'EKG:H:NP:ST:aef:hkl:n:p:qtvxy' +OPTIONSLIST = 'EKG:H:NP:ST:aef:hkl:n:p:qstvxy' PADWIDTH = 12 PATHDELIM = ':' PATHSEP = os.sep @@ -91,7 +91,7 @@ USAGE = \ PROGVER + "\n" +\ HOMEPAGE + "\n\n" +\ - "Usage: tsshbatch.py [-EKNSTaehkqvxy -G 'file dest' -P 'file dest' -f cmdfile -l logfile -n name -p pw ] -H 'host ..' | hostlistfile [command arg ... ]\n" +\ + "Usage: tsshbatch.py [-EKNSTaehkqstvy -G 'file dest' -P 'file dest' -f cmdfile -l logfile -n name -p pw ] -H 'host ..' | hostlistfile [command arg ... ]\n" +\ " where,\n" +\ "\n" +\ " -E Write error output to stdout instead of stderr\n" +\ @@ -111,6 +111,7 @@ " -n name Specify login name\n" +\ " -p pw Specify login password\n" +\ " -q Quiet mode - produce less 'noisy' output\n" +\ + " -s Silence all program noise - only return command output\n" +\ " -t Run in test mode, don't actually execute commands\n" +\ " -v Display extended program version information\n" +\ " -x Turn off test mode (if on) and execute requests\n" +\ @@ -196,10 +197,8 @@ Hosts = [] # List of hosts to target KEYEXCHANGE = False # Do key exchange-based auth? LOGFILE = "/dev/null" # Where paramiko logging output goes -NOISY = False # Print output with extra detail PROMPTUSERNAME = False # Don't use $USER, prompt for username PWORD = "" # Password -QUIET = False # Make output less noisy REDIRSTDERR = False # Redirect stderr to stdout REPORTERR = True # Report stderr output from remote host TESTMODE = True # Run program in test mode, don't actually execute commands @@ -207,6 +206,14 @@ UNAME = "" # Login name +# Noise levels + +NOISELEVEL = 0 # Normal noisy +SILENT = 1 # No program noise at all +QUIET = 2 # Make output less noisy +NOISY = 3 # Print output with extra detail + + ##### # Global Data Structures ##### @@ -274,16 +281,16 @@ # Print Message(s) To stderr ##### -def PrintStderr(msg, TERMINATOR="\n"): +def PrintStderr(msg, EOL="\n"): # If we've been told to redirect to stdout, do so instead if REDIRSTDERR: - PrintStdout(msg, TERMINATOR) + PrintStdout(msg, EOL) else: - sys.stderr.write(msg + TERMINATOR) + sys.stderr.write(msg + EOL) sys.stderr.flush() # End of 'PrintStderr()' @@ -293,8 +300,8 @@ # Print Message(s) To stdout ##### -def PrintStdout(msg, TERMINATOR="\n"): - sys.stdout.write(msg + TERMINATOR) +def PrintStdout(msg, EOL="\n"): + sys.stdout.write(msg + EOL) sys.stdout.flush() # End of 'PrintStdout()' @@ -400,7 +407,7 @@ else: ssh.connect(host, username=user, password=pw, allow_agent=False, look_for_keys=False, timeout=TIMEOUT) - if not QUIET: + if NOISELEVEL != QUIET: PrintReport([host, CONSUCCESS]) # Run all requested commands @@ -445,7 +452,7 @@ raise SystemExit cmdreport = " [%s]" % command - if QUIET: + if NOISELEVEL == QUIET: cmdreport = "" PrintReport([host + " (stdout)" + cmdreport, "\n"] + stdout.readlines() + ["\n"]) @@ -483,20 +490,32 @@ def PrintReport(results, HANDLER=PrintStdout): - hostname = results[0] - HANDLER(SEPARATOR + hostname + - TRAILER + - (PADWIDTH - len(results[0])) * " " + - results[1]) - # Prepend the host name if we've asked for noisy reporting + if NOISELEVEL != SILENT: - hostnoise ="" - if NOISY: - hostnoise = HOSTNOISE % hostname + hostname = results[0] + HANDLER(SEPARATOR + hostname + + TRAILER + + (PADWIDTH - len(results[0])) * " " + + results[1]) - for r in results[2:]: # Command Results - HANDLER(hostnoise + INDENTWIDTH * " " + r.strip()) + # Prepend the host name if we've asked for noisy reporting + + hostnoise ="" + if NOISELEVEL == NOISY: + hostnoise = HOSTNOISE % hostname + + for r in results[2:]: # Command Results + HANDLER(hostnoise + INDENTWIDTH * " " + r.strip()) + + # In silent mode we just return results with no headers or formatting. + # We suppress the last line in the list. It is an empty line + # introduced for formatting by the caller, but isn't really part + # of the returned output. + else: + + for r in results[2:-1]: + HANDLER(r, EOL="") # End of 'PrintReport()' @@ -738,8 +757,10 @@ PWORD = val if opt == "-q": - QUIET = True - NOISY = False + NOISELEVEL = QUIET + + if opt == "-s": + NOISELEVEL = SILENT if opt == "-t": TESTMODE = True @@ -752,8 +773,7 @@ TESTMODE = False if opt == "-y": - NOISY = True - QUIET = False + NOISELEVEL = NOISY ##### diff --git a/tsshbatch.rst b/tsshbatch.rst index 6f5f8b4..16abd02 100644 --- a/tsshbatch.rst +++ b/tsshbatch.rst @@ -30,7 +30,7 @@ -------- :: - tsshbatch.py [-EKNSTaehkqvxy -G 'file dest' -P 'file dest' -f cmdfile -l logfile -n name -p pw ] -H 'host ..' | hostlistfile [command arg ... ] + tsshbatch.py [-EKNSTaehkqstvxy -G 'file dest' -P 'file dest' -f cmdfile -l logfile -n name -p pw ] -H 'host ..' | hostlistfile [command arg ... ] DESCRIPTION ----------- @@ -1056,7 +1056,7 @@ :: - $Id: tsshbatch.rst,v 1.156 2014/12/05 20:59:05 tundra Exp $ + $Id: tsshbatch.rst,v 1.157 2014/12/08 15:50:42 tundra Exp $ This document was produced with ``emacs``, ``RestructuredText``, and ``TeX Live``.