Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 01-13-2009, 04:05 PM
1720 1720 is offline
Registered User
 
Join Date: Jul 2007
Posts: 13
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
Reply With Quote
  #2  
Old 01-13-2009, 05:50 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
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]
Reply With Quote
  #3  
Old 01-14-2009, 07:16 AM
1720 1720 is offline
Registered User
 
Join Date: Jul 2007
Posts: 13
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.
Reply With Quote
  #4  
Old 01-14-2009, 09:50 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Thanks for posting the entire script.

Quote:
The error I get is - Error: Object doesn't support this property or method: 'crt.Screen.ReadString'.
I would expect to see this if using an older version of SecureCRT that doesn't recognize the relatively new ReadString function.

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]
Reply With Quote
  #5  
Old 01-14-2009, 10:09 AM
1720 1720 is offline
Registered User
 
Join Date: Jul 2007
Posts: 13
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?
Reply With Quote
  #6  
Old 01-14-2009, 12:16 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Quote:
I am running v5.0.0 (build 992). I am assuming because of the error, ReadString funtion is not available in this release.
ReadString is available in version 5.1 and later. If you're not interested in upgrading your license to get the current version of software, plus another year of access to free upgrades and support, you may be eligible for a free upgrade to a newer version of SecureCRT depending on the date you purchased your license.

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:
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?
The ReadString method is the most elegant method, but the following code snippet shows how you can do something similar to crt.Screen.ReadString by using crt.Screen.Get. The code builds up the data that appears on screen, line by line, and searches for a prompt to know that we're done gathering output.
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]
Reply With Quote
  #7  
Old 01-14-2009, 12:46 PM
1720 1720 is offline
Registered User
 
Join Date: Jul 2007
Posts: 13
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
Reply With Quote
  #8  
Old 01-14-2009, 04:04 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
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
Could you post a log file so that we can see the output and try to see why the code may be getting stuck?

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]
Reply With Quote
  #9  
Old 01-28-2009, 05:05 PM
gus gus is offline
Registered User
 
Join Date: Jan 2009
Posts: 2
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?
Reply With Quote
  #10  
Old 01-28-2009, 05:46 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
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]
Reply With Quote
  #11  
Old 02-08-2010, 05:12 PM
gus gus is offline
Registered User
 
Join Date: Jan 2009
Posts: 2
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
Format of text file named device_list.txt (tell the script where it is - DEVICE_FILE_PATH =):

Code:
host-a:SSH
host-b:Telnet
Reply With Quote
  #12  
Old 02-08-2010, 06:11 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Thanks Gus!

It's great to see SecureCRT scripts posted to the forums.
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #13  
Old 02-11-2010, 08:56 AM
WesR WesR is offline
Registered User
 
Join Date: Mar 2005
Location: USA
Posts: 132
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
and then I just add checks like:

Code:
    If ReadStr then ...
I don't know if there is an easier way, but this seems to work for me...

Wes
Reply With Quote
  #14  
Old 03-02-2010, 08:41 AM
vickz1975 vickz1975 is offline
Registered User
 
Join Date: Jan 2010
Posts: 18
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
Reply With Quote
  #15  
Old 03-02-2010, 10:59 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,997
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:
I logon onto a secure host and then run the script.
If you are attempting to perform the initial connection to the Telnet server, you would need to use "crt.session.connect" or "crt.session.ConnectInTab" and then provide the IP, protocol and authentication as necessary.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 07:08 AM.