diff --git a/tsshbatch.py b/tsshbatch.py index a9faea5..0179e78 100755 --- a/tsshbatch.py +++ b/tsshbatch.py @@ -17,7 +17,7 @@ PROGNAME = "tsshbatch.py" BASENAME = PROGNAME.split(".py")[0] PROGENV = BASENAME.upper() -CVSID = "$Id: tsshbatch.py,v 1.141 2013/10/21 00:03:33 tundra Exp $" +CVSID = "$Id: tsshbatch.py,v 1.142 2013/10/21 03:21:58 tundra Exp $" VERSION = CVSID.split()[2] CPRT = "(c)" @@ -63,6 +63,7 @@ INDENTWIDTH = 8 OPTIONSLIST = "G:H:NP:ehkn:p:v" PADWIDTH = 30 +PATHSEP = os.sep SEPERATOR = " ---> " SUCCESS = "SUCCESS" SUDO = 'sudo' @@ -75,10 +76,10 @@ "Usage: tsshbatch.py [-Nehkv] [-n name] [-p pw] -G 'file dest' -P 'file dest' [-H 'host ..' | serverlistfile] command arg ... \n" +\ " where,\n" +\ "\n" +\ - " -G 'file dest' GET file on host and write local dest\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" +\ " -N Force prompting for username\n" +\ - " -P 'file dest' PUT local file to host dest\n" +\ + " -P 'file dest' PUT local file to host dest directory\n" +\ " -e Don't report remote host stderr output\n" +\ " -h Display help\n" +\ " -k Use key exchange-based authentication\n" +\ @@ -94,13 +95,20 @@ eBADFILE = "Cannot open '%s'!" eBADSUDO = "sudo Failed, Check Password Or Command! sudo Error Report: %s" eBADTXRQ = "Bad Transfer Request: %s Must Have Exactly 1 Source And 1 Destination!" -eFXERROR = "File Transfer Error While Attempting %s: %s" +eFXERROR = "File Transfer Error: %s" eNOCONNECT = "Cannot Connect: %s" eNOHOSTS = "No Hosts Specified!" eNOLOGIN = "Cannot Login! (Login/Password Bad?)" ##### +# Informational Messages +##### + +iTXFILE = "Writing %s To %s ..." + + +##### # Prompts ##### @@ -145,13 +153,13 @@ # Transfer Files To A Host ##### -def HostFileTransfer(host, user, pw, filelist): +def HostFileTransfer(host, user, pw, filelist, GET=False): - ssh = paramiko.SSHClient() + try: + ssh = paramiko.SSHClient() # Connect and run the command, reporting results as we go - try: ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) if KEYEXCHANGE: @@ -160,19 +168,33 @@ ssh.connect(host, username=user, password=pw) sftp = ssh.open_sftp() - for srcfile in filelist: - for destfile in filelist[srcfile]: - sftp.put(srcfile, destfile) + for srcfile in filelist: + for destdir in filelist[srcfile]: + + # Make sure we have a trailing path separator + destination = destdir + if destination[-1] != PATHSEP: + destination += PATHSEP + + if GET: + destination += host + "-" + os.path.basename(srcfile) + PrintStdout(iTXFILE % (host + ":" + srcfile, destination)) + sftp.get(srcfile, destination) + + else: + destination += os.path.basename(srcfile) + PrintStdout(iTXFILE % (srcfile, host + ":" + destination)) + sftp.put(srcfile, destination) sftp.close() + ssh.close() except: - attempted = "%s -> %s:%s" % (srcfile, host, destfile) - ErrorExit(eFXERROR % (attempted, sys.exc_info()[1][1])) - - ssh.close() + sftp.close() + ssh.close() + ErrorExit(eFXERROR % (sys.exc_info()[1])) # End of 'HostFileTransfer()' @@ -442,8 +464,12 @@ host = host.split(COMMENT)[0].strip() if host: - print Get_Transfer_List - print Put_Transfer_List + if Get_Transfer_List: + HostFileTransfer(host, UNAME, PWORD, Get_Transfer_List, GET=True) + + if Put_Transfer_List: + HostFileTransfer(host, UNAME, PWORD, Put_Transfer_List, GET=False) + if CMD: HostCommand(host, UNAME, PWORD, CMD)