Link to home
Start Free TrialLog in
Avatar of fileinster
fileinsterFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Using scripting in SecureCRT, my script isn't waiting for input before continuing

Hi all,

You will have to forgive my ignorance in this area as I know next to nothing about vbScript! I have a feeling this is a very basic premise that I am about to ask! Please bear in mind that I really do mean the above... go easy on me!!! :-P

I am trying to create a script in SecureCRT that will connect to multiple hosts, login using different credentials (OTP), find out if the device is running IOS or CatOS and then list the running configuration and save it to a file. However, I have identified that the problem with my script seems to be that I throw up a dialog prompt to ask for the password, but meanwhile the script plods along in the background. Another thing I have noticed is that only certain parts of the script continue. I do a "sh version" to get the OS type, and then read the screen to find it out, but it seems the reading is happening before the output arrives.

I've heard that vbScript can now use regexp; if this is true, could I wait for the prompt to read the device name followed by anything, like "^DEVICE_(#|> $)"

(The devices.txt file is semicolon separated router and protocol. Also the MsgBox commands were there to aid troubleshooting)

Thanks to all in advance!
Sub Main
	
	Dim fso
	Dim fil
	Dim Connected
	Dim UID
	Dim DeviceType
 
	Const DEVICE_FILE_PATH = ".\Devices.txt"
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set fil = fso.OpenTextFile(DEVICE_FILE_PATH)
	UID = "user"
 
	While Not fil.AtEndOfStream
		line = fil.ReadLine
		
		Router = Split(line, ";")(0)
		protocol = Split(line, ";")(1)
		
		Select Case protocol
			Case "Telnet"
				cnxnString = "/TELNET " & Router & " 23"
			Case "SSH2"
				cnxnString = "/SSH2 /L " & UID & " /PASSWORD " & password & " /C 3DES /M MD5 " & Router
			Case "SSH1"
				cnxnString = "/SSH1 /L " & UID & " /PASSWORD " & password & " /C 3DES /M MD5 " & Router
		End Select	
		
		crt.Session.Connect cnxnString
'		crt.Screen.Synchronous = True
' I tried this and it breaks everything!
		crt.Screen.Send UID & chr(13)
		Password = GetPassword()
		crt.Screen.Send Password & chr(13)
		crt.Screen.Send "sh ver " + chr(124) + " i IOS" + chr(13)
		screenrow = crt.screen.CurrentRow
			MsgBox screenrow
		screenrow = screenrow - 1
			MsgBox screenrow
		tempDevice = crt.Screen.Get(screenrow, 1, screenrow, 3 )
			MsgBox tempDevice
		If tempDevice = "IOS" then
			DeviceType = "IOS"
		Else
			DeviceType = "CatOS"
		End If
			Msgbox DeviceType	
		
		Dim logfile
		logfile = ("C:\TEMP\" + Router + "-confg.txt")
		crt.Session.LogFileName = logfile
		crt.Session.Log True
			
 
			
			If DeviceType = "IOS" then
				crt.Screen.Send "term len 0" & chr(13)
				crt.Screen.Send "sh run" & chr(13)
			End If
			IF DeviceType = "CatOS" then
				crt.Screen.Send "set length 0" & chr(13)
				crt.Screen.Send "sh conf" & chr(13)
			End If
 
			crt.Session.Log False
			crt.Session.Disconnect
		
 
	Wend
			
			fil.Close 
 
End Sub
 
 
Function GetPassword()
 Dim Password
 Do
  Password = crt.Dialog.Prompt("Enter Password:","Password","",True)
  If Password = "" Then 
   result = crt.Dialog.MessageBox("Password cannot be blank. Retry?","No Password",4)
    If result = 7 then
     crt.quit
    End If
  End If
 Loop Until Password<>""
 GetPassword=Password
 Exit Function
End Function

Open in new window

Avatar of merowinger
merowinger
Flag of Germany image

Hey i don't know if i can help you, but i'll give it a try :)

what do you mean with "regexp"

what is "crt.Session.Connect" does it work?

where do wanna wait why? :)
Avatar of fileinster

ASKER

Thanks a lot for your resonse; I was beginning to think I wasn't going to get one!

REgExp = Short for Regular Expressions

crt.Session commands are SecureCRT scripting commands that directly interface with the application. "crt.Session.Connect" sends a command to the app to connect to connect to a host using variables such as host, protocol, port number ect. The session does connect so I know this part is OK. (it's not very well documented, but the site is here: http://www.vandyke.com/support/crt/scripting_faq.html )

What I mean by "wait" is that the line "Password = GetPassword()" prompts me for a password as the script logs into each device as we use one time passwords, so I need a different password for each device.
However from my msgbox commands it appears that while the script is waiting for my input it is continuing the script in the background. The script should let me enter a password, then enter a command, the read the screen for output of that command and set a variable accordingly. The Msgbox outputs suggest that it is reading the screen while it's waiting for my password, but it still enters the command  correctly. It should also enter command based on the device type, but it doesn't. The script defaults to CatOS, but it doesn't enter the commands for CatOS at any time. It does however create the log file at "crt.Session.Log True".


Any ideas? Please....
ASKER CERTIFIED SOLUTION
Avatar of merowinger
merowinger
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I seen that script before and that's where I plagiarised half the code from. You'll notice the syntax is extremely similar. Using the waitforstring doesn't fix my problem.

I appreciate that a lot of this depends on how CRT handles the script; I thought I may be missing something obvious.
Sorry, I submitted that when I wasn't finished. What I meant to say was that when I used the waitforstring "#" to wait for the command prompt it did not fix anything. However I think that's because it is reading the whole string. If I could use regular expressions to search for the device name at the start and either # or "> " at the end I think that would work, but I'm not sure how to use regexp in vb. The expression would be ^ROUTERNAME_[#(> )]$
could you show the box with the password question...
This file has the Password dialog box as well as all message boxes. Here you can see that it selects the line at Username as it is waiting for the password and minuses one from the current row. However, when it selects CatOS it doesn't run the commands for catOs and instead quits.
MsgBox-and-Password.zip
ok if i understand you right, your sending commands to that console via

crt.Screen.Send

and get the screenoutput in variables with

crt.Screen.Get

So where exactly is the problem and what's the content of the variables when executing screen.get
From that particular exampe is was setting the variable with "Use" as the input string, and as it didn't equal IOS it defaulted to CatOS. However, it should then have run commands for CatOS, but it didn't
I eventually got the solution with help from the VanDyke forum. Here's the working solution for future reference. Thanks Merowinger for all your help!
Sub Main
	
	Dim fso
	Dim fil
	Dim Connected
	Dim UID
	Dim DeviceType
	Dim Password
	Dim Router
	Dim protocol
	Dim LogFileLocation
 
'******************************
'******************************
'   LOCATION OF LOG FILE
 
 LogFileLocation = "C:\Temp\"
 
'******************************
'******************************
 
 
	Const DEVICE_FILE_PATH = ".\Devices.txt"
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set fil = fso.OpenTextFile(DEVICE_FILE_PATH)
	UID = GetUID()
 	crt.Session.Disconnect
 
	While Not fil.AtEndOfStream
		line = fil.ReadLine
		
		Router = Split(line, ";")(0)
		protocol = Split(line, ";")(1)
		Password = GetPassword()
		
		Select Case protocol
			Case "Telnet"
				cnxnString = "/TELNET " & Router & " 23"
			Case "SSH2"
				cnxnString = "/SSH2 /L " & UID & " /PASSWORD " & password & " /C 3DES /M MD5 " & Router
			Case "SSH1"
				cnxnString = "/SSH1 /L " & UID & " /PASSWORD " & password & " /C 3DES /M MD5 " & Router
		End Select	
		
		crt.Session.Connect cnxnString
		crt.Screen.Synchronous = True
		If protocol = "Telnet" then
			crt.Screen.WaitForString "sername:"
			crt.Screen.Send UID & chr(13)
			crt.Screen.WaitForString "assword:"
			crt.Screen.Send Password & chr(13)
		End If
		crt.Screen.WaitForStrings Router + "#",  Router + "> (enable)"
		crt.Screen.Send "sh ver " + chr(124) + " i IOS" + chr(124) + "Sw" + chr(13)
		crt.Screen.WaitForStrings Router + "#",  Router + "> (enable)"
		screenrow = crt.screen.CurrentRow
		screenrow = screenrow - 1
		tempDevice = crt.Screen.Get(screenrow, 1, screenrow, 80 )
 
		DeviceType = "CatOS"
 
		If instr(tempDevice,"IOS") > 0 then
			DeviceType = "IOS"
		End If
		
		
		Dim logfile
		logfile = (LogFileLocation + Router + "-confg.txt")
		crt.Session.LogFileName = logfile
		crt.Session.Log True
			
 
			
			If DeviceType = "IOS" then
				crt.Screen.Send "term len 0" & chr(13)
				crt.Screen.WaitForStrings Router + "#",  Router + "> (enable)"
				crt.Screen.Send "sh run" & chr(13)
				crt.Screen.WaitForStrings Router + "#",  Router + "> (enable)"
			End If
			If DeviceType = "CatOS" then
				crt.Screen.Send "set length 0" & chr(13)
				crt.Screen.WaitForStrings Router + "#",  Router + "> (enable)"
				crt.Screen.Send "sh conf" & chr(13)
				crt.Screen.WaitForStrings Router + "#",  Router + "> (enable)"
			End If
 
			crt.Session.Log False
			crt.Session.Disconnect
		
 
	Wend
			
			fil.Close 
 
End Sub
 
 
 
 
 
'**************************************************************************************
Function GetPassword()
 Dim TempPassword
 Do
  TempPassword = crt.Dialog.Prompt("Enter Password:","Password","",True)
  If TempPassword = "" Then 
   result = crt.Dialog.MessageBox("Password cannot be blank. Retry?","No Password",4)
    If result = 7 then
     crt.quit
    End If
  End If
 Loop Until TempPassword<>""
 GetPassword=TempPassword
 Exit Function
End Function 
 
'**************************************************************************************
Function GetUID()
  dim TempUID
  Do
   TempUID = crt.Dialog.Prompt("Enter UserID:","UserID","",False)
   If TempUID = "" Then 
    result = crt.Dialog.MessageBox("UserID cannot be blank. Retry?","No Userid",4)
    If result = 7 then
     crt.quit
    End If
   End If
  Loop Until TempUID<>""
  GetUID=TempUID
  Exit Function
End Function

Open in new window

PS, here's the link to the Vandyke discussion:

http://forums.vandyke.com/showthread.php?p=14178