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
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?
#$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
Set g_objTab = crt.GetScriptTab
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
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",_
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
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.Send username & chr(13)
crt.Screen.Send pass & chr(13)
Case 3 ' RSA key already learned
crt.Screen.Send pass & chr(13) ' just send pass from credentials file
crt.Screen.WaitForString ">" ' Waits for prompt
crt.Screen.Send "enable" & chr(13) ' Enable
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"
SendExpect "show running-config", hostname + "#"
' 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
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
SendExpect = True