Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 04-20-2011, 04:06 AM
hannes.loots hannes.loots is offline
Registered User
 
Join Date: Apr 2011
Posts: 5
Script Required for saving Cisco configurations from already opened telnet tabs

Hi,

I was wondering if any of you already have some script to save configuration via already opened telnet tabs.

I have 10 tabs already open and after I have worked on the routers I would like to make a backup of the configuration using "show run" before I close the connections.

I would like to be able to save the configurations using the tab names as filenames in a directory I specify in runtime.

I would also then want to be able to run a script to apply the configurations from file in a directory I specify in runtime to the already opened tabs of routers and switches.

P.S. Please note that I cannot use tftp as I only have access to the equipment using reverse telnet sessions via the console ports.

Thank you in advance for you help.

Regards,
Hannes
Reply With Quote
  #2  
Old 04-20-2011, 09:39 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi Hannes,

Thanks for the question. Here is an example script that illustrates how to send a command to all open tabs:
Code:
# $language = "VBScript"
# $interface = "1.0"
' SendCommandToAllTabs.vbs
'
' Description:
'   This example script shows how to send the same command to all tabs
'   that currently have an active connection.
' 
' Demonstrates:
'   - How to send a command to all tabs using the tab scripting capability
'     of SecureCRT
'   - How to prompt user for input using the crt.Dialog.Prompt() method.
'   - How to access the total number of Tabs in the SecureCRT window
'     by using the crt.GetTabCount property
'   - How to Activate a tab in the UI from within a script.
'   - How to use the Session.Connected property to determine if a tab
'     currently has an active connection or not.
'   - How to send data to a tab using the screen.Send method in SecureCRT.

Dim g_objTab, g_strSkippedTabs

' Get a reference to the tab that was active when this script was launched.
Set g_objTab = crt.GetScriptTab

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub Main()
   
    ' Ask user what command should be sent to each tabs
    Dim strCommand
    strCommand = crt.Dialog.Prompt("Enter command to be sent to all tabs:", _
                                  "Send To All Connected Tabs", "ls -alR", False)
    if strCommand = "" then exit sub
    
    ' Give the user a chance to review the command and cancel if they notice
    ' something wrong.
    If crt.Dialog.MessageBox(_
        "Are you sure you want to send the following command to " & _
        "__ALL__ tabs?" & vbcrlf & vbcrlf & strCommand, _
        "Send Command To All Tabs - Confirm", _
        vbyesno) <> vbyes then exit sub
    
    ' Activate each tab in order from left to right, and issue the command in
    ' each "connected" tab...
    Dim nIndex
    For nIndex = 1 to crt.GetTabCount
        Set objCurrentTab = crt.GetTab(nIndex)
        objCurrentTab.Activate
        ' Skip tabs that aren't connected
        if objCurrentTab.Session.Connected = True then
            objCurrentTab.Screen.Send strCommand & vbcr
        else
            if g_strSkippedTabs = "" then
                g_strSkippedTabs = nIndex
            else
                g_strSkippedTabs = g_strSkippedTabs & "," & nIndex
            end if
        end if
    Next
    
    ' Now, activate the original tab on which the script was started
    g_objTab.Activate

    ' Determine if there were any skipped tabs, and prepare a message for
    ' displaying at the end.
    if g_strSkippedTabs <> "" then
        g_strSkippedTabs = vbcrlf & vbcrlf & _
            "The following tabs did not receive the command because" & vbcrlf & _
            "they were not connected at the time:" & _
            vbcrlf & vbtab & g_strSkippedTabs
    end if

    crt.Dialog.MessageBox _
        "The following command was sent to all connected tabs:" & vbcrlf & _
        vbtab & strCommand & g_strSkippedTabs

End Sub
You can use the Log or LogUsingSessionOptions methods from the Session object to start logging on each tab prior to sending the command, and you can use the Log method to disable logging after the output has been captured. You can use the ReadString method of the Screen object to capture the data

Does this information help get you started writing your script?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #3  
Old 04-21-2011, 01:03 AM
hannes.loots hannes.loots is offline
Registered User
 
Join Date: Apr 2011
Posts: 5
Thumbs up

I took a first stab at it yesterday and came up with this basic code to save the configurations. Works for me. I still need to find a nice way to save the configurations to the Cisco routers and switches that already is pre-configured. Just copy and paste can do it with some few reboots or a tcl script to change the startup-config file before refor reboot (but that is a discussion for another thread.

Thanks very much for your help.

Oh yeh the code... Not sure i I am cleaning everything out all the objects.
Code:
 
strStartFolder = "C:\Test"

Sub Main()
    Dim nResult
    Dim nCount
    Dim strLogFile
    Dim strCommandResults
    Dim strFolder
    Set objDlg = CreateObject("Shell.Application")
    Set objF = objDlg.BrowseForFolder(&H0, "Select the folder to the configurations to", 0, strStartFolder)
    Set objFolderItem = objF.Self    
    strFolder = objFolderItem.Path 
    for nCount = 1 to crt.GetTabCount()
      Set objTab = crt.GetTab(nCount)
      objTab.Activate
      objTab.Screen.Synchronous = True
      objTab.Screen.send " " & vbcr
      objTab.Screen.WaitForString vbcr
      objTab.Screen.send " " & vbcr
      objTab.Screen.WaitForString vbcr
      nResult = objTab.Screen.WaitForStrings(">","(config","#")
      if nResult = 1 Then
          objTab.Screen.send "enable" & vbcr
          objTab.Screen.WaitForString vbcr
          objTab.Screen.WaitForString "#"
      ElseIf nResult = 2 Then
          objTab.Screen.WaitForString "#"
          objTab.Screen.send "end" & vbcr
          objTab.Screen.WaitForString vbcr
          objTab.Screen.WaitForString "#"
      end if
      objTab.Screen.send "terminal length 0" & vbcr
      objTab.Screen.WaitForString vbcr
      objTab.Screen.WaitForString "#"
      objTab.Screen.send "show run" & vbcr
      objTab.Screen.WaitForString vbcr
      objTab.Screen.WaitForString "bytes" & vbcr
      strCommandResults = ""
      strCommandResults = objTab.Screen.ReadString(objTab.Caption & "#")
      objTab.Screen.send "terminal length 24" & vbcr
      objTab.Screen.WaitForString vbcr
      Set fso = CreateObject("Scripting.FileSystemObject")
      strLogFile = strFolder& "\" & objTab.Caption & ".txt"
      Set objFile = fso.OpenTextFile(strLogFile, 2, True)
      objFile.Write strCommandResults 
      objFile.Close
   Next
   
End Sub
Reply With Quote
  #4  
Old 04-21-2011, 07:41 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi Hannes,

Thanks for the update and posting your working solution. As a note, you may be interested to know that SecureCRT 6.7 has the ability to browse for a folder location in the API.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #5  
Old 04-24-2011, 05:35 AM
hannes.loots hannes.loots is offline
Registered User
 
Join Date: Apr 2011
Posts: 5
Hi,

Thanks for the tip. I will see if I can use it rather.

By the way...

I am getting some strange error every now and then on this code. The screen will not respond and then when I cancle the script the screen will suddenly start rambing on with "^@" and "?" characters that SecureCRT sent to the router instead of the script details. With this I sometimes even get information from the previous tabs capture being sent to the next one.

Example:
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#^@
SW3#?
Exec commands:
access-enable Create a temporary Access-List entry
access-template Create a temporary Access-List entry
archive manage archive files
cd Change current directory
clear Reset functions
clock Manage the system clock
cns CNS agents
configure Enter configuration mode
connect Open a terminal connection

Is there something I need to be looking out for?

Regards,
Hannes
Reply With Quote
  #6  
Old 04-25-2011, 08:33 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi Hannes,

Thanks for the update. What version of SecureCRT are you using when you see the "^@" and "?" characters sent?

When cancelling the script, what line is indicated?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #7  
Old 04-26-2011, 08:02 AM
hannes.loots hannes.loots is offline
Registered User
 
Join Date: Apr 2011
Posts: 5
Hi, I am using the following version of ScureCRT.
Version 6.7.0 (x64 build 110) - Beta Release - March 8, 2011

And it is normally at line 21 int the code.

I have made a few updates to the code to try and stop it.
1) Try to watch for the ^@ characters and exit. It doesn't help at all as the screen does not give the output until the you exit the script and restart the connection.
2) It looks like waiting for vbcrlf and inputting vbcrlf works better than just vbcr. Not sure if that might have been the problem. I am still waiting for the next time it bombs out after this change.
3) I also included "objTab.Screen.Synchronous = False" just before leaving the tab and going to the next. Not sure if it did any good. Not sure what it is for.

Code:
strStartFolder = "C:\Test"

Sub Main()
    Dim nResult
    Dim nCount
    Dim strLogFile
    Dim strCommandResults
    Dim strFolder
    Set objDlg = CreateObject("Shell.Application")
    Set objFolder = objDlg.BrowseForFolder(&H0, "Select the folder to the configurations to", 0, strStartFolder)
    if objFolder Is Nothing Then
        BrowseFolder = ""
        Exit Sub
    End If
    Set objFolderItem = objFolder.Self    
    strFolder = objFolderItem.Path 
    for nCount = 1 to crt.GetTabCount()
      Set objTab = crt.GetTab(nCount)
      objTab.Activate
      objTab.Screen.Synchronous = True
      objTab.Screen.send " " & vbcrlf
      objTab.Screen.WaitForString vbcr
      objTab.Screen.send " " & vbcrlf
      objTab.Screen.WaitForString vbcrlf
      nResult = objTab.Screen.WaitForStrings(">","(config","^@","#")
      if nResult = 1 Then
          objTab.Screen.send "enable" & vbcrlf
          objTab.Screen.WaitForString vbcrlf
          objTab.Screen.WaitForString "#"
      ElseIf nResult = 2 Then
          objTab.Screen.WaitForString "#"
          objTab.Screen.send "end" & vbcrlf
          objTab.Screen.WaitForString vbcrlf
          objTab.Screen.WaitForString "#"
      ElseIf nResult = 3 Then   
          MsgBox "Error Occured!!!!"
          Exit Sub
      end if
      objTab.Screen.send "terminal length 0" & vbcrlf
      objTab.Screen.WaitForString vbcrlf
      objTab.Screen.WaitForString "#"
      objTab.Screen.send "show run" & vbcrlf
      objTab.Screen.WaitForString vbcr
      objTab.Screen.WaitForString "bytes" & vbcrlf
      strCommandResults = ""
      strCommandResults = objTab.Screen.ReadString(objTab.Caption & "#")
      objTab.Screen.send "terminal length 24" & vbcrlf
      objTab.Screen.WaitForString vbcrlf
      objTab.Screen.WaitForString "#"
      objTab.Screen.Synchronous = False
      Set fso = CreateObject("Scripting.FileSystemObject")
      strLogFile = strFolder& "\" & objTab.Caption & ".cfg"
      Set objFile = fso.OpenTextFile(strLogFile, 2, True)
      objFile.Write strCommandResults 
      objFile.Close
   Next
   
End Sub
Reply With Quote
  #8  
Old 04-26-2011, 10:13 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi Hannes,

Thanks for the information. Do you see this problem with a certain device?

It is possible that the remote is not sending the data the script is expecting.
If you record a script (Script / Start Recording Script) capturing you manually performing the tasks, does it differ from the script you are using?

We would recommend using vbcr rather than vbcrlf. Most devices expect a carriage return rather than a carriage return and a line feed.

Can you tell me why you are sending a space followed by a vbcrlf on lines 22 and 24?

To explain, the Synchronous property allows you to ensure that data will not be missed when using the WaitForString or ReadString methods. When Synchronous is set to true, data will not paint to the terminal window until a the string has been found by WaitForString or ReadString. Since you see no data on the screen, it is possible that you are waiting for something that is never appearing.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #9  
Old 04-27-2011, 01:27 AM
hannes.loots hannes.loots is offline
Registered User
 
Join Date: Apr 2011
Posts: 5
Smile

Thanks for all your prompt replies.

I haven't had an issue since the 3 changes (not sure if it was a device or not) and I am using it on a daily basis at the moment. Hope I didn't jinx myslef by writing it in black and white.

The space lines was just to make sure that the sevice is responsive. I could have done a pure VbCr (but I still don't trust it). It just feels that the VBCrLf might have done the trick. Lets put it this way... I am not getting errors at the moment with them in.

I have the load from config done as well. It seems to work. Remember that in a lot of Cisco routers the default is a shutdown interface and if an interface was open in the configurations it would not show "no shutdown" int the config of the interface just "shutdown" in the interface configurations that are shut. I therefore added a few lines to check this and unshut the port if there is no shutdown command under the interface.

I also added a section that you can remove, but it helps me a bit on slower connections.

nLines = nLines + 1
objTab.Screen.send strLine & vbcrlf
if (nLines > 3) then
objTab.Screen.WaitForString vbcrlf
nLines = 0
end if

It is just so that it doesn't wait for feedback after each and every line.

Here is the code for the load config:
Code:
strStartFolder = "C:\Test"

Sub Main()   
    Const ForReading = 1
    Const ForWriting = 2
    Const ForAppending = 8
    
    Dim sFileName
    Dim strLine
    Dim nResult
    Dim nLines
    Dim bShutdown
    Dim bInterface
    sFileName = ""

    Set objDlg = CreateObject("Shell.Application")
    Set objFolder = objDlg.BrowseForFolder(&H0, "Select the folder to the configurations to", 0, strStartFolder)
    if objFolder Is Nothing Then
        BrowseFolder = ""
        Exit Sub
    End If
    Set objFolderItem = objFolder.Self    
    strFolder = objFolderItem.Path 
       
    Set objTab = crt.GetTab(1)
    sConnection = Split(objTab.Session.Path, " ")(0)
    
    for nCount = 1 to crt.GetTabCount()
        Set objTab = crt.GetTab(nCount)
        objTab.Activate
        Set fso = CreateObject("Scripting.FileSystemObject")
        sFileName = strFolder & "\" & objTab.Caption & ".cfg"
        Set objStream = fso.OpenTextFile(sFileName,ForReading, True)
        strLine = ""
        objTab.Screen.send vbcrlf
        objTab.Screen.WaitForString vbcrlf
        objTab.Screen.send vbcrlf
        nResult = objTab.Screen.WaitForStrings(">","#")
        if nResult = 1 Then
            objTab.Screen.send "enable" & vbcrlf
            objTab.Screen.WaitForString vbcrlf
            objTab.Screen.WaitForString "#"
        end if
        objTab.Screen.send "conf t" & vbcrlf
        objTab.Screen.WaitForString vbcrlf
        nLines = 0
        Do While objStream.AtEndOfStream <> True
            strLine = objStream.ReadLine
            If (left(strLine,9) = "interface") Then
                bShutdown = false
                bInterface = true
            ElseIf (right(strLine,8) = "shutdown") Then
                bShutdown = True
            ElseIf  ((strLine = "!")and(bInterface)) then
                bInterface = false
                if (not bShutdown) then 
                    objTab.Screen.send " no shutdown" & vbcrlf
                    objTab.Screen.WaitForString vbcrlf
                end if
            End If 
            nLines = nLines + 1
            objTab.Screen.send strLine & vbcrlf
            if (nLines > 3) then 
              objTab.Screen.WaitForString vbcrlf
              nLines = 0
            end if
        Loop
        objStream.Close
    Next    
End Sub
Reply With Quote
  #10  
Old 04-27-2011, 08:37 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi Hannes,

Thanks for the update. I am glad to hear that the 3 changes you made seem to have resolved the issue.

This just further cements the fact that scripting must be tailored to the remote devices to which one is connecting.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
Reply

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 07:32 PM.