|
#1
|
|||
|
|||
![]()
Any help guys?
My script actually is not mine. It does simply telnet multiple devices then issue commands. I tried to modify it that if telnet fails it should try to use ssh instead by if else and waitforstring However from our Jumpserver, when the telnet fails like this below (simulate): 10.10.10.10 only allows ssh, it should try to waitforstring and then if the condition is true(login prompt) use ssh instead if > = false means it was able to go to the router privilege mode Trying 10.10.10.10... telnet: connect to address 10.10.10.10: Connection refused telnet: Unable to connect to remote host: Connection refused JUMPSERVER# All the syntax below are not being evaluated after the failed telnet I tried to verify and just send a string after the failed connection and it doesn't work. So there must be some additional lines to add to the telnet connection if it fails. Am i missing something here? I need to make the If then statement work if the telnet fails to use ssh instead. Attaching the script: Code:
#$language = "VBScript" #$interface = "1.0" crt.Screen.Synchronous = True sub Main Const DEVICE_FILE_PATH = "C:\XXXX\devices.txt" Const COMMANDS_FILE_PATH = "C:\XXXX\commands.txt" dim fso set fso = CreateObject("Scripting.FileSystemObject") dim fil set fil = fso.OpenTextFile(DEVICE_FILE_PATH) const username = "XXXX" ' Username for login const password = "XXXX" ' Password for login const enable = "XXXX" ' Enable for login 'turn on synchronous mode so we don't miss any data crt.Screen.Synchronous = True 'ENABLING LOGGING '----------------------------------------------------------------- If crt.Session.Logging Then crt.Session.Log False End If crt.Session.LogFileName = "C:\XXXX.txt" If crt.Session.Logging <> True Then crt.Session.Log True End If '----------------------------------------------------------------- 'END OF ENABLING LOGGING While Not fil.AtEndOfStream ip = fil.ReadLine crt.Screen.Send "telnet " & ip & vbcr crt.Screen.WaitForString "Username: " crt.Screen.Send username & vbcr crt.Screen.WaitForString "Password:" crt.Screen.Send password & vbcr dim result result = crt.Screen.WaitForStrings("JUMPSERVER#", ">", 10) If result = 1 Then crt.Screen.WaitForString "JUMPSERVER#" crt.Screen.Send "ssh -l XXXX " & ip & vbcr crt.Screen.WaitForString "Password: " crt.Screen.Send password & vbcr End If If result = 2 Then crt.Screen.WaitForString ">" crt.Screen.Send "en" & vbcr crt.Screen.Send enable & vbcr crt.Screen.WaitForString "#" End If dim com set com = fso.OpenTextFile(COMMANDS_FILE_PATH) While Not com.AtEndOfStream ioscommand = com.ReadLine crt.Screen.Send ioscommand & vbcr wend com.Close crt.Screen.Send "exit" & vbcr crt.Screen.WaitForString "JUMPSERVER#" wend fil.Close 'turn off synchronous mode to restore normal input processing crt.Screen.Synchronous = False end Sub |
#2
|
|||
|
|||
Hi mms,
Thanks for the post. What version of SecureCRT are you using? One way to troubleshoot this would be to use a message box to find out what "result" contains. What do you get in the failure case? |
#3
|
|||
|
|||
Hi rtb, thank you for your quick response
I'm using this version. Version 7.0.3 (x64 build 480) - Official Release - January 17, 2013 I did add the line below after this line result = crt.Screen.WaitForStrings and the messagebox don't appear. Code:
crt.Dialog.MessageBox result When the telnet failed, after Connection refused error, syntax below are not evaluated. Thank you |
#4
|
|||
|
|||
Hi mms,
I am not sure what might be happening based on what you have said. Please post the modified script with the message box line added. NOTICE: The requested troubleshooting data may include sensitive information (usernames, passwords, publicly-accessible host names or IP addresses, etc.). |
#5
|
|||
|
|||
Here it is. I did modified some of the details same as previous due to sensitive info.
Code:
#$language = "VBScript" #$interface = "1.0" crt.Screen.Synchronous = True sub Main Const DEVICE_FILE_PATH = "C:\XXXX\devices.txt" Const COMMANDS_FILE_PATH = "C:\XXXX\commands.txt" dim fso set fso = CreateObject("Scripting.FileSystemObject") dim fil set fil = fso.OpenTextFile(DEVICE_FILE_PATH) const username = "XXXX" ' Username for login const password = "XXXX" ' Password for login const enable = "XXXX" ' Enable for login 'turn on synchronous mode so we don't miss any data crt.Screen.Synchronous = True 'ENABLING LOGGING '----------------------------------------------------------------- If crt.Session.Logging Then crt.Session.Log False End If crt.Session.LogFileName = "C:\XXXX.txt" If crt.Session.Logging <> True Then crt.Session.Log True End If '----------------------------------------------------------------- 'END OF ENABLING LOGGING While Not fil.AtEndOfStream ip = fil.ReadLine crt.Screen.Send "telnet " & ip & vbcr crt.Screen.WaitForString "Username: " crt.Screen.Send username & vbcr crt.Screen.WaitForString "Password:" crt.Screen.Send password & vbcr dim result result = crt.Screen.WaitForStrings("JUMPSERVER#", ">", 10) crt.Dialog.MessageBox result If result = 1 Then crt.Screen.WaitForString "JUMPSERVER#" crt.Screen.Send "ssh -l XXXX " & ip & vbcr crt.Screen.WaitForString "Password: " crt.Screen.Send password & vbcr End If If result = 2 Then crt.Screen.WaitForString ">" crt.Screen.Send "en" & vbcr crt.Screen.Send enable & vbcr crt.Screen.WaitForString "#" End If dim com set com = fso.OpenTextFile(COMMANDS_FILE_PATH) While Not com.AtEndOfStream ioscommand = com.ReadLine crt.Screen.Send ioscommand & vbcr wend com.Close crt.Screen.Send "exit" & vbcr crt.Screen.WaitForString "JUMPSERVER#" wend fil.Close 'turn off synchronous mode to restore normal input processing crt.Screen.Synchronous = False end Sub |
#6
|
|||
|
|||
Hi mms,
The issue is not related to escape sequences. The problem is actually hard to describe. If you cancel your script when you are testing a Telnet failure, what line is indicated? My guess would be that the script is stuck waiting for the username prompt on a connection where Telnet is failing. The basic concept to overcome this is to issue a command, and then wait for all possible responses including success, failure, etc. in a loop handling each possibility as it is received. If you wait for only one possible response, then you loose the possibility of dealing with any other responses. Your script is probably hanging, so you aren't loosing data, but since you are not finding what you need the script is not working. We have an example that script that illustrates how to handle many possible responses in a loop. Please find it attached. You will need to modify the example to wait for the responses that you know are possible. That string is on lines 17 to 20. Also, each time you add a possible response, you will have to add a case to the Select statement (beginning on line 40) to deal with the possible response. Does this help you fix your script? |
![]() |
Tags |
failedremote , ssh , telnet , waitforstring |
Thread Tools | |
Display Modes | Rate This Thread |
|
|