diff --git a/tsshbatch.py b/tsshbatch.py index a4c4e8a..49538a2 100755 --- a/tsshbatch.py +++ b/tsshbatch.py @@ -20,7 +20,7 @@ CMDINCL = PROGENV + "CMDS" HOSTINCL = PROGENV + "HOSTS" -CVSID = "$Id: tsshbatch.py,v 1.193 2014/12/02 20:46:41 tundra Exp $" +CVSID = "$Id: tsshbatch.py,v 1.194 2014/12/03 19:12:07 tundra Exp $" VERSION = CVSID.split()[2] CPRT = "(c)" DATE = "2011-2014" @@ -123,14 +123,18 @@ ##### -# Hostname substitution support +# Builtin Symbols ##### - HOSTNAME = '__HOSTNAME__' +HOSTNUM = '__HOSTNUM__' HOSTSHORT = '__HOSTSHORT__' -HostName = "" -HostShort = "" + +BuiltIns = (HOSTNAME, HOSTNUM, HOSTSHORT) + +##### +# Global Symbol Table +##### SymbolTable = {} @@ -321,23 +325,14 @@ srcfile = VarSub(src) - # Process any HOSTNAME, HOSTSHORT substitutions - - srcfile = srcfile.replace(HOSTNAME, HostName) - srcfile = srcfile.replace(HOSTSHORT, HostShort) - for destdir in filelist[src]: # Process any .define substitutions destdir = VarSub(destdir) - # Process any HOSTNAME, HOSTSHORT substitutions - - destdir = destdir.replace(HOSTNAME, HostName) - destdir = destdir.replace(HOSTSHORT, HostShort) - # Make sure we have a trailing path separator + destination = destdir if destination[-1] != PATHSEP: destination += PATHSEP @@ -394,10 +389,9 @@ for command in commands: - # Resolve references to hostname within the command string + # Dereference variables - command = command.replace(HOSTNAME, HostName) - command = command.replace(HOSTSHORT, HostShort) + command = VarSub(command) # It's possible to get blank lines from stdin. # Ignore them. @@ -563,7 +557,7 @@ # It's a normal line - do variable substitution and save else: - listcontainer.append(VarSub(line)) + listcontainer.append(line) f.close() FileIncludeStack.pop() # Remove this invocation from the stack @@ -769,7 +763,7 @@ if command: # Do variable substitution here like any other command - Commands.append(VarSub(command)) + Commands.append(command) ##### # Authentication Credential Processing @@ -892,10 +886,36 @@ else : + # Check to see if user is trying to override any builtins + + protected = [] + for builtin in BuiltIns: + if builtin in SymbolTable: + protected.append(builtin) + + # Now iterate over requested hosts + hostnum = 0 for host in Hosts: - HostName = host - HostShort = host.split('.')[0] + # Update the host counter + + hostnum += 1 + + # Add internally generated symbols to the symbol table. + # That way, both user-defined and builtin symbols will + # subsequently be substituted. + + internals = [(HOSTNAME, host), + (HOSTNUM, str(hostnum)), + (HOSTSHORT, host.split('.')[0]), + ] + + # Install builtins in the symbol table but only if the + # user isn't overriding them. + + for symbol, value in internals: + if symbol not in protected: + SymbolTable[symbol] = value if Get_Transfer_List: HostFileTransfer(host, UNAME, PWORD, Get_Transfer_List, GET=True)