Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 05-04-2016, 02:43 PM
Rhudi's Avatar
Rhudi Rhudi is offline
Registered User
 
Join Date: Jul 2013
Location: Greenville, SC
Posts: 80
Locate and start an existing session?

I have a particular structure where I store my session files.
Within:
%USDERPROFILE%\AppData\Roaming\VanDyke\Config\Sessions
I have a series of folders by state.
Within each state, I have a series of folders by City.
In those are my session files.

When I use:
crt.Session.ConnectInTab("/S " & sHostName)
It does not recursively find the actual path to my session file.

How do I locate the full relative path to a session file?
Reply With Quote
  #2  
Old 05-04-2016, 04:02 PM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 931
/S isn't recursive... you have to specify the actual Session Path to the session you wish to have SecureCRT connect.

The scripting guide shows you how to accomplish connections with /S in chapter 3 (specifically in section 3.1, there's even a screen shot showing the session tree and an example crt.Session.Connect("/S <session_path>") line of code underneath it).

--Jake
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
Reply With Quote
  #3  
Old 05-05-2016, 03:37 AM
Rhudi's Avatar
Rhudi Rhudi is offline
Registered User
 
Join Date: Jul 2013
Location: Greenville, SC
Posts: 80
That example seems to only be useful when I know the path. My environment is dynamic. However, I did port some code from an excel vba I use.

Follows are two sections of code:

Code:
Sub DoRouterConnect(sSessionName)
Dim shell, env
Dim sSessionPath, nLoopSanityCount
    Set shell = CreateObject("WScript.Shell")
    Set env = shell.Environment("process")
    Set objCurrentTab = crt.GetScriptTab
    sScriptTab = objCurrentTab.Caption
    nLoopSanityCount = 0
    sSessionPath = env("AppData") & "\VanDyke\Config\Sessions\"
    Do
        Call Recurse(sSessionPath, sSessionName & ".ini")
        Select Case sSecureCRTSessionFile
            Case ""
'                crt.Dialog.MessageBox sSessionName & " doesn't exist!"
                sSomethingFailedText = sSessionName & " doesn't exist!"
                bSomethingFailed = True
                Exit Do
            Case Else
                sSecureCRTSessionFile = Replace(sSecureCRTSessionFile, sSessionPath, "")
                crt.Session.ConnectInTab("/S " & sSecureCRTSessionFile)
                If crt.Session.Connected Then
                    sSecureCRTSessionFile = ""
                    Exit do
                End If
                If nLoopSanityCount > 3 Then
                    crt.Dialog.MessageBox "GIVE UP."
                    Exit Do
                End If
        End Select
    Loop Until crt.Session.Connected = True
End Sub
It is important to note that "sSecureCRTSessionFile" and sSomethingFailedText are Dim'd before the main sub (so they're 'Global' to all subs)

The following function does the recursive search:
Code:
Function Recurse(sPath, sFile)
Dim myFolder, mySubFolder, myFile
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set myFolder = objFSO.GetFolder(sPath)
    For Each mySubFolder In myFolder.SubFolders
        For Each myFile In mySubFolder.Files
            If UCase(myFile.Name) = UCase(sFile) Then
                sSecureCRTSessionFile = myFile.Path
                sSecureCRTSessionFile = Replace(sSecureCRTSessionFile, ".ini", "")
                sSecureCRTSessionFile = Chr(34) & sSecureCRTSessionFile & Chr(34)
                Exit For
            End If
        Next
        Recurse = Recurse(mySubFolder.Path, sFile)
    Next
End Function
So, when I call DoRouterConnect, I pass it a valid session name. When the recursive function runs, if it finds the session, it will set the Global variable and exit.

I've tested it.

I'm using this as part of a control routine that will read a text file that contains a list of sessions. I need to make this verify that all sessions exist before it tries to connect to any.

The sSomethingFailedText is how that main routine tells me what failed.
Reply With Quote
  #4  
Old 05-05-2016, 09:16 AM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 931
SecureCRT allows you to have duplicate/identical session names in separate folders.

You solution assumes that there will never be any duplication of any session names in different folders. If this assumption holds true, then you're good to go.

If there are times where (in your dynamic environment) you have session names that might be the same but in different folders (like two sessions named "Router 1" in different folder locations), you'll need to specify the actual session path as it is explained in the scripting guide.

FYI.
--Jake
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
Reply With Quote
  #5  
Old 05-05-2016, 11:38 AM
Rhudi's Avatar
Rhudi Rhudi is offline
Registered User
 
Join Date: Jul 2013
Location: Greenville, SC
Posts: 80
In my environment, the session names must match the router's assigned CLLI or the host part of a FQDN.

I'm planning on adding a parsing option that will detect if I've sent an IP or a hostname in case I don't have a matching name.

I realised a long time ago that the sessions can have duplicate names and decided that was just bad juju.

Thanks tho! If anyone can use my code, please do (or I wouldn't have posted it )!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 02:18 PM.