# $language = "Python" # $interface = "1.0" # SendCommandWithDateTimeSubstitutions.py # # Last Modified: # Oct 2, 2020: # - Initial revision # # Description: # Demonstrates how to import python date/time # substitutions, convert those substitution parameters to # the parameters supported for log file naming (%Y, %M, # %D...) and import those substitution parameters into # the command specified in the Arguments field. # # Note: # While this script can only be used to modify one # "command string" in the arguments field at a time, it # can be used within multiple buttons/commands in # succession for modification of multiple commands import datetime # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def Main(): strCommand = "{Empty - %Y%M%D%h%m%s_%t}" # Here is where we join all commands in the arguments field # that may be separated by spaces so the full command # doesn't need to be quoted if crt.Arguments.Count > 0: strCommand = " ".join(crt.Arguments) # Create a maping between desired substitution params # and params that python's datetime module knows # about... cSubstitutionMappings = { "%Y":"%Y", "%M":"%m", "%D":"%d", "%h":"%H", "%m":"%M", "%s":"%S", "%t":"%f" } # In the default case, given the substitution map above, # a command that uses substitutions a la SecureCRT Log # Filename substitutions would look like this: # scp /filesystem/folder/backup.tar.gz usr@server.example.com:/backups/backup-%Y-%M-%D_%h%m%s_%t.tar.gz # If you want to use native python datetime module # substitutions, uncomment the following line of code: #cSubstitutionMappings = { "%Y":"%Y", "%m":"%m", "%d":"%d", "%H":"%H", "%M":"%M", "%S":"%S", "%f":"%f" } # In the above example, a command that uses native # python datetime module substitutions would look # like this: # scp /filesystem/folder/backup.tar.gz usr@server.example.com:/backups/backup-%Y-%m-%d_%H%M%S_%f.tar.gz # If you want to use your own custom escape/subs, # for example... # \dYear, \dMon, \dDay, \dHour, \dMin, \dSec, \dMsec # ...then uncomment the the following line of code and # modify the \dYear to be what you want the sub code # to look like for the year, etc: #cSubstitutionMappings = { "\dYear":"%Y", "\dMon":"%m", "\dDay":"%d", "\dHour":"%H", "\dMin":"%M", "\dSec":"%S", "\dMsec":"%f" } # In the above example, a command that uses those # substitutions would look like this: # scp /filesystem/folder/backup.tar.gz usr@server.example.com:/backups/backup-\dYear-\dMon-\dDay_\dHour\dMin\dSec_\dMsec.tar.gz for strCmdSub in cSubstitutionMappings.keys(): strPySub = cSubstitutionMappings[strCmdSub] strSubTxt = datetime.datetime.now().strftime(strPySub) # special case the %f since the last 3 digits are always 000 if strPySub == "%f": strSubTxt = strSubTxt[:3] # Sub in the SubTxt into the command string wherever # the CmdSub exists strCommand = strCommand.replace(strCmdSub, strSubTxt) # Want confirmation before sending the command? # Uncomment the following block of code: #if crt.Dialog.MessageBox("Send Command?\r\n\r\n{}".format(strCommand), "Send Command?", 4) <> 6: # return # Now, actually send the command crt.Screen.Send(strCommand + "\r") Main()