diff --git a/tsshbatch.py b/tsshbatch.py index 2d15515..fe11aaf 100755 --- a/tsshbatch.py +++ b/tsshbatch.py @@ -17,11 +17,11 @@ PROGNAME = "tsshbatch.py" BASENAME = PROGNAME.split(".py")[0] PROGENV = BASENAME.upper() -RCSID = "$Id: tsshbatch.py,v 1.137 2013/02/22 21:29:00 tundra Exp $" +RCSID = "$Id: tsshbatch.py,v 1.138 2013/10/20 21:33:04 tundra Exp $" VERSION = RCSID.split()[2] CPRT = "(c)" -DATE = "2011" +DATE = "2011-2013" OWNER = "TundraWare Inc." RIGHTS = "All Rights Reserved." COPYRIGHT = "Copyright %s %s, %s %s" % (CPRT, DATE, OWNER, RIGHTS) @@ -59,9 +59,9 @@ FAILURE = "FAILURE: %s" INDENTWIDTH = 8 -OPTIONSLIST = "H:ehkn:p:v" +OPTIONSLIST = "H:Nehkn:p:v" PADWIDTH = 30 -SEPARATOR = " ---> " +SEPERATOR = " ---> " SUCCESS = "SUCCESS" SUDO = 'sudo' SUDOPROMPT = 'READINGSUDOPW' @@ -74,6 +74,7 @@ " where,\n" +\ "\n" +\ " -H '...' List of targeted hosts passed as a single argument\n" +\ + " -N Force prompting for username\n" +\ " -e Don't report remote host stderr output\n" +\ " -h Display help\n" +\ " -k Use key exchange-based authentication\n" +\ @@ -99,7 +100,7 @@ pPASS = "Password: " pSUDO = "%s Password: " % SUDO -pUSER = "Username: " +pUSER = "Username (%s): " ##### @@ -217,7 +218,7 @@ def PrintReport(results, HANDLER=PrintStdout): - HANDLER(SEPARATOR + results[0] + + HANDLER(SEPERATOR + results[0] + TRAILER + (PADWIDTH - len(results[0])) * " " + results[1]) @@ -236,11 +237,12 @@ # Options that can be overriden by user -HOSTS = "" # List of hosts to target -KEYEXCHANGE = False # Do key exchange-based auth? -PWORD = "" # Password -REPORTERR = True # Report stderr output from remote host -UNAME = "" # Login name +HOSTS = "" # List of hosts to target +KEYEXCHANGE = False # Do key exchange-based auth? +PROMPTUSERNAME = False # Don't use $USER, prompt for username +PWORD = "" # Password +REPORTERR = True # Report stderr output from remote host +UNAME = "" # Login name # Handle any options set in the environment @@ -271,6 +273,9 @@ HOSTS = val.split() MINARGS = 1 + if opt == "-N": + PROMPTUSERNAME = True + if opt == "-e": REPORTERR = False @@ -301,17 +306,32 @@ # Go Do The Requested Work ##### -# 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 +# Precedence of authentication credential sources: +# +# 1) Key exchange +# 2) Command Line/$TSSHBATCH env variable sets name and/or password +# 3) Forced prompting for name via -N +# 4) Name picked up from $USER (Default behavior) if not KEYEXCHANGE: + # Preset commandline and/or program option variable username takes precedence if not UNAME: - UNAME = raw_input(pUSER) + current_user = os.getenv("USER") + + # By default, use $USER as the login name and don't prompt for it + if not PROMPTUSERNAME: + UNAME = current_user + + # Interactive prompting using $USER as default + else: + + UNAME = raw_input(pUSER % current_user) + if not UNAME: # User just hit return - wants default + UNAME = current_user + + # Preset commandline and/or program option variable password takes precedence if not PWORD: PWORD = getpass.getpass(pPASS) @@ -343,8 +363,8 @@ # If user want 'sudo' execution, they MUST provide a password because # key exchange-based authentication is not part of sudo. If the -# password has not been set by some other means (command line or -# environment variable), ask for it here. +# password has not been set by some other means (interactive, command +# line or environment variable), ask for it here. CMD = CMD.strip() if CMD.startswith(SUDO) and not PWORD: diff --git a/tsshbatch.rst b/tsshbatch.rst index f0a1894..818dc9c 100644 --- a/tsshbatch.rst +++ b/tsshbatch.rst @@ -8,7 +8,7 @@ SYNOPSIS -------- -tsshbatch.py [-ehvk] [-n name] [-p pw] [-H 'h1 h2 ...' | hostlistfile] command arg ... +tsshbatch.py [-Nehvk] [-n name] [-p pw] [-H 'h1 h2 ...' | hostlistfile] command arg ... DESCRIPTION @@ -31,19 +31,28 @@ OPTIONS ------- - -H 'hostlist' Single quoted list of hosts on which to run the command +``tsshbatch`` supports a variety of options which can be specified +on either the command line or in the ``$TSSHBATCH`` environment +variable: - -e Don't report remote host stderr output + -H hostlist List of hosts on which to run the command. This should + be enclosed in *single quotes* as in:: - -h Print help information + 'host1 host2 host3' - -k Use ssh keys instead of name/password credentials + -N Force interactive username dialog - -n name Login name to use + -e Don't report remote host stderr output - -p pw Password to use when logging in and/or doing sudo + -h Print help information - -v Print detailed program version information and exit + -k Use ssh keys instead of name/password credentials + + -n name Login name to use + + -p pw Password to use when logging in and/or doing sudo + + -v Print detailed program version information and exit ENVIRONMENT @@ -101,12 +110,15 @@ 2) Authentication Using Name And Password + The simplest way to use ``tsshbatch`` is to just name the hosts can command you want to run:: tsshbatch.py linux-prod-hosts uptime - You will be prompted for your username and password one time which + By default, ``tsshbatch`` uses your login name found in the + ``$USER`` environment variable when logging into other systems. + In this example, you'll be prompted only for your password which ``tsshbatch`` will then use to log into each of the machines named in ``linux-prod-hosts``. (*Notice that his assumes your name and password are the same on each host!*) @@ -240,6 +252,17 @@ again to do ``sudo`` privilege promotion. +5) Precedence Of Authentication Options + + ``tsshbatch`` supports these various authentication options + in a particular heirarchy using a "first match wins" + scheme. From highest to lowest, the precedence is: + + 1. Key Exchange + 2. ``-n`` and ``-p`` options on command line or in ``$TSSHBATCH`` + 3. Forced interactive prompting for user name with ``$USER`` as the default. + 4. Using ``$USER`` as the login name and prompting only for password. (Default program behavior). + OTHER ----- @@ -309,7 +332,7 @@ :: - $Id: tsshbatch.rst,v 1.110 2013/05/21 17:47:22 tundra Exp $ + $Id: tsshbatch.rst,v 1.111 2013/10/20 21:33:04 tundra Exp $ You can find the latest version of this program at: