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 08-22-2008, 09:08 AM
darkoth darkoth is offline
Registered User
 
Join Date: Jun 2007
Posts: 29
Script to automate passwords on multiple servers?

I'm looking to do the following but am not sure if I can do this with a SecureCRT script. Here's basically what I want to do.

First I am already logged into the Solaris 10 server via SecureCRT. I want to execute these commands FROM the server I am already logged into:

1.) Execute command -
cat server_list.txt

2.) Read in the list of server names and store them.

3.) ssh first server name

4.) If this comes up on the screen
"Are you sure you want to continue connecting (yes/no)?"
Then send yes else continue

5.) This will display
"Password: "
send pop up - "Please enter your password"
store password and send it to the terminal.

6.) Run command:
su -

7.) Wait for "Password: " and send to screen - Please enter root password.
store root password.

8.) Next run command
passwd root

9.) Wait for "New Password: "

10.) send pop up with screen asking for a new root password and store it.

11.) Send new password

12.) Wait for "Re-enter new Password: " and send password again.

13.) Wait for "passwd: password successfully changed for root"

14.) send "exit" twice to logout of the server and return to next server in list and then repeat steps 4-13 until the list has expired.


That's pretty much what I'm wanting to do. I have a list of about 50 servers I need to change the root password for and rather than having to do it manually I figured a script would be the easiest way to go about this.

Even better would be if it presented me with a screen right up front that asked me for the variables I need and then just ran the script:

so it would ask:

Input YOUR Password:
Current root password:
NEW root password:

and then run and do its thing... Any help with this would be great.

Last edited by darkoth; 08-22-2008 at 09:12 AM.
Reply With Quote
  #2  
Old 08-22-2008, 06:29 PM
jjh jjh is offline
VanDyke Customer Support
 
Join Date: Feb 2004
Posts: 815
Hello darkoth.

Here is some information that shows you how you might accomplish your goals with scripting:

Code:
'~ 1.) Execute command -
'~ cat server_list.txt 

crt.Screen.Synchronous = True
crt.Screen.Send "cat server_list.txt" & vbcr


'~ 2.) Read in the list of server names and store them. 

vServerList = Split(crt.Screen.ReadString("]$"), vbcr)


'~ 3.) ssh first server name

For Each szHost in vServerList

    crt.Screen.Send "ssh " & szHost & vbcr

    '~ 4.) If this comes up on the screen
    '~ "Are you sure you want to continue connecting (yes/no)?"
    '~ Then send yes else continue 

    '~ 5.) This will display
    '~ "Password: "
    '~ send pop up - "Please enter your password"
    '~ store password and send it to the terminal. 

    Do
        szResults = Crt.Screen.WaitforStrings("Are you sure...", "Password:", 60) = 1
        
        Select Case szResult
            Case 1
                crt.Screen.Send "yes" & vbcr
            
            Case 2
                szPassword = crt.dialog.prompt("Please enter your password", "Password", "", True)
                crt.Screen.Send szPassword & vbcr
                exit do
               
            Case else
                MsgBox "We timed out"
        end select
    Loop
   

    '~ 6.) Run command:
    '~ su -
    
    crt.Screen.Send "su -" & vbcr
    

    '~ 7.) Wait for "Password: " and send to screen - Please enter root password. 
    '~ store root password. 

    crt.Screen.WaitForString "Password:"
    szRootPassword = crt.dialog.Prompt("Please enter the root password: ", "Root Password", "", True)
    crt.Screen.Send szRootPassword & vbcr

    '~ 8.) Next run command
    '~ passwd root 
    
    crt.Screen.Send "passwd root" & vbcr

    '~ 9.) Wait for "New Password: "

    '~ 10.) send pop up with screen asking for a new root password and store it. 

    Do

        szNewPassword = crt.dialog.prompt("Please enter the new password: ", "New Password", "", True)
        szConfirmPassword = crt.dialog.prompt("Please confirm your new password: ", "Confirm Password", "", True)

        if szNewPassword <> szConfirmPassword then
            MsgBox "User did not provide matching passwords"
        end if

    While szNewPassword <> szConfirmPassword

    '~ 11.) Send new password 
    crt.screen.Send szNewPassword & vbcr

    '~ 12.) Wait for "Re-enter new Password: " and send password again. 
    crt.Screen.WaitForString "Re-enter new Password:"
    crt.Screen.Send szNewPassword & vbcr

    '~ 13.) Wait for "passwd: password successfully changed for root" 
    crt.Screen.WaitForString "passwd: password successfully changed for root"

    '~ 14.) exit server and return to next server in list and then repeat steps 4-13 until the list has expired. 
    crt.Screen.Send "exit" & vbcr
    
Next
Does this look like it will help you?

Thank you

JJH
Reply With Quote
  #3  
Old 08-25-2008, 08:55 AM
darkoth darkoth is offline
Registered User
 
Join Date: Jun 2007
Posts: 29
Hrm. I'm getting an "unexpected 'next". I'm not all that versed with VB as of yet. FYI. I'm using Version 6.1 of SecureCRT

Code:
#$language = "VBScript"
#$interface = "1.0"

crt.Screen.Synchronous = True

Sub Main
	crt.Screen.Send "cat server_list.txt" & vbcr
	vServerList = Split(crt.Screen.ReadString("]$"), vbcr)

	For Each szHost in vServerList
    		crt.Screen.Send "ssh " & szHost & vbcr
    		Do
        		szResults = Crt.Screen.WaitforStrings("Are you sure...", "Password:", 60) = 1
        		Select Case szResult
            			Case 1
                			crt.Screen.Send "yes" & vbcr
            			Case 2
                			szPassword = crt.dialog.prompt("Please enter your password", "Password", "", True)
                			crt.Screen.Send szPassword & vbcr
					exit do
            			Case else
                			MsgBox "We timed out"
        		end select
    	Loop

	crt.Screen.Send "su -" & vbcr
    	crt.Screen.WaitForString "Password:"
    	szRootPassword = crt.dialog.Prompt("Please enter the root password: ", "Root Password", "", True)
    	crt.Screen.Send szRootPassword & vbcr
    	crt.Screen.Send "passwd root" & vbcr
    	Do
        	szNewPassword = crt.dialog.prompt("Please enter the new password: ", "New Password", "", True)
        	szConfirmPassword = crt.dialog.prompt("Please confirm your new password: ", "Confirm Password", "", True)

        	if szNewPassword <> szConfirmPassword then
            		MsgBox "User did not provide matching passwords"
        	end if

    		While szNewPassword <> szConfirmPassword
    			crt.screen.Send szNewPassword & vbcr
    			crt.Screen.WaitForString "Re-enter new Password:"
    			crt.Screen.Send szNewPassword & vbcr
    			crt.Screen.WaitForString "passwd: password successfully changed for root"
    			crt.Screen.Send "exit" & vbcr
	Next
End Sub
Also, what would be a good book to learn VBScript?

Last edited by darkoth; 08-25-2008 at 09:04 AM.
Reply With Quote
  #4  
Old 08-25-2008, 11:21 AM
jjh jjh is offline
VanDyke Customer Support
 
Join Date: Feb 2004
Posts: 815
I've made some changes to the script, which you can see in
the attached screenshot. As you can see, the following
changes were made:

We had originally defined "szRezults", but we were using
"szResult", so I changed "szResults" to "szResult" to match.

Next, I made a change to wait for the prompt before starting
the password change

The next changes I made were to exit the sub if the user
cancels the "New Password" or "Confirm Password" prompts.

The next change fixes a problem with the Do, Loop. I think
this is the actual problem you experienced.

Lastly, I made a change to wait for the prompt before
starting the process over again.

Here is the new version of the script:

Code:
#$language = "VBScript"
#$interface = "1.0"

crt.Screen.Synchronous = True

Sub Main
        crt.Screen.Send "cat server_list.txt" & vbcr
        vServerList = Split(crt.Screen.ReadString("]$"), vbcr)

        For Each szHost in vServerList
            crt.Screen.Send "ssh " & szHost & vbcr
            Do
                szResult = Crt.Screen.WaitforStrings("Are you sure...", "Password:", 60) = 1
                Select Case szResult
                        Case 1
                            crt.Screen.Send "yes" & vbcr
                        Case 2
                            szPassword = crt.dialog.prompt("Please enter your password", "Password", "", True)
                            crt.Screen.Send szPassword & vbcr
                            exit do
                        Case else
                            MsgBox "We timed out"
                            Exit sub
                end select
            Loop

            crt.Screen.Send "su -" & vbcr
            crt.Screen.WaitForString "Password:"
            szRootPassword = crt.dialog.Prompt("Please enter the root password: ", "Root Password", "", True)
            crt.Screen.Send szRootPassword & vbcr
            szResult = crt.screen.waitforstring ("]$", 10)
            If szResult = 0 then
                msgbox "Did not find root shell prompt."
                Exit sub
            End if
            crt.Screen.Send "passwd root" & vbcr
            Do
                szNewPassword = crt.dialog.prompt("Please enter the new password: ", "New Password", "", True)
                If szNewPassword = "" then exit sub
                
                szConfirmPassword = crt.dialog.prompt("Please confirm your new password: ", "Confirm Password", "", True)
                If szConfirmPassword = "" then exit sub
                
                if szNewPassword <> szConfirmPassword then
                        MsgBox "User did not provide matching passwords"
                Else
                    Exit Do
                end if
            Loop

                crt.screen.Send szNewPassword & vbcr
                crt.Screen.WaitForString "Re-enter new Password:"
                crt.Screen.Send szNewPassword & vbcr
                crt.Screen.WaitForString "passwd: password successfully changed for root"
                crt.Screen.Send "exit" & vbcr
                crt.screen.WaitForString "]$"
        Next
End Sub
Does this work better for you now?

JJH
Attached Images
File Type: gif ScriptDifferences.gif (33.7 KB, 762 views)
Reply With Quote
  #5  
Old 08-25-2008, 12:23 PM
darkoth darkoth is offline
Registered User
 
Join Date: Jun 2007
Posts: 29
I tried and now its actually doing this when I try to run it:

Code:
]$ cat server_list.txt
frodo
samwise

]$ ssh cat server_list.txt
ssh: cat: host/servname not known
I actually changed it based on the BasicReadStringExample script to this and also added a #$ line at the end of the file. It does see the servers right in the messagebox but now I'm getting a "VBScript runtime error - Error: Object not in a collection" error at the For Each line:

Code:
#$language = "VBScript"
#$interface = "1.0"

Sub Main

	Dim szPrompt, objTab

	szPrompt = "#$"
	Set objTab = crt.GetScriptTab
	objTab.Screen.Synchronous = True
	'objTab.Screen.IgnoreEscape = True

	szCommand = "cat server_list.txt"
        objTab.Screen.Send szCommand & vbcr

	objTab.Screen.WaitForString szCommand & vbcr
	vServerList = objTab.Screen.ReadString(szPrompt)
        'vServerList = Split(objTab.Screen.ReadString(), vbcr)
	crt.Dialog.Messagebox vServerList 

        For Each szHost in vServerList
            objTab.Screen.Send "ssh " & szHost & vbcr
            Do
                szResult = Crt.Screen.WaitforStrings("Are you sure...", "Password:", 60) = 1
                Select Case szResult
                        Case 1
                            objTab.Screen.Send "yes" & vbcr
                        Case 2
                            szPassword = crt.dialog.prompt("Please enter your password", "Password", "", True)
                            objTab.Screen.Send szPassword & vbcr
                            exit do
                        Case else
                            MsgBox "We timed out"
                            Exit sub
                end select
            Loop

            objTab.Screen.Send "su -" & vbcr
            objTab.Screen.WaitForString "Password:"
            szRootPassword = crt.dialog.Prompt("Please enter the root password: ", "Root Password", "", True)
            objTab.Screen.Send szRootPassword & vbcr
            szResult = objTab.screen.waitforstring ("]#", 10)
            If szResult = 0 then
                msgbox "Did not find root shell prompt."
                Exit sub
            End if
            objTab.Screen.Send "passwd root" & vbcr
            Do
                szNewPassword = crt.dialog.prompt("Please enter the new password: ", "New Password", "", True)
                If szNewPassword = "" then exit sub
                
                szConfirmPassword = crt.dialog.prompt("Please confirm your new password: ", "Confirm Password", "", True)
                If szConfirmPassword = "" then exit sub
                
                if szNewPassword <> szConfirmPassword then
                        MsgBox "User did not provide matching passwords"
                Else
                    Exit Do
                end if
            Loop

                objTab.screen.Send szNewPassword & vbcr
                objTab.Screen.WaitForString "Re-enter new Password:"
                objTab.Screen.Send szNewPassword & vbcr
                objTab.Screen.WaitForString "passwd: password successfully changed for root"
                objTab.Screen.Send "exit" & vbcr
                objTab.screen.WaitForString "]#"
        Next
End Sub

Last edited by darkoth; 08-25-2008 at 02:09 PM.
Reply With Quote
  #6  
Old 08-25-2008, 04:16 PM
jjh jjh is offline
VanDyke Customer Support
 
Join Date: Feb 2004
Posts: 815
I notice that you are using the following in your new code:

Code:
	objTab.Screen.WaitForString szCommand & vbcr
	vServerList = objTab.Screen.ReadString(szPrompt)
        'vServerList = Split(objTab.Screen.ReadString(), vbcr)
	crt.Dialog.Messagebox vServerList
In order to be able to iterate over each item is to create
an array, which is what the split does for you. If you
don't use split, you end up with just a string that doesn't
allow you to parse just the information you are looking for.
Try changing that portion of your code to look like this:

Code:
	objTab.Screen.WaitForString szCommand & vbcr
        vServerList = Split(objTab.Screen.ReadString(szPrompt), vbcr)
	' crt.Dialog.Messagebox vServerList
The reason why the crt.Dialog.MessageBox line is also
commented out is that since it is now an array, you'd
get an error attempting to display it as a regular string.

Does that work better for you?

JJH

Last edited by jdev; 08-25-2008 at 05:30 PM.
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 03:30 AM.