diff --git a/tsshbatch.py b/tsshbatch.py index b57c205..846e122 100755 --- a/tsshbatch.py +++ b/tsshbatch.py @@ -1,7 +1,21 @@ #!/usr/bin/env python # Non-Interactive ssh Connection -# $Id: tsshbatch.py,v 1.108 2010/12/15 20:26:07 tundra Exp $ +# $Id: tsshbatch.py,v 1.109 2010/12/15 21:18:35 tundra Exp $ +##### +# Suppress Deprecation Warnings until Paramiko catches up +# to latest Python modules +##### + +import warnings +warnings.filterwarnings("ignore", "", DeprecationWarning) + + +##### +# Imports +##### + +import getpass import paramiko import socket import sys @@ -17,16 +31,25 @@ SEPARATOR = " ---> " SUCCESS = "SUCCESS" TRAILER = ": " -USAGE = "Usage: sshlogin.py username password hostname command" - +USAGE = "Usage: sshlogin.py serverlistfile command" ##### # Error Messages ##### +eBADFILE = "Cannot open '%s'!" eNOCONNECT = "Cannot Connect! (Name/Address Bad? Destination Unreachable?)" eNOLOGIN = "Cannot Login! (Login/Password Bad?)" + +##### +# Prompts +##### + +pPASS = "Password: " +pUSER = "Username: " + + ##### # Print Message(s) To stdout ##### @@ -50,12 +73,41 @@ ##### +# Process A Command On A Host +##### + +def HostCommand(host, user, pw, command): + + ssh = paramiko.SSHClient() + + # 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(command) + PrintReport([host, SUCCESS] + stdout.readlines() + stderr.readlines()) + + except paramiko.AuthenticationException: + PrintReport([host, FAILURE, eNOLOGIN]) + + except socket.gaierror: + PrintReport([host, FAILURE, eNOCONNECT]) + + except socket.error: + PrintReport([host, FAILURE, eNOCONNECT]) + + ssh.close() + +# End of 'HostCommand()' + + +##### # Print Report ##### # Expects input as [host, success/failure message, result1, result2, ...] - def PrintReport(results): PrintStdout(SEPARATOR + results[0] + @@ -73,34 +125,36 @@ # Program Entry Point ##### -# Unpack command line arguments +# Make sure we have sufficient command line args -if len(sys.argv[1:]) >= 4: - user, pw, host = sys.argv[1:4] - cmd = " ".join(sys.argv[4:]) - -# Otherwise let the user know - -else: +if len(sys.argv[1:]) < 2: ErrorExit(USAGE) -ssh = paramiko.SSHClient() +# Get the list of hosts -# Connect and run the command, reporting results as we go +try: + f = open(sys.argv[1]) + hosts = f.readlines() + f.close() -try: - ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - ssh.connect(host, username=user, password=pw) - stdin, stdout, stderr = ssh.exec_command(cmd) - PrintReport([host, SUCCESS] + stdout.readlines()) +except: + ErrorExit(eBADFILE % sys.argv[1]) + +# Create the command + +cmd = " ".join(sys.argv[2:]) -except paramiko.AuthenticationException: - PrintReport([host, FAILURE, eNOLOGIN]) +# Get user name & password -except socket.gaierror: - PrintReport([host, FAILURE, eNOCONNECT]) +uname = raw_input(pUSER) +pword = getpass.getpass(pPASS) -except socket.error: - PrintReport([host, FAILURE, eNOCONNECT]) - +# Iterate over the list of hosts, executing the command + +for host in hosts: + host = host.strip() + if host: + HostCommand(host, uname, pword, cmd) + +