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 07-26-2012, 11:12 AM
tpolimeni tpolimeni is offline
Registered User
 
Join Date: Jul 2012
Posts: 3
New to Scripting

Hi All,
I am trying to setup a or creat a script that will assist me with backing up and dropping the same config on about 1800 switches. I have a list of switch IP addresses in a text file and would like that script to look at that file log into each switch drop the config write it to mem and then back up each switch/router config into its own file using Hostname MM DD YY format. The config temp is also saved to a text file as well is there away to make the script look at that file and push the config to each box?

Only thing is to connect to the switches they are on a private network and you can only connect to them after you log into a ssh jumpbox running SSH2 and telnet from that box to each switch. I would like the script to log into each switch one after another. I was looking for examples on the site and I was able to find anything that would do this. I also tried to record function for a script but I can not figure out how to get that log into each switch and backup each switches config one by one.

Thanks,
Tim
Reply With Quote
  #2  
Old 07-26-2012, 01:04 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
I think the SecureCRT Scripting Manual will be useful for you before your project is complete.

Which version of SecureCRT are you using (Help / About SecureCRT)?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #3  
Old 07-26-2012, 01:05 PM
tpolimeni tpolimeni is offline
Registered User
 
Join Date: Jul 2012
Posts: 3
I am using CRT 6.7 currently
Reply With Quote
  #4  
Old 07-26-2012, 02:06 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Good! There should be a folder named Scripts in the SecureCRT installation folder. Inside that folder is a script named ConnectToMultipleSessionsAndSendCommands.vbs.

It reads sessions from a file... are your switches defined in sessions already? Or, do you need to make ad hoc connections?

For anyone using an older version which didn't install the script, here it is:
Code:
#$language = "VBScript"
#$interface = "1.0"
' ConnectToMultipleSessionsAndSendCommands.vbs
'
' Description:
'     Reads in sessions from a file (one session per line) and
'     connects to each one (one at a time) and sends a series of
'     commands to the remote, then disconnects from the session
'     and moves on to the next session.
'     
'     By default, the file containing the sessions is expected to be named
'     "SessionList.txt", and should be located in the current user's "My
'     Documents" folder.  Each line of the file is expected to contain the
'     path to the session as it appears within the Connect dialog (excluding
'     the leading "Sessions/" component.  Here is an example file contents:
'         
'         redhat
'         redhat8
'         192.168.0.123
'         192.168.0.131
'         Redhat Sessions\RedHat - XTerm
'
'     For the sake of simplicity, this example assumes that all
'     sessions we're connecting to are using the SSH1 and SSH2
'     protocols, with usernames and passwords saved encrypted
'     as part of the session configuration so that we don't need
'     to worry about authentication within this script.
'
' This example demonstrates:
'     - How to use the Windows FileSystemObject to load information from
'       a file.
'     - How to determine location of current user's "My Documents" folder
'       by using WScript's Shell.SpecialFolders() method
'     - How to use a VBScript dynamic array
'     - How to connect to a remote machine using an existing session

Dim g_szErrors

Sub Main()

    Dim objFso, objShell, objTextStream, szLine
    Dim vSessionsArray()
    Dim nSessionCount
    nSessionCount = 0

    ' Start of with a reasonable size for the array
    ReDim vSessionsArray(5)

    ' Read in Sessions from a file that contains session names (paths as they
    ' appear in the Connect dialog), one per line
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set objShell = CreateObject("WScript.Shell")
    Dim szMyDocs, szSessionsFile
    szMyDocs = objShell.SpecialFolders("MyDocuments")
    
    szSessionsFile = szMyDocs & "\SessionList.txt"
    if Not objFso.FileExists(szSessionsFile) then
        crt.Dialog.MessageBox "Session list file not found:" & vbcrlf & _
            vbcrlf & _
            szSessionsFile & vbcrlf & vbcrlf & _
            "Create a session list file as described in the description of " & _
            "this script code and then run the script again."
        exit sub
    end if
    
    Set objTextStream = objFso.OpenTextFile(szSessionsFile, 1, false)

    Dim szSession
    Do While Not objTextStream.AtEndOfStream
        szSession = ""
        szSession = Trim(objTextStream.ReadLine)
        
        ' Don't add empty lines/sessions
        if szSession <> "" then
            ' Check to see if our current array is full. If it's full, we'll
            ' allocate enough additional room in the array to store 5 more
            ' elements
            if UBound(vSessionsArray) >= nSessionCount then
                redim preserve vSessionsArray(nSessionCount + 5)
            end if
            
            ' Now that we're assured there's enough room to store the session
            ' name/path we've read in, we'll store it in the array and increase
            ' our counter variable.
            vSessionsArray(nSessionCount) = szSession
            nSessionCount = nSessionCount + 1
        end if
    Loop

    objTextStream.Close

    ' Connect to each session and issue a few commands, then disconnect.
    For Each szSession in vSessionsArray
        if szSession = "" then exit for
        
        'MsgBox "Connecting to Session: " & szSession
        On Error Resume Next
        crt.Session.Connect("/S " & chr(34) & szSession & chr(34))
        szError = Err.Description
        On Error Goto 0
        
        ' If we successfully connected, we'll do the work we intend to do...
        ' otherwise, we'll skip the work and move on to the next session in 
        ' the list.
        if crt.Session.Connected then
            crt.Screen.Synchronous = True
            
            ' When we first connect, there will likely be data arriving from the
            ' remote system.  This is one way of detecting when it's safe to
            ' start sending data.
            Do
                bCursorMoved = crt.Screen.WaitForCursor(1)
            Loop until bCursorMoved = False
            ' Once the cursor has stopped moving for about a second, we'll
            ' assume it's safe to start interacting with the remote system.
            
            ' Get the shell prompt so that we can know what to look for when
            ' determining if the command is completed. Won't work if the prompt
            ' is dynamic (e.g. changes according to current working folder, etc)
            nRow = crt.Screen.CurrentRow
            szPrompt = crt.screen.Get(nRow, _
                                      0, _
                                      nRow, _
                                      crt.Screen.CurrentColumn - 1)
            szPrompt = Trim(szPrompt)
            
            crt.Screen.Send "ls -l" & vbcr
            ' Wait for the command to complete, by looking for the prompt to
            ' appear once again.
            crt.Screen.WaitForString szPrompt

            crt.Screen.Send "pwd" & vbcr
            ' Wait for the command to complete, by looking for the prompt to
            ' appear once again.
            crt.Screen.WaitForString szPrompt

            crt.Screen.Send "who" & vbcr
            ' Wait for the command to complete, by looking for the prompt to
            ' appear once again.
            crt.Screen.WaitForString szPrompt

            ' Now disconnect from the remote machine...
            crt.session.disconnect
            ' Wait for the connection to close
            Do
                crt.Sleep 100
            Loop While crt.Session.Connected = True
            
            crt.Sleep 1000
        else
            g_szErrors = g_szErrors & vbcrlf & _
                "*** Error connecting to " & szSession & ": " & szError
        end if
    Next
    
    if g_szErrors = "" then
        crt.Dialog.MessageBox _
            "Tasks completed.  No Errors were detected."
    else
        crt.Dialog.MessageBox _
            "Tasks completed.  The following errors occurred:" & _
            vbcrlf & g_szErrors
    end if
    
    crt.Quit
    
End Sub
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #5  
Old 07-26-2012, 02:11 PM
tpolimeni tpolimeni is offline
Registered User
 
Join Date: Jul 2012
Posts: 3
Thanks for all the help. The sessions are not predefined since I have to SSH2 to a linux jumbox and then from the jump box telnet to the switches/routers.
Reply With Quote
  #6  
Old 07-26-2012, 03:25 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Thanks Tim,

Makes sense, but I wanted to be certain (in case automate logon was enabled in all sessions, or something like that).

I think the example script will still be your best starting point. You can modify the script to use the "/SSH2" option instead of "/S".

So line 98 would be:
crt.Session.Connect("/SSH2 " & chr(34) & szSession & chr(34))
instead of
crt.Session.Connect("/S " & chr(34) & szSession & chr(34))
As kind of an overview of how I see your project and where I think we're headed, connecting to each host is one part of the script. Once that's working, we can look at another part of the script - sending the commands. Finally, you'll set up the log file but that can probably be last.
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
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 01:22 AM.