#16
|
|||
|
|||
Hi stuartX,
Thanks for the update. I still think that you would be better off using an example from and the information contained in the scripting manual. I already mentioned the chapters that I think would be most beneficial as you write your script. There is a solution/example that may also be of assistance. It is the solution in chapter 7.2 called Read Data from Separate Files: Hosts, Commands. You could also modify this example to log data using the information in chapter 7.1 Feel free to post any additional questions you might have as you work on your script. Last edited by rtb; 02-21-2012 at 01:31 PM. |
#17
|
|||
|
|||
Ok,
I'm back... I revamped the script by viewing the link you sent me (thanks). It works as far as logging into the device and performing the three commands I have (after I get it fix there may be more commands). And it logs into the file. My "Test-router-1.txt" contains three entries for three routers....shown below: Nexus1;x.x.x.1;Telnet 'router#1 Nexus2;x.x.x.2;Telnet 'router#2 Nexus3;x.x.x.3;Telnet 'router#3 My problem is that the script does the following: login into router#1 skips commands goes to router#2 skips commands goes to router#3 shows first command only, cuts to second command then stops. I think that I don't have enough delay time in for it to perform and complete the commands. Below is the entire script. I have a few lines commented out to get me to this point. Below is my full code: Code:
# $language = "VBScript" # $interface = "1.0" Sub Main Const username = "userx" ' Username to use for login Const password = "passx" ' Password for corresponding user Const loginPass = "admin" ' Password to use for password only - no TACACSnew-model Const DEVICE_FILE_PATH = "s:\riggs\Test-router-1.txt" Dim fso Set fso = CreateObject("Scripting.FileSystemObject") Dim fil Set fil = fso.OpenTextFile(DEVICE_FILE_PATH) Dim ip Dim name Dim protocol Dim line Dim cnxnString 'File Reads @ stored location While Not fil.AtEndOfStream line = fil.ReadLine name = Split(line, ";")(0) ip = Split(line, ";")(1) protocol = Split(line, ";")(2) 'Protocol Selection Select Case protocol Case "Telnet" cnxnString = "/TELNET " & ip & " 23" Case "SSH2" cnxnString = "/SSH2 /L " & username & " /PASSWORD " & password & " /C 3DES /M MD5 " & ip Case "SSH1" cnxnString = "/SSH1 /L " & username & " /PASSWORD " & password & " /C 3DES /M MD5 " & ip End Select ' Connect crt.Screen.Synchronous = True 'Error on error resume next crt.Session.Connect cnxnString If protocol = "Telnet" Then Dim index index = crt.Screen.WaitForStrings("login:", "Password:") If index = 1 Then crt.Screen.Send username & vbCr'crt.Screen.Send username & vbCr crt.Screen.WaitForString "Password:" crt.Screen.Send password & vbCr crt.Screen.WaitForString "#" 'Take about page pauses crt.Screen.Send "term length 0"& vbCr 'Send CPU command crt.Screen.Send "show proc cpu | inc CPU"& vbCr 'Wait for unique character crt.Screen.WaitForString "#" 'send show version information crt.Screen.Send "show version"& vbCr Else If index = 2 then crt.Screen.Send vbCr 'crt.Screen.WaitForString "Password:" crt.Screen.Send vbCr crt.Screen.WaitForString "#" crt.Screen.Send "show users"& vbCr 'crt.Session.Log False End If End If 'Start to collect information in log-file 'crt.Screen.WaitForString "linux$", 1 Dim logfile logfile = "S:\Riggs\" & ip &"-Nexus-config" & ".log" crt.Session.LogFileName = logfile crt.Session.Log True ' Enable 'crt.Screen.WaitForString ">" 'crt.Screen.Send "show inventory"& vbCr 'crt.Screen.WaitForString ">" 'crt.Screen.Send "show snmp status"& vbCr 'crt.Screen.WaitForString ">" 'crt.Screen.Send "show snmp accexx"& vbCr ' Save 'Close log file crt.Session.Log False crt.Screen.Synchronous = False 'Close out session crt.Session.Disconnect end if wend fil.Close End Sub |
#18
|
|||
|
|||
Hi stuartX,
Thanks for posting your script that works, but doesn't. I have found a few issues that may be the cause of the issue you are currently seeing. Here is a list:
Last edited by rtb; 02-22-2012 at 09:57 AM. |
#19
|
|||
|
|||
Mr. Todd,
It works!! Thanks for you help. But I can not make the recommended "IF, Else" changes. But I did make the error exception change. Below is the working code. I tried to clean it up as much as possible. I may have gotten some indentions wrong. Thank you Sir, much appreciated. Code:
# $language = "VBScript" # $interface = "1.0" Sub Main Const username = "userX" ' Username to use for login Const password = "passX" ' Password for corresponding user 'Const enablePass = "admin" ' Password to use for enable password - TACACSnew-model Const DEVICE_FILE_PATH = "s:\riggs\Test-router-1.txt" Dim fso Set fso = CreateObject("Scripting.FileSystemObject") Dim fil Set fil = fso.OpenTextFile(DEVICE_FILE_PATH) Dim ip Dim name Dim protocol Dim line Dim cnxnString 'File Reads @ stored location While Not fil.AtEndOfStream line = fil.ReadLine name = Split(line, ";")(0) ip = Split(line, ";")(1) protocol = Split(line, ";")(2) 'Protocol Selection Select Case protocol Case "Telnet" cnxnString = "/TELNET " & ip & " 23" Case "SSH2" cnxnString = "/SSH2 /L " & username & " /PASSWORD " & password & " /C 3DES /M MD5 " & ip Case "SSH1" cnxnString = "/SSH1 /L " & username & " /PASSWORD " & password & " /C 3DES /M MD5 " & ip End Select ' Connect crt.Screen.Synchronous = True 'Error Exception Information on error resume next crt.Session.Connect cnxnString strErr = Err.Description nError = Err.Number On Error Goto 0 If protocol = "Telnet" Then Dim index index = crt.Screen.WaitForStrings("login:", "Password:") If index = 1 Then crt.Screen.Send username & vbCr'crt.Screen.Send username & vbCr crt.Screen.WaitForString "Password:" crt.Screen.Send password & vbCr crt.Screen.WaitForString "#" 'Start to collect information in log-file Dim logfile logfile = "S:\Riggs\" & ip &"-Nexus-config" & ".txt" crt.Session.LogFileName = logfile crt.Session.Log True 'Take about page pauses crt.Screen.Send "term length 0"& vbCr crt.Screen.WaitForString "linux$", 3 'Send CPU command crt.Screen.Send "show proc cpu | inc CPU"& vbCr crt.Screen.WaitForString "linux$", 1 crt.Screen.WaitForString "#" 'send show version information crt.Screen.Send "show version"& vbCr crt.Screen.WaitForString "linux$", 3 Else If index = 2 then crt.Screen.Send vbCr 'crt.Screen.WaitForString "Password:" crt.Screen.Send vbCr crt.Screen.WaitForString "#" End If End If crt.Screen.WaitForString "linux$", 1 'Close log file crt.Session.Log False crt.Screen.Synchronous = False 'Close out session crt.Session.Disconnect end if wend fil.Close End Sub |
#20
|
|||
|
|||
Hi stuartX,
Thanks for the update. I am glad to hear that you have the script working. I do still see a couple of potential problem areas. They are associated with how you have the script performing authentication. If you get to the point that you see problems, or you want to revisit trying to get the authentication code removed from the command code, please post the error and the current iteration of your script. I am happy to help. |
#21
|
|||
|
|||
Thanks Todd. I appreciate all your help. This has been a marathon (for me) to get to the working point.
By the way, I would like to streamline the authentication as you mention. But when I changed the IF Else statements, I got errors. So, I would welcome your expertise. Also, I did find the option to append to a file with the same name with a script writing to the same file. The second "true" turns on append. That is done by the following line command: Code:
crt.Session.Log True, true Code:
logfile = "S:\Riggs\" & HosName &"%H-%Y-%M-%D--%h-%m-%s.%t.txt" |
#22
|
|||
|
|||
Thanks for posting the additional information stuartX.
I have gone through the script that you posted, and made some changes to it. Here it is: Code:
Sub Main strUsername = "user" strPassword = "password" strPrompt = "#" Set fso = CreateObject("Scripting.FileSystemObject") strDeviceList = "C:\Switches\devices.txt" Set fil = fso.OpenTextFile(strDeviceList) ' Start loop to iterate over all devices defined in file While Not fil.AtEndOfStream ' Process a line at a time vLine = Split(fil.ReadLine, ";") strHostname = vLine(0) strIP = vLine(1) strProtocol = vLine(2) ' Generate a connection string Select Case strProtocol Case "Telnet" cnxnString = "/TELNET " & strIP & " 23" ' Don't wait for connection to complete before moving on because ' we are going to handle authentication after connecting. bWait = False Case "SSH2" cnxnString = "/SSH2 /L " & strUsername & " /PASSWORD " & strPassword & " " & strIP ' Wait for connection to complete before moving on. bWait = True Case "SSH1" cnxnString = "/SSH1 /L " & strUsername & " /PASSWORD " & strPassword & " " & strIP ' Wait for connection to complete before moving on. bWait = True End Select 'Handle possible errors when connecting On Error Resume Next crt.Session.Connect cnxnString, bWait strErr = Err.Description nError = Err.Number On Error Goto 0 ' Enable Synchronous which is necessary for not missing data from remote crt.Screen.Synchronous = True ' Handle Telnet authentication If strProtocol = "Telnet" Then nIndex = crt.Screen.WaitForStrings("login:", "Password:") ' Handle authentication starting at username If nIndex = 1 Then crt.Screen.Send strUsername & vbcr crt.Screen.WaitForString "Password:" crt.Screen.Send strPassword & vbcr ' Handle authentication starting at password Else crt.Screen.Send strPassword & vbcr End If End If ' Confirm that remote is ready crt.Screen.WaitForString strPrompt ' Start logging to new file strLogFile = "C:\Switches\" & strHostname & "-%Y-%M-%D--%h-%m-%s.%t.txt" crt.Session.LogFileName = strLogFile crt.Session.Log True ' Turn off paging crt.Screen.Send "term length 0" & vbcr crt.Screen.WaitForString strPrompt ' Send commands crt.Screen.Send "show proc cpu | inc CPU" & vbcr crt.Screen.WaitForString strPrompt crt.Screen.Send "show version" & vbcr crt.Screen.WaitForString strPrompt ' Stop logging crt.Session.Log False ' Disconnect from device If crt.Session.Connected Then crt.Session.Disconnect End If Wend ' Close device file fil.Close End Sub If you decide to try it, and it fails, let me know what error and line number you see. If the script is hanging, then I would need to know on what line the script cancels. |
#23
|
|||
|
|||
Todd,
I loaded the script and ran. It hung, and when I hit Cancel, it reported error script cancelled @ line 46. |
#24
|
|||
|
|||
Hi stuartX,
Thanks for the information. What do you see in the SecureCRT terminal when the script is hanging? Would you be able to post or send a screenshot, or copy the data out of the terminal and post or send it? |
#25
|
|||
|
|||
well, when I started the script, the CRT window was blank. it did not prompt or log into a device at all. There was nothing in the window. When I cancel the script, thats when it came up with the line 46. But it did not initiate a session.
|
#26
|
|||
|
|||
the screeshot is attached
|
#27
|
|||
|
|||
Hi stuartX,
Thanks for the information. If you are establishing a Telnet connection, then it may be necessary to send a carriage return to get the authentication process started. What happens if you modify the following code: Code:
If strProtocol = "Telnet" Then nIndex = crt.Screen.WaitForStrings("login:", "Password:") Code:
If strProtocol = "Telnet" Then crt.Screen.Send vbcr nIndex = crt.Screen.WaitForStrings("login:", "Password:") |
#28
|
|||
|
|||
That just it, there is nothing that pops up on the screen (lol).
When I start the script, before (with working/non-efficient script) I would get the login prompt. But that doesn't come up, the window is blank when the script starts. When I turned on Trace, it does not copy anything to the window. I attached the error dialog box (line 46).when I cancelled. |
#29
|
|||
|
|||
Hi stuartX,
It seems like we need to know what SecureCRT is trying to use for the connection. Would you make the following modification and let me know what the message box says (this is only if you have the time, of course)? Code:
crt.Dialog.MessageBox "Connect string:" & vbcrlf & vbtab & cnxcString &_ vbcrlf & "Should I wait:" & vbcrlf & vbtab & bWait On Error Resume Next crt.Session.Connect cnxnString, bWait strErr = Err.Description nError = Err.Number On Error Goto 0 |
#30
|
|||
|
|||
I'm sorry for the delay, had to deal with a network issue on Friday.
I inserted the additional script and I attached errors. The first was the attached ErrorMod-1.jpg and the second was the attached ErrorMod-2.jpg. |
![]() |
Thread Tools | |
Display Modes | Rate This Thread |
|
|