Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 07-27-2011, 09:03 AM
Carter05 Carter05 is offline
Registered User
 
Join Date: Feb 2010
Posts: 7
Changing password script

I'm trying to put together a script that will automate the changing of passwords on some cisco boxes. I have the data stored in a excel file and have been successful in reading from it. The problem I have am having is the script sends commands well ahead of what the switch is responding to.

Basic flow is:

Wait for username prompt, display text box with username, send username.
Wait for password prompt, display text box with password, send password.
send the "en" command
wait for password prompt,display text box with password, send password.
send "reload in 15"
get the current line to compare in case statements
send "conf t"
set new passwords


I have watched the data stream with Wireshark and found that when the final message box appears that displays the current line, the script has already sent the "reload in 15" command even though the switch is still waiting for the enable password.

I have tried crt.sleep commands to slow the script down. It would almost seem that it isn't actually waiting for the exact string to appear before sending the text.

Any help with this would be much appreciated!

Code:
On Error Resume Next

    ' First disconnect if we're already connected.
    If crt.Session.Connected Then crt.Session.Disconnect

    g_strError = ""
    Err.Clear
    
    Dim strCmd
            strCmd = "/TELNET " & strIP
            crt.Session.Connect strCmd
            crt.Screen.Synchronous = True
            
            If crt.Session.Connected <> True Then Exit Function
            
            ' Look for username and password prompts
            	 crt.Screen.WaitForString("Username:")
		MsgBox "Found " & strUsername
	    	crt.Screen.SendKeys strUsername & "{ENTER}"
            	crt.Screen.WaitForString("Password:")
		MsgBox "Found " & strPassword
	    	crt.Screen.SendKeys strPassword & "{ENTER}"
		crt.Screen.SendKeys "en" & "{ENTER}"
		crt.Screen.WaitForString("Password:")
		MsgBox "Found " & strEnable
		crt.Screen.SendKeys strEnable & "{ENTER}"
		crt.Screen.Send vbcr
		crt.Sleep 5000
		crt.Screen.SendKeys "reload in 15" & "{ENTER}"

		'GET THE CURRENT TEXT ON THE SCREEN TO DETERMINE WHAT THE RELOAD COMMAND CAME BACK WITH
		strCurLine = crt.Screen.Get(crt.Screen.CurrentRow,0,crt.Screen.CurrentRow,crt.Screen.CurrentColumn)

		MsgBox "Found " & strCurLine

		'CASE STATEMENTS TO HAVE DIFFERENT ACTIONS DEPENDING ON WHAT THE STRCURLINE VARIABLE HOLDS
		Select Case UCase(strCurLine)
			Case "System configuration has been modified. Save? [yes/no]:"
			crt.Screen.SendKeys "yes" & "{ENTER}"

			Case "Proceed with reload? [confirm]"
			crt.Screen.SendKeys "{ENTER}"
		End Select
		

		crt.Screen.SendKeys "{ENTER}"
		crt.Screen.Sendkeys "conf t"  & "{ENTER}"
		crt.Screen.SendKeys "enable secret " & strNEWEnable
		crt.Screen.SendKeys "{ENTER}"
		crt.Screen.SendKeys "username " & strUsername & " password " & strNEWPassword & " {ENTER}"
		crt.Screen.SendKeys "{ENTER}"
            Connect = True
    
    If Err.Number <> 0 Then
        g_strError = Err.Description
    End If
    On Error Goto 0

Last edited by Carter05; 07-27-2011 at 09:23 AM.
Reply With Quote
  #2  
Old 07-27-2011, 09:40 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,635
Hi Carter05,

Quote:
the script has already sent the "reload in 15" command even though the switch is still waiting for the enable password.
I do not see anywhere in your script that you are "waitingfor" the remote's response after sending the password.

As a rule, you never want to have multiple "sends" without an intervening "waitfor".

If, in place of the sleep, you "waitfor" the expected response from the remote, what are the results?
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 07-27-2011, 10:37 AM
Carter05 Carter05 is offline
Registered User
 
Join Date: Feb 2010
Posts: 7
Code:
crt.Screen.WaitForString("Username:")
		MsgBox "Found " & strUsername
	    	crt.Screen.SendKeys strUsername & "{ENTER}"
            	crt.Screen.WaitForString("Password:")
		MsgBox "Found " & strPassword
	    	crt.Screen.SendKeys strPassword & "{ENTER}"
		crt.Screen.SendKeys "en" & "{ENTER}"
		crt.Screen.WaitForString("Password:")
		MsgBox "Found " & strEnable
		crt.Screen.SendKeys strEnable & "{ENTER}"
		crt.Screen.Send vbcr
		crt.Sleep 5000
		crt.Screen.SendKeys "reload in 15" & "{ENTER}"

'GET THE CURRENT TEXT ON THE SCREEN TO DETERMINE WHAT THE RELOAD COMMAND CAME BACK WITH
		strCurLine = crt.Screen.Get(crt.Screen.CurrentRow,0,crt.Screen.CurrentRow,crt.Screen.CurrentColumn)

		MsgBox "Found " & strCurLine
I do have a WaitForString("Password:") then it send the enable password and then {ENTER} and a vbcr for good measure. This should bring me to a prompt such as "switchname#". The thing is even after I wait 5 seconds the "switchname#" prompt never appears until it runs through the rest of the commands.

I guess I don't understand why the script wont send a CR after sending the enable password and give me the "switchname#" prompt. When the current text routine comes back in the message box I always get "Password:"

I don't know what I would use for a reponse to wait for from the switch since it doesn't send me acknowledgement.

Maybe I'm making this overly complicated. I'm fairly new to programming and scripting in general.
Reply With Quote
  #4  
Old 07-27-2011, 02:28 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,635
Hi Carter05,

As someone new to scripting, I have a couple of other suggestions.

You should definitely check out using the script recorder (Script menu) to give you a start on the scripts you want to create.

Also, you can review the scripting manual, which will help to introduce you to concepts specific to SecureCRT.

In it, you will find reinforced what I mentioned in my prior post. You will need to have a WaitForString to offset every Send.

Quote:
Basic flow is:

Wait for username prompt, display text box with username, send username.
Wait for password prompt, display text box with password, send password.
send the "en" command
wait for password prompt,display text box with password, send password.
send "reload in 15"
get the current line to compare in case statements
send "conf t"
set new passwords
Note above how there are two places where you send twice in a row and only waitfor once. So it can be the earlier "two sends in a row" that throws off the timing of the script and causes the results you see.

Also, you don't need to use the SendKeys method in your script, just Send should do the job.

For example:
Code:
On Error Resume Next

' First disconnect if we're already connected.
If crt.Session.Connected Then crt.Session.Disconnect

g_strError = ""
Err.Clear
    
Dim strCmd
strCmd = "/TELNET " & strIP
crt.Session.Connect strCmd
crt.Screen.Synchronous = True
            
If crt.Session.Connected <> True Then Exit Function
         
    ' Look for username and password prompts
    crt.Screen.WaitForString "Username:"
    MsgBox "Found " & strUsername
    crt.Screen.Send strUsername & vbcr
    crt.Screen.WaitForString "Password:" 
    MsgBox "Found " & strPassword
    crt.Screen.Send strPassword & vbcr
    crt.Screen.WaitForString "expected_prompt" 
    crt.Screen.Send "en" & vbcr
    crt.Screen.WaitForString "Password:"
    MsgBox "Found " & strEnable
    crt.Screen.Send strEnable & vbcr
    crt.Screen.WaitForString "expected_prompt"
    crt.Screen.Send "reload in 15" & vbcr

     'GET THE CURRENT TEXT ON THE SCREEN TO DETERMINE WHAT THE RELOAD COMMAND CAME BACK WITH
		strCurLine = crt.Screen.Get(crt.Screen.CurrentRow,0,crt.Screen.CurrentRow,crt.Screen.CurrentColumn)

		MsgBox "Found " & strCurLine

etc.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 07-27-2011, 02:59 PM
Carter05 Carter05 is offline
Registered User
 
Join Date: Feb 2010
Posts: 7
Starting to understand better now, thanks.

Is there a way I can have the WaitForString command wait for ANY text response rather than something specific?
Reply With Quote
  #6  
Old 07-27-2011, 03:51 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,635
Hi Carter05,

You can use the WaitForStrings method (note plural) to wait for one of several expected strings.

When it comes to an expected prompt from the remote device, the scripting manual includes a section on how to heuristically detect a prompt, if that's what you wish to do.

I believe the code snippet is found in section 7, but if you search for the word heuristic, you will find it since that word only appears once in the entire scripting manual.
__________________
Thanks,
--Brenda

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

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 11:20 PM.