#1
|
|||
|
|||
Capture output when connected to a Cisco device
I am writing a VBScript that first attempts to ssh to a cisco device and if it gets the Password prompt, it sends the correct password string, otherwise it attempts to telnet. However, I am having a problem that even though I get the Password prompt it does not seem to login. Here is a snippet from my code -
cmd = "/SSH2 /L " & user_name & " /PASSWORD " & passwd & " /C 3DES /M MD5 " & host crt.Session.Connect cmd crt.Screen.Send "ssh -l " & user_name & " " & device & chr(13) response = crt.Screen.ReadString() Dim regex, Match, Matches Set regEx = New RegExp regEx.Pattern = "Password" regEx.IgnoreCase = False regEx.Global = True If regEx.Test(response) then crt.window.caption = device crt.Screen.Send passwd & vbCr else crt.window.caption = device crt.Screen.send "telnet " & device & chr(13) crt.Screen.WaitForString "Username:" crt.Screen.Send user_name & vbCr crt.Screen.WaitForString "Password:" crt.Screen.Send passwd & vbCr End if |
#2
|
||||
|
||||
Hello,
Thanks for posting your question. Can you post the entire script, or if you're concerned about security and other things, send it to support@vandyke.com with a subject of forum thread #3388? One of the reasons I'd like to look at the script is to find out the following - have you set Crt.Screen.Synchronous = TRUE at the beginning of the script? If not, that could be the problem.
__________________
Mike VanDyke Software Technical Support [http://www.vandyke.com/support] |
#3
|
|||
|
|||
Hi Mike,
Here is a copy of the entire script - # $language = "VBScript" # $interface = "1.0" Sub Main crt.Screen.Synchronous = TRUE Dim passwordPrompt prompt = "\nPassword: " Dim host host = crt.Arguments(0) Dim user_name user_name = crt.Dialog.Prompt("Enter your TACACS userID ", user_name, "TACACS Login",False) Dim passwd passwd = crt.Dialog.Prompt("Enter your TACACS password ", passwd, "TACACS Login",True) Dim device device = crt.Arguments(1) cmd = "/SSH2 /L " & user_name & " /PASSWORD " & passwd & " /C 3DES /M MD5 " & host crt.Session.Connect cmd crt.Screen.Send "ssh -l " & user_name & " " & device & chr(13) response = crt.Screen.ReadString() Dim regex, Match, Matches Set regEx = New RegExp regEx.Pattern = "Password" regEx.IgnoreCase = False regEx.Global = True If regEx.Test(response) then crt.window.caption = device crt.Screen.Send passwd & vbCr else crt.window.caption = device crt.Screen.send "telnet " & device & chr(13) crt.Screen.WaitForString "Username:" crt.Screen.Send user_name & vbCr crt.Screen.WaitForString "Password:" crt.Screen.Send passwd & vbCr End if End Sub The error I get is - Error: Object doesn't support this property or method: 'crt.Screen.ReadString'. Thanks. |
#4
|
||||
|
||||
Thanks for posting the entire script.
Quote:
What is the version you see in Help / About SecureCRT? I've tested the script and it should work with perhaps only one minor change. I had to modify the ReadString function call to wait for a particular string such as a '$' prompt, and use a timeout of a couple seconds: response = crt.Screen.ReadString("$",2)After doing this, and adding a few Crt.Screen.Echo statements to simulate the interaction you might expect, the script worked for me using SecureCRT 6.1.3 build 423.
__________________
Mike VanDyke Software Technical Support [http://www.vandyke.com/support] |
#5
|
|||
|
|||
Hi Mike,
I am running v5.0.0 (build 992). I am assuming because of the error, ReadString funtion is not available in this release. Would you be able to recommend another method of getting the input? The problem with WaitforString is that it should be exact or is it a pattern match only? |
#6
|
||||
|
||||
Quote:
What is the Issue Date listed in your Help / About SecureCRT window? You can check the eligibility page to find out whether the purchase/issue date of your license allows a free upgrade to a newer version. Quote:
Code:
szPrompt = "$" Do crt.Screen.WaitForString vbcr, 3 nCurrentRow = crt.Screen.CurrentRow nCurrentColumns = crt.Screen.Columns szOutput = szOutput & Trim(crt.Screen.Get(nCurrentRow, 0, nCurrentRow, nColumns)) & vbcr if InStr(szOutput, szPrompt) > 0 then exit do Loop response = szOutput
__________________
Mike VanDyke Software Technical Support [http://www.vandyke.com/support] |
#7
|
|||
|
|||
Hi Mike,
The issue date is Official Release - June 16, 2005. We actually have purchased 6.0 but not all desktops have been updated with the newer version. I am not sure what the time line is and so I am stuck with this version for now. I tried using the code you sent and it just sits on the Password prompt. Here is what my code looks like - Code: Code:
# $language = "VBScript" # $interface = "1.0" Sub Main crt.Screen.Synchronous = TRUE ' MsgBox "SecureCRT version is: " & crt.Version Dim szPrompt szPrompt = "Password: " Dim host host = crt.Arguments(0) Dim user_name user_name = crt.Dialog.Prompt("Enter your TACACS userID ", user_name, "TACACS Login",False) Dim passwd passwd = crt.Dialog.Prompt("Enter your TACACS password ", passwd, "TACACS Login",True) Dim device device = crt.Arguments(1) cmd = "/SSH2 /L " & user_name & " /PASSWORD " & passwd & " /C 3DES /M MD5 " & host crt.Session.Connect cmd crt.Screen.Send "ssh -l " & user_name & " " & device & chr(13) Do crt.Screen.WaitForString szPrompt, 1 nCurrentRow = crt.Screen.CurrentRow nCurrentColumns = crt.Screen.Columns szOutput = szOutput & Trim(crt.Screen.Get(nCurrentRow, 0, nCurrentRow, nColumns)) & vbcr if InStr(szOutput, szPrompt) > 0 then exit do Loop response = szOutput If regEx.Test(response) then crt.window.caption = device crt.Screen.Send passwd & vbCr else crt.window.caption = device crt.Screen.send "telnet " & device & chr(13) crt.Screen.WaitForString "Username:" crt.Screen.Send user_name & vbCr crt.Screen.WaitForString "Password:" crt.Screen.Send passwd & vbCr End if End Sub |
#8
|
||||
|
||||
I'm not sure I understand where the code is stuck.
Is it stuck in the Else statement at the bottom? Code:
crt.Screen.WaitForString "Password:" crt.Screen.Send passwd & vbCr Here's a way to set a log filename and enable logging in your code. Code:
crt.Session.LogFileName = "c:\temp\Mylog-" & Replace(Replace(Replace(Now, ":", "-"), "/", "-"), " ", "_") & ".log" crt.Session.Log true, false
__________________
Mike VanDyke Software Technical Support [http://www.vandyke.com/support] |
#9
|
|||
|
|||
My script also hangs at password prompt when using SSH
Did the hanging at the password prompt issue get fixed?
I have a similar script which logs into multiple Cisco devices using either telnet or ssh. I first login to a secure jump-box and then run my script. All works fine except when its an SSH router the script is trying to connected to. The script just hangs at the password prompt. Once I enter the password in manually the script continues and completes. Is there an issue with passing the password via a script when using SSH to a Cisco router from SecureCRT 6.1? |
#10
|
||||
|
||||
Hello gus,
As far as I know we haven't received a follow up message via e-mail so I don't know the current status of the password prompt issue. There shouldn't be any technical issues sending the password. When authentication is involved there can be certain timing issues, but these are pretty rare. Using Crt.Screen.WaitForString or a variation of it should get around any timing issues. Could you either post the script (with the password and any other sensitive information such as hostname or IP address deleted or replaced with some other character such as '*', 'x') or ideally for security reasons send the script to support@vandyke.com with a subject of attention Mike forum #3388? Also, can you describe how you launch the script - from the Script menu or as part of the SecureCRT command line using /Script?
__________________
Mike VanDyke Software Technical Support [http://www.vandyke.com/support] |
#11
|
|||
|
|||
Working Script for Cisco IOS Devices
I promised I would post a working script.....that was over a year ago but here it is! I have been using this for a while now to take snapshots of devices I am about to make a change on.
I logon onto a secure host and then run the script. Pop-up boxes ask for username and password for both telnet and ssh devices as well as the enable password for your routers. The script will run through all the devices listed in a text file and log the output from each device into individual log files on your desktop (windows). I use this for before and after snapshots when carrying out changes. Because the log files are date and time stamped I then use a diff tool (Beyond Compare) to compare pre and post change snapshots. The script could be easily adapted to backup configs or logon to devices other than Cisco. Cheers Giuseppe Code:
# $language = "VBScript" # $interface = "1.0" ' cisco_show.vbs ' Tested on SecureCrt Version 6.1.0 (build 349) ' Giuseppe Nardo ' 24-04-09 Sub Main Dim myusername, mypassword, myenablepass myusername = InputBox("Enter username for Telnet devices:") username = myusername mypassword = InputBox("Enter password for Telnet devices:") password = mypassword myusername1 = InputBox("Enter username for SSH devices:") username1 = myusername1 mypassword1 = InputBox("Enter password for SSH devices:") password1 = mypassword1 myenablepass = InputBox("Enter enable password:") enablepass = myenablepass '----------------------------------------------------------------------------------------------- 'State device list location and open list '----------------------------------------------------------------------------------------------- Const DEVICE_FILE_PATH = "file path to\device_list.txt" Dim fso, shell, fil Set fso = CreateObject("Scripting.FileSystemObject") Set shell = CreateObject("WScript.Shell") Set fil = fso.OpenTextFile(DEVICE_FILE_PATH) '----------------------------------------------------------------------------------------------- 'Use login protocol stated in device list and login into device '----------------------------------------------------------------------------------------------- Dim ip, protocol, line, connnect, index While Not fil.AtEndOfStream line = fil.ReadLine ip = Split(line, ":")(0) protocol = Split(line, ":")(1) Select Case protocol Case "Telnet" connect = "telnet " & ip & vbCr Case "SSH" connect = "ssh -l " & username1 & " " & ip & vbCr End Select crt.Screen.Synchronous = True crt.Screen.Send connect index = crt.Screen.WaitForStrings("sername:", "assword:") If index = 1 Then crt.Screen.Send username & vbCr crt.Screen.WaitForString "assword:" crt.Screen.Send password & vbCr End If If index = 2 Then crt.Screen.Send password1 & vbCr End If '----------------------------------------------------------------------------------------------- 'Create log file to capture output '----------------------------------------------------------------------------------------------- Dim command_ouput_format, command_output command_ouput_format = shell.SpecialFolders("Desktop") & "\host_hou-min-sec_dd-mm-yyyy.log" crt.session.Log False command_output = command_ouput_format command_output = Replace(command_output, "sec" , D(second(now))) command_output = Replace(command_output, "min" , D(minute(now))) command_output = Replace(command_output, "hou" , D(hour(now))) command_output = Replace(command_output, "host" , ip) command_output = Replace(command_output, "dd" , D(Day(Now))) command_output = Replace(command_output, "mm" , D(Month(Now))) command_output = Replace(command_output, "yyyy" , Year(Now)) crt.session.logfilename = command_output crt.session.log True '----------------------------------------------------------------------------------------------- 'Issue commands to device '----------------------------------------------------------------------------------------------- crt.Screen.WaitForString ">" crt.Screen.Send "enable" & vbCr crt.Screen.WaitForString "assword:" crt.Screen.Send enablepass & vbCr crt.Screen.WaitForString "#" crt.Screen.Send "terminal length 0" & vbCr crt.Screen.WaitForString "#" crt.Screen.Send "show ip access-lists" & vbCr crt.Screen.WaitForString "#" crt.Screen.Send "show ip ospf ne" & vbCr crt.Screen.WaitForString "#" 'crt.Screen.Send "show ip ospf int brie" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show cdp ne" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show ip int brie | ex unass" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show ip route summ" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show ip route" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show ip bgp" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show run" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show vlan brie" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show int trunk" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show mac-address-table" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show ip arp" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show ver" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show module" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show power" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show cdp ne de" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show int status" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show ip int brie" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show etherchannel summary" & vbCr 'crt.Screen.WaitForString "#" 'crt.Screen.Send "show log" & vbCr 'crt.Screen.WaitForString "#" crt.Screen.Send "exit" & vbCr crt.Screen.WaitForString ">" crt.session.Log False Wend fil.Close crt.Session.Disconnect End Sub '----------------------------------------------------------------------------------------------- 'Function to add leading 0 to single digit dates '----------------------------------------------------------------------------------------------- Function D(nNumber) D = nNumber If nNumber < 10 Then D = "0" & nNumber End Function Code:
host-a:SSH host-b:Telnet |
#12
|
||||
|
||||
Thanks Gus!
It's great to see SecureCRT scripts posted to the forums.
__________________
Mike VanDyke Software Technical Support [http://www.vandyke.com/support] |
#13
|
|||
|
|||
This is a little unrelated to some of the discussion, but we have the same problem here where I can never tell what version of SecureCRT my scripts will be run on. I've gotten into the habit of testing for each feature and then either stopping the script or using some workaround code depending on the situation. For example, I typically include:
Code:
' --------------- check for SecureCRT clipboard support ---------------- On Error Resume Next result = crt.Clipboard.Text if Err.Number = 0 Then ' Clipboard is supported Clip = 1 End If On Error GoTo 0 ' --------------- check for SecureCRT ReadString support ---------------- On Error Resume Next text.Send vbCr result = crt.screen.ReadString(vbCr,1) if Err.Number = 0 Then ' ReadString is supported ReadStr = 1 End If On Error GoTo 0 Code:
If ReadStr then ... Wes |
#14
|
|||
|
|||
Need help!
Hi,
I am trying to use the the above posted script. I just modified the paths of the files and commented out the SSH lines. It is giving me an error at line 60 of the above code. crt.Screen.Send connect <== CRT Scripting Runtime error Error: Screen.Send: not connected File:vbscript.vbx Line: 60 Why is it not connecting? Thanks, Vickz |
#15
|
|||
|
|||
Hello vickz1975,
Gus's script is intended to be used in a "jump" host type scenario. By that I mean you would first need to be authenticated to a host and then the "SSH" or "Telnet" commands are executed as if you typed them in the SecureCRT terminal window. Quote:
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | Rate This Thread |
|
|