Newer
Older
tsshbatch / tsshbatch.py
#!/usr/bin/env python
# tsshbatch.py - Non-Interactive ssh Connection
# Copyright (c) 2011-2016 TundraWare Inc.
# Permission Hereby Granted For Unrestricted Personal Or Commercial Use
# See "tsshbatch-license.txt" For Licensing Details
#
# For Updates See:  http://www.tundraware.com/Software/tsshbatch

# A tip of the hat for some of the ideas in the program goes to:
#
#     http://jessenoller.com/2009/02/05/ssh-programming-with-paramiko-completely-different/

#####
# Version Information - Overwritten by makefile during release process
#####

GITID      = '51f8f43 tundra Fri Oct 14 14:44:40 2016 -0500'
VERSION    = '1.316'


#####
# Program Housekeeping
#####

PROGNAME     = "tsshbatch.py"
BASENAME     = PROGNAME.split(".py")[0]
PROGENV      = BASENAME.upper()
CMDINCL      = PROGENV + "CMDS"
HOSTINCL     = PROGENV + "HOSTS"

CPRT         = "(c)"
PROGDATE     = "2011-2016"
OWNER        = "TundraWare Inc."
RIGHTS       = "All Rights Reserved."
COPYRIGHT    = "Copyright %s %s, %s  %s" % (CPRT, PROGDATE, OWNER, RIGHTS)

PROGVER      = PROGNAME + " " + VERSION + (" - %s" % COPYRIGHT)
HOMEPAGE     = "http://www.tundraware.com/Software/%s\n" % BASENAME


#####
# Suppress Deprecation Warnings
# Required in some older environments where paramiko version
# is behind the python libs version.
#####

import warnings
warnings.filterwarnings("ignore", "", DeprecationWarning)


#####
# Imports
#####

import collections
import commands
import getopt
import getpass
import os
import paramiko
import shlex
import socket
import sys
import time


#####
# Constants And Literals
#####


ABORTING     = 'Aborting ...'
BANNERTIME   = 'Elapsed Time: %s Seconds'
BANNERMSG    = '%s %s %s On %s At %s' % (PROGNAME, VERSION, '%s', '%s', '%s')
BANNEREND    = 'Ended'
BANNERSTART  = 'Started'
COMMENT      = '#'
COMMANDS     = 'Commands'
CFGKEYID     = 'identityfile'
CFGREALHOST  = 'hostname'
CONSUCCESS   = 'SUCCESS: Connection Established'
EXECUTE      = '!'
FILEGET      = '.getfile'
FILEPUT      = '.putfile'
GETFILES     = 'Files To GET'
HOSTSEP      = '-'
HOSTNOISE    = '[%s]'
HOSTLIST     = 'Hosts'
INDENTWIDTH  = 8
NOMATCH      = "No string matches!"
NOTIFICATION = 'NOTIFICATION'
OPTIONSLIST  = 'BC:EF:G:H:KLNP:ST:V:Wabef:hi:kl:n:p:qrstvxy'
PADWIDTH     = 12
PATHDELIM    = ':'
PATHSEP      = os.sep
PUTFILES     = 'Files To PUT'
SEPARATOR    = ' --->  '
STDIN        = '-'
SUDO         = 'sudo'
SUDOPROMPT   = 'READINGSUDOPW'
SUDOARGS     = '-S -p %s' % SUDOPROMPT
SUDOPWHINT   = '(Default: login password): '
SYMTABLE     = 'Global Symbol Table'
TESTRUN      = 'Test Run For'
TRAILER      = ': '
USERVAR      = 'USER'

USAGE        = \
    PROGVER  + "\n"                                                                                     +\
    HOMEPAGE + "\n"                                                                                     +\
    "Usage:  tsshbatch.py [-BEF:KLNSTVWaehkqrstvy -C configfile -G 'file dest' -P 'file dest' -f cmdfile -l logfile -n name -p pw ] -H 'host ...' -i 'hostfile ...' [command arg ... ]\n" +\
    "          where,\n"                                                                                +\
    "\n"                                                                                                +\
    "            -B                  Print start and stop statistics (Off)\n"                               +\
    "            -C configfile       Specify location of ssh configuration file (~/.ssh/config)\n"          +\
    "            -E                  Write error output to stdout instead of stderr (Output to stderr)\n"   +\
    "            -F 'string ...'     Report host- and command files with matching strings\n"                +\
    "            -K                  Force password prompting - Overrides previous -k\n"                    +\
    "            -G 'file dest'      GET file on host and write local dest directory\n"                     +\
    "            -H '...'            List of targeted hosts passed as a single argument\n"                  +\
    "            -L                  List all known hostfiles and cmdfiles, and exit\n"                     +\
    "            -N                  Force prompting for username\n"                                        +\
    "            -P 'file dest'      PUT local file to host dest directory\n"                               +\
    "            -S                  Force prompting for sudo password\n"                                   +\
    "            -T seconds          Timeout for ssh connection attempts (15 sec)\n"                        +\
    "            -V 'string ...'     Report host- and command files without matching strings\n"             +\
    "            -W                  Write list of hosts to stdout and exit\n"                              +\
    "            -a                  Don't abort program after failed file transfers (Abort on failure)\n"  +\
    "            -b                  Don't abort program after failed sudo command (Abort on failure)\n"    +\
    "            -e                  Don't report remote host stderr output (Report host stderr) \n"        +\
    "            -f cmdfile          Read commands from file\n"                                             +\
    "            -h                  Display help\n"                                                        +\
    "            -i 'file file...'   Retrieve list of hosts from hostfile.  Can be repeated.\n"             +\
    "            -k                  Use key exchange-based authentication (Use password auth)\n"           +\
    "            -l logfile          Log errors to logfile (/dev/null)\n"                                   +\
    "            -n name             Specify login name\n"                                                  +\
    "