VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Script to Backup running config to local PC (https://forums.vandyke.com/showthread.php?t=13398)

stamati76 02-10-2019 07:08 AM

Script to Backup running config to local PC
 
I have very little experience with scripting and i was wondering if anyone could help me run a script that pulled the running configuration from all my devices in session manager?

I have run the following script that hangs on line 25. Basically, it's looking for the devices file, which i have not yet written :p

How could i change it to look through my session manager or export all the devices from session manager to a readable file for the vbs script to read?

Code:

#$language = "VBScript"
#$interface = "1.0"

' Test Script to log in via SSH/telnet and enters in privileged mode
' IF enable password is not set or you connect with TACACS/Radius directly in privileged mode,
' then comment the lines after Loop until the next comment section
' This version uses only one "devices.txt" file to store IP Address;hostname;username;password;enable password

Dim g_objTab
Set g_objTab = crt.GetScriptTab

Sub Main
        CRT.Screen.synchronous = True
        If crt.Session.Logging Then                                                                ' Turn off session logging before setting up our script's
          crt.Session.Log False                                                                        ' logging... if needed
        End If
                Dim fso                                                                                                        ' Declaring filesystemobject
                Dim fil                                                                                                        ' Declaring file containing devices
                Dim vWaitFors                                                                                        ' Array to determine if RSA key is known, also to determine if SSH or Telnet is enabled
                vWaitFors = Array("(yes/no)?",_
                                                        "port 22: Connection refused",_
                                                        "assword:")
                Const DEVICE_FILE_PATH = ".\devices.txt"                                ' Path to devices list (can be modified, now it points to same folder where script is located)
                Set fso = CreateObject("Scripting.FileSystemObject")
                Set fil = fso.OpenTextFile(DEVICE_FILE_PATH)                        ' Open devices file
        While Not fil.AtEndOfStream                                                                        ' Read devices.txt. Until not EOF do :
                line = fil.ReadLine                                                                                ' Read line by line
                Device = Split(line, ";")(0)                                                        ' Device's IP address
                hostname = Split(line,";")(1)                                                        ' hostname - for easy human read
                username = Split(line,";")(2)                                                        ' username to connect to device
                pass = Split(line,";")(3)                                                                ' password for username
                pass2 = Split(line,";")(4)                                                                ' password to enter privileged mode if needed
                crt.Screen.Send "ssh " + username + "@" + Device & chr(13)        ' First try to connect via SSH to device
                Do
                Dim nResult       
                nResult = crt.Screen.WaitForStrings(vWaitFors)
                Select Case nResult                                                                                ' Now determine if RSA key has to be added or not in SSH case or try via Telnet
                        Case 1                                                                                                ' RSA key for /.ssh/known_hosts must be added
                crt.Screen.Send "yes" & chr(13)                                                        ' so send and "yes"
                crt.Screen.WaitForString "assword:"                                                ' then wait for password prompt
                crt.Screen.Send pass & chr(13)                                                        ' and send pass
                        Case 2                                                                                                ' SSH not enabled on device, then go for telnet
                crt.Screen.Send "telnet " + Device & chr(13)
                crt.Screen.WaitForString "sername:"
                crt.Screen.Send username & chr(13)
                crt.Screen.WaitForString "assword:"
                crt.Screen.Send pass & chr(13)
                        Case 3                                                                                                ' RSA key already learned
                crt.Screen.Send pass & chr(13)                                                        ' just send pass from credentials file
                End Select
                Exit Do
                Loop
                crt.Screen.WaitForString ">"                                                        ' Waits for prompt
                crt.Screen.Send "enable" & chr(13)                                                ' Enable
                crt.Screen.WaitForString "assword:"
                crt.Screen.Send pass2 & chr(13)                                                        ' sends enable pass
                crt.Screen.WaitForString "#"                                                        ' waits for privileged mode prompt
                ' From now on, just put whatever commands you need for your device
                ' use crt.Screen.Send "command" & chr(13) --- to send what you want, chr(13) stands for ENTER key
                ' use after each command an crt.Screen.WaitForString "#" -- to wait for prompt after command's output
                ' Or even better just use SendExpect "command", "prompt" --- see below function
                SendExpect "terminal length 0", hostname + "#"
                crt.Session.logFileName = ".\" + hostname + ".txt"
                crt.Session.Log True
                SendExpect "show running-config", hostname + "#"
                crt.Session.Log False
                ' Commands sent/received output ends here. Next is disconnecting from device       
                SendExpect "exit", "]$ "                                                                ' Closes connection & Waits for Terminal Server prompt and is ready for next device
        Wend       
        fil.Close                                                                                                        ' Close devices file
End Sub                                                                                                                        ' THE END OF THE UNIVERSE AS YOU KNEW IT:)
Function SendExpect(szSend, szExpect)                                                        ' Made this to replace the Screen.Send/Screen.WaitForString pairs
        ' Returns true if the text in 'szSend' was successfully sent and
        ' the text in 'szExpect' was successfully found as a result.
    ' If we're not connected, we can't possibly return true, or even send/recv text
   
        if g_objTab.Session.Connected <> True then exit function
       
    g_objTab.Screen.Send szSend & vbcr
    g_objTab.Screen.WaitForString szExpect

    SendExpect = True
End Function


bgagnon 02-10-2019 06:08 PM

Hi stamati76,

Presently there's not an easy way to iterate over the sessions in Session Manager. I have added this thread to a feature request in our product enhancement database to add Sessions object to scripting API for iterating over all sessions in Session Manager. Should a future release of SecureCRT include this feature, notification will be posted here.

If you prefer direct email notification, send an email to support@vandyke.com and include "Feature Request - Forum Thread #13398" in the subject line or use this form from the support page of our website.

There may be a way you can export the connection data. Please contact us via email (support@vandyke.com) and reference "Attn Brenda - Forum Thread #13398" in the subject line.

In your email please let me know the version of SecureCRT and what OS you are running it on.


All times are GMT -6. The time now is 05:12 AM.