diff --git a/tsshbatch.py b/tsshbatch.py index 0c40d27..f70d4a3 100755 --- a/tsshbatch.py +++ b/tsshbatch.py @@ -1,6 +1,17 @@ #!/usr/bin/env python # Non-Interactive ssh Connection -# $Id: tsshbatch.py,v 1.111 2011/02/11 18:25:34 tundra Exp $ + + +##### +# Program Housekeeping +##### + +PROGNAME = "tsshbatch.py" +BASENAME = PROGNAME.split(".py")[0] +PROGENV = BASENAME.upper() +RCSID = "Id: tsshbatch.py,v 1.111 2011/02/11 18:25:34 tundra Exp tundra $" +VERSION = RCSID.split()[2] + ##### # Suppress Deprecation Warnings until Paramiko catches up @@ -15,7 +26,9 @@ # Imports ##### +import getopt import getpass +import os import paramiko import socket import sys @@ -27,16 +40,23 @@ FAILURE = "FAILURE" INDENTWIDTH = 8 +OPTIONSLIST = "kn:p:" PADWIDTH = 30 SEPARATOR = " ---> " SUCCESS = "SUCCESS" TRAILER = ": " -USAGE = "Usage: tsshbatch.py serverlistfile command" - +USAGE = "Usage: tsshbatch.py -n username -p password serverlistfile command\n" +\ + " tsshbatch.py [-k] serverlistfile command\n" +\ + " where,\n" +\ + "\n" +\ + " -k Turns on key-exchange authentication\n" +\ + " -n name Specifies login name\n" +\ + " -p pw Specifies login password\n" ##### # Error Messages ##### +eBADARG = "Invalid command line: %s!" eBADFILE = "Cannot open '%s'!" eNOCONNECT = "Cannot Connect! (Name/Address Bad? Destination Unreachable?)" eNOLOGIN = "Cannot Login! (Login/Password Bad?)" @@ -126,15 +146,52 @@ # Program Entry Point ##### -# Make sure we have sufficient command line args +# Options that can be overriden by user -if len(sys.argv[1:]) < 2: +KEYEXCHANGE = False +PWORD = "" +UNAME = "" + +# Handle any options set in the environment + +OPTIONS = sys.argv[1:] +envopt = os.getenv(PROGENV) +if envopt: + OPTIONS = shlex.split(envopt) + OPTIONS + +# Combine them with those given on the command line +# This allows the command line to override defaults +# set in the environment + +try: + opts, args = getopt.getopt(OPTIONS, OPTIONSLIST) +except getopt.GetoptError, (errmsg, badarg): + ErrorExit(eBADARG % errmsg) + +# Make sure we have sufficient command line args +# to do something useful + +if len(args) < 2: ErrorExit(USAGE) +for opt, val in opts: + + if opt == "-k": + KEYEXCHANGE = True + + if opt == "-n": + UNAME = val + + if opt == "-p": + PWORD = val + +# Go do the requested work + + # Get the list of hosts try: - f = open(sys.argv[1]) + f = open(args[0]) hosts = f.readlines() f.close() @@ -143,12 +200,21 @@ # Create the command -cmd = " ".join(sys.argv[2:]) - -# Get user name & password +cmd = " ".join(args[1:]) -uname = raw_input(pUSER) -pword = getpass.getpass(pPASS) +# If we're not doing key exchange-based authentication, get +# user name & password. + +# Only do this if they've not been set in the environment +# variable/command line + +if not KEYEXCHANGE: + + if not UNAME: + UNAME = raw_input(pUSER) + + if not PWORD: + PWORD = getpass.getpass(pPASS) # Iterate over the list of hosts, executing the command @@ -156,6 +222,6 @@ for host in hosts: host = host.strip() if host: - HostCommand(host, uname, pword, cmd) + HostCommand(host, UNAME, PWORD, cmd)