diff --git a/tsshbatch.py b/tsshbatch.py index f21176d..38ecf7d 100755 --- a/tsshbatch.py +++ b/tsshbatch.py @@ -1,14 +1,63 @@ #!/usr/bin/env python # Non-Interactive ssh Connection -# usage: sshservers.py host username password command -# $Id: tsshbatch.py,v 1.101 2010/09/27 20:35:36 tundra Exp $ +# $Id: tsshbatch.py,v 1.102 2010/12/14 18:47:50 tundra Exp $ import paramiko import sys +##### +# Constants And Literals +##### + +SEPARATOR = " ---> " +USAGE = "Usage: sshlogin.py username password hostname command" + + +##### +# Print Message(s) To stdout +##### + +# Expects an iterable object as input + +def PrintStdout(messages, TERMINATOR="\n"): + + for msg in messages: + sys.stdout.write(msg + TERMINATOR) + +# End of 'PrintStdout()' + + +##### +# Display An Error Message And Exit +##### + +def ErrorExit(msg): + + PrintStdout([msg]) + sys.exit() + +# End Of 'ErrorExit()' + + +##### +# Program Entry Point +##### + +# Unpack command line arguments + +if len(sys.argv[1:]) == 4: + user, pw, host, cmd = sys.argv[1:] + +# Otherwise let the user know + +else: + ErrorExit(USAGE) + ssh = paramiko.SSHClient() +PrintStdout(host + SEPARATOR, TERMINATOR="") ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host, username=user, password=pw) -stdin, stdout, stderr = ssh.exec_command(command) -print stdout.readlines() +stdin, stdout, stderr = ssh.exec_command(cmd) +PrintStdout(stdout) +