diff --git a/tsshbatch.py b/tsshbatch.py index b32a159..122161a 100755 --- a/tsshbatch.py +++ b/tsshbatch.py @@ -20,7 +20,7 @@ CMDINCL = PROGENV + "CMDS" HOSTINCL = PROGENV + "HOSTS" -CVSID = "$Id: tsshbatch.py,v 1.190 2014/11/28 18:07:20 tundra Exp $" +CVSID = "$Id: tsshbatch.py,v 1.191 2014/12/01 19:28:51 tundra Exp $" VERSION = CVSID.split()[2] CPRT = "(c)" DATE = "2011-2014" @@ -211,6 +211,47 @@ ##### +# Check To See If A Key Exists In A String, Excluding Quoted Substrings +##### + +def KeyInString(key, string): + + """ Look for 'key' in 'string', but exclude segments of the string + that are contained within single- or double quotes. + """ + + quote_chars = ('"', "'") + + InLiteral = False + search = "" + index = 0 + while index < len(string): + + char = string[index] + + if InLiteral: + if char == quote_char: + InLiteral = False + + elif char in quote_chars: + quote_char = char + InLiteral = True + + else: + search += char + + index += 1 + + status = False + if search.count(key) > 0: + status = True + + return status + +# End of KeyInString + + +##### # Print Message(s) To stderr ##### @@ -359,14 +400,14 @@ # If this is a sudo run, force password to be read # from stdin thereby avoiding fiddling around with ptys. - if command.count(SUDO + " ") > 0: + if KeyInString(SUDO + " ", command): command = command.replace(SUDO, "%s %s" % (SUDO, SUDOARGS), 1) stdin, stdout, stderr = ssh.exec_command(command) # If doing a sudo command, send the password - if command.count(SUDO + " ") > 0: + if KeyInString(SUDO + " ", command): stdin.write("%s\n" % sudopw) stdin.flush() @@ -780,7 +821,7 @@ SUDOPRESENT = False for command in Commands: - if command.count(SUDO + " ") > 0: + if KeyInString(SUDO + " ", command): SUDOPRESENT = True # Check condition 1) above.