diff --git a/tsshbatch.py b/tsshbatch.py index 0e39155..b57c205 100755 --- a/tsshbatch.py +++ b/tsshbatch.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # Non-Interactive ssh Connection -# $Id: tsshbatch.py,v 1.107 2010/12/15 16:30:32 tundra Exp $ +# $Id: tsshbatch.py,v 1.108 2010/12/15 20:26:07 tundra Exp $ import paramiko import socket @@ -11,10 +11,12 @@ # Constants And Literals ##### +FAILURE = "FAILURE" INDENTWIDTH = 8 PADWIDTH = 30 -FAILURE = "Command Failed" SEPARATOR = " ---> " +SUCCESS = "SUCCESS" +TRAILER = ": " USAGE = "Usage: sshlogin.py username password hostname command" @@ -29,12 +31,8 @@ # Print Message(s) To stdout ##### -# Expects an iterable object as input - -def PrintStdout(messages, TERMINATOR="\n"): - - for msg in messages: - sys.stdout.write(INDENT + msg + TERMINATOR) +def PrintStdout(msg, TERMINATOR="\n"): + sys.stdout.write(msg + TERMINATOR) # End of 'PrintStdout()' @@ -45,13 +43,33 @@ def ErrorExit(msg): - PrintStdout([msg]) + PrintStdout(msg) sys.exit() # End Of 'ErrorExit()' ##### +# Print Report +##### + +# Expects input as [host, success/failure message, result1, result2, ...] + + +def PrintReport(results): + + PrintStdout(SEPARATOR + results[0] + + TRAILER + + (PADWIDTH - len(results[0])) * " " + + results[1]) + + for r in results[2:]: # Command Results + PrintStdout(INDENTWIDTH * " " + r.strip()) + +# End of 'PrintReport()' + + +##### # Program Entry Point ##### @@ -64,46 +82,25 @@ # Otherwise let the user know else: - - - - - - - - - - - - - - - - ErrorExit(USAGE) ssh = paramiko.SSHClient() -# Report attempt - -INDENT = "" -PrintStdout([host + SEPARATOR]) -INDENT += " " * INDENTWIDTH - # Connect and run the command, reporting results as we go try: ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host, username=user, password=pw) stdin, stdout, stderr = ssh.exec_command(cmd) - PrintStdout(stdout.readlines()) + PrintReport([host, SUCCESS] + stdout.readlines()) except paramiko.AuthenticationException: - PrintStdout([eNOLOGIN]) + PrintReport([host, FAILURE, eNOLOGIN]) except socket.gaierror: - PrintStdout([eNOCONNECT]) + PrintReport([host, FAILURE, eNOCONNECT]) except socket.error: - PrintStdout([eNOCONNECT]) + PrintReport([host, FAILURE, eNOCONNECT]) +