# $language = "Python" # $interface = "1.0" # PingHostFromClipboardOrSelection(LocalShell).py # # Last Modified: # 21 Apr, 2023 # - Support Python 2 or Python 3 as the scripting # engine. # Python 2 (Windows only): # $lanauge = "Python" # Python 3: # $language = "Python3" # - Improve logic to determine when the local shell # instance is ready to receive/handle interactivity. # # 04 Aug, 2020 # - If a "Ping" tab already exists, activate and use # that tab instead of opening a new tab. # - If a tab already exists that was connected with # the saved session named with the value of the # strLocalShellSessName variable, activate and use # that tab instead of opening a new tab. # # 30 Jul, 2020 # - Initial revision # # Description: # This example allows one to use SecureCRT's "Local Shell" # functionality to run a 'ping' command on the value # in the clipboard, selected in the terminal window (if # clipboard is empty), or specified by the individual (if # both clibpoard and selection are either empty or invalid). # # If a tab named "Ping" already is open, then that tab is the # one that will be used for running the 'ping' command. # # If a tab already exists for a session named with the value # of the strLocalShellSessName variable (by default this is # "_Local_Shell_Ping_Session"), then that tab will be used # for running the 'ping' command. # --> If such a tab does not already exist, but the saved # session exists, then that saved session is connected # in a new tab, and that new tab is used for running # the 'ping' command. # # If none of the aforementioned cases are true, this script # will attempt to create a local shell session to be used # for running the ping command. # import re def DoPing(): strPingTabName = "Ping" strLocalShellSessName = "_Local_Shell_Ping_Session_" # Check to see if there already is an existing tab named "Ping" # that we should use when issuing the ping command: bPingTabAlreadyExists = False objLocalShellTab = crt.GetScriptTab() for i in range(1, crt.GetTabCount() + 1): objTab = crt.GetTab(i) if objTab.Caption == strPingTabName: bPingTabAlreadyExists = True objLocalShellTab = objTab break if objTab.Session.Path == strLocalShellSessName: bPingTabAlreadyExists = True objLocalShellTab = objTab break if not bPingTabAlreadyExists: # Make sure we have a saved session for local shell pinging... if not SessionExists(strLocalShellSessName): objConfig = crt.OpenSessionConfiguration("Default") # Save a copy of the Default session to a special # saved session for which we can change the protocol # without having the Default session's protocol changed. objConfig.Save(strLocalShellSessName) try: objConfig = crt.OpenSessionConfiguration( strLocalShellSessName) except: strMsg = ( "Unable to save/open the Local Shell ping " + "session which must exist for this script to work " + "if a 'Ping' tab isn't already open.\r\n" + "\r\n" + "Check to ensure you have write permission for " + "SecureCRT's Config/Sessions folder. Open Global " + "Options and navigate to the Configuration Paths " + "category if you don't know the Config location.") crt.Dialog.MessageBox(strMsg) return # Make sure that the new session we've created has the # Protocol set to "Local Shell" if objConfig.GetOption("Protocol Name") != "Local Shell": try: objConfig.SetOption("Protocol Name", "Local Shell") except: crt.Dialog.MessageBox( "Unable to set protocol to 'Local Shell'. Make sure " + "you're using the current version of SecureCRT.") return objConfig.Save() # If we get to this point, we know that the local shell # session exists, so all we need to do is connect to it (since # the "Ping" tab did not already exist prior) objLocalShellTab = crt.Session.ConnectInTab("/S \"{}\"".format( strLocalShellSessName)) # Rename the tab to "Ping" so it's easy to find by the individual: if objLocalShellTab.Caption != strPingTabName: objLocalShellTab.Caption = strPingTabName else: if not objLocalShellTab.Session.Connected: objLocalShellTab.Session.Connect() # Wait for the local shell to become initialized # sufficiently that we can interact with it. Any # interaction prior to it being ready will potentially # cause the local shell session to become disconnected # as the shell process terminates unexpectedly. objLocalShellTab.Screen.Synchronous = True objLocalShellTab.Screen.IgnoreEscape = True while not objLocalShellTab.Session.Connected: crt.Sleep(25) objLocalShellTab.Screen.Send("\rREADY?", True) objLocalShellTab.Screen.WaitForString("READY?") objLocalShellTab.Screen.Send("\r ", True) objLocalShellTab.Screen.WaitForString(" ") # Wait for the shell prompt to materialize... objLocalShellTab.Screen.Synchronous = False strLastTextLeftOfCursor = "Initial Value, Ignore" while crt.Screen.Get(crt.Screen.CurrentRow, 1, crt.Screen.CurrentRow, crt.Screen.CurrentColumn) != strLastTextLeftOfCursor: strLastTextLeftOfCursor = crt.Screen.Get( crt.Screen.CurrentRow, 1, crt.Screen.CurrentRow, crt.Screen.CurrentColumn) crt.Sleep(250) # Make sure the local shell "Ping" tab is active objLocalShellTab.Activate() # Cancel any prior ongoing 'ping' or other command objLocalShellTab.Screen.Send("\003") # Wait for the shell prompt to materialize... objLocalShellTab.Screen.Synchronous = False strLastTextLeftOfCursor = "Initial Value, Ignore" while crt.Screen.Get(crt.Screen.CurrentRow, 1, crt.Screen.CurrentRow, crt.Screen.CurrentColumn) != strLastTextLeftOfCursor: strLastTextLeftOfCursor = crt.Screen.Get( crt.Screen.CurrentRow, 1, crt.Screen.CurrentRow, crt.Screen.CurrentColumn) crt.Sleep(250) # Send the ping command of whatever is in the clipboard. strTarget = crt.Clipboard.Text if strTarget.strip() == "": # If nothing is in the clipboard, use what's selected in SecureCRT's terminal strTarget = crt.Screen.Selection # If still nothing is selected, prompt with a default value: if strTarget.strip() == "": # Set up a default value... strTarget = "192.168.232.220" strTarget = crt.Dialog.Prompt("What would you like to ping?", "Ping Target?", strTarget) if strTarget.strip() == "": return # Make sure we have semi-legit info sane for pinging... strTarget = strTarget.strip() if not IsValidHostname(strTarget): strTarget = crt.Dialog.Prompt("What would you like to ping?", "Ping Target?", strTarget) # check for cancelation if strTarget == "": return objLocalShellTab.Screen.Send("ping {}\r".format(strTarget)) return def IsValidHostname(strHostname): # If longer than 255, it's not a valid host name, IP addr, or FQDN if len(strHostname) > 255: return False # Just the same, if the length of strHostname is < 1, it's not legit. if len(strHostname) < 1: return False # remove trailing '.' char, if present (allowed, but not used) if strHostname[-1] == ".": strHostname = strHostname[:-1] objRegExp = re.compile("(?!-)[A-Z\d-]{1,63}(?