#1
|
|||
|
|||
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. |
#2
|
|||
|
|||
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 Thank you JJH |
#3
|
|||
|
|||
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 Last edited by darkoth; 08-25-2008 at 09:04 AM. |
#4
|
|||
|
|||
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 JJH |
#5
|
|||
|
|||
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 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. |
#6
|
|||
|
|||
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 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 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. |
#7
|
|||
|
|||
Nope. Now I'm getting this when I run with the new lines:
Code:
]# cat server_list.txt frodo samwise #$ ]# ssh frodo Usage: ssh [options] host [command] Options: -l user Log in using this user name. -n Redirect input from /dev/null. -F config Config file (default: ~/.ssh/config). -A Enable authentication agent forwarding. -a Disable authentication agent forwarding (default). -X Enable X11 connection forwarding. -x Disable X11 connection forwarding (default). -i file Identity for public key authentication (default: ~/.ssh/identity) -t Tty; allocate a tty even if command is given. -T Do not allocate a tty. -v Verbose; display verbose debugging messages. Multiple -v increases verbosity. -V Display version number only. -q Quiet; don't display any warning messages. -f Fork into background after authentication. -e char Set escape character; ``none'' = disable (default: ~). -c cipher Select encryption algorithm -m macs Specify MAC algorithms for protocol version 2. -p port Connect to this port. Server must be on the same port. -L listen-port:host:port Forward local port to remote address -R listen-port:host:port Forward remote port to local address These cause ssh to listen for connections on a port, and forward them to the other side by connecting to host:port. -D port Enable dynamic application-level port forwarding. -C Enable compression. -N Do not execute a shell or command. -g Allow remote hosts to connect to forwarded ports. -1 Force protocol version 1. -2 Force protocol version 2. -4 Use IPv4 only. -6 Use IPv6 only. -o 'option' Process the option as if it was read from a configuration file. -s Invoke command (mandatory) as SSH2 subsystem. -b addr Local IP address. ]# frodo ksh: frodo: not found Code:
#$language = "VBScript" #$interface = "1.0" Sub Main Dim szPrompt, objTab, szHost, vServerList 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 = Split(objTab.Screen.ReadString(szPrompt), vbcr) 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-26-2008 at 09:04 AM. |
#8
|
|||
|
|||
I think the problem you are experiencing now is that we are
splitting the line at a vbcr, but each line actually contains a vbcrlf. You should be able to get rid of any extra cr's or lf's if you make the following three lines the first lines after your "For Each szHost in vServerList" line: Code:
' Get rid of any carriage returns or line feeds that still may be ' remaining in szHost szHost = replace(replace (szHost, vblf, ""), vbcr, "") Thanks JJH |
#9
|
|||
|
|||
password script
Ok. it hung on the first part so I changed it to get it working through that. I also moved all the password prompts to the start of the script so it flows faster.
I thought I had it working but... it gets all the way to the end and then tried to ssh to #$, so I changed the script to replace #$ from the end of the server_list.txt file and of course now it tries to ssh to nothing... Any ideas on how to wrap this up? Is there something we can do to strip the last line of the file from the array and then replace? New Script below: Code:
#$language = "VBScript" #$interface = "1.0" Sub Main Dim szPrompt, objTab, szHost, vServerList szPrompt = "#$" Set objTab = crt.GetScriptTab objTab.Screen.Synchronous = True objTab.Screen.IgnoreEscape = True Do szPassword = crt.dialog.prompt("Please enter your password: ", "Password: ", "", True) If szPassword = "" then exit sub szRootPassword = crt.dialog.Prompt("Please enter the current ROOT password: ", "Root Password", "", True) If szRootPassword = "" then exit sub szNewPassword = crt.dialog.prompt("Please enter the new ROOT password: ", "New Password", "", True) If szNewPassword = "" then exit sub szConfirmPassword = crt.dialog.prompt("Please confirm the new ROOT 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 szCommand = "cat server_list.txt" objTab.Screen.Send szCommand & vbcr objTab.Screen.WaitForString szCommand & vbcr vServerList = Split(objTab.Screen.ReadString(szPrompt), vbcr) For Each szHost in vServerList ' Get rid of any carriage returns or line feeds that still may be ' remaining in szHost szHost = Replace(szHost, "#$", "") szHost = Replace(Replace(szHost, vblf, ""), vbcr, "") objTab.Screen.Send "ssh " & szHost & vbcr Do Dim szResult szResult = Crt.Screen.WaitforStrings("Are you sure you want to continue connecting (yes/no)? ", "Password:", 60) If szResult = 1 Then objTab.Screen.Send "yes" & vbcr objTab.Screen.WaitforString "Password: " objTab.Screen.Send szPassword & vbcr exit do End If If szResult = 2 Then objTab.Screen.Send szPassword & vbcr exit do End If If szResult = 0 Then MsgBox "We timed out" Exit sub End if Loop objTab.Screen.WaitforString "TERM = (xterm) " objTab.Screen.Send "vt100" & vbcr objTab.Screen.WaitforString " ]# " objTab.Screen.Send "su -" & vbcr objTab.Screen.WaitForString "Password:" objTab.Screen.Send szRootPassword & vbcr objTab.Screen.WaitforString "TERM = (xterm) " objTab.Screen.Send "vt100" & vbcr objTab.Screen.WaitforString " ]# " objTab.Screen.Send "passwd root" & vbcr ObjTab.Screen.WaitForString "New Password: " 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 "]#" objTab.Screen.Send "exit" & vbcr objTab.screen.WaitForString "]#" Next MsgBox "Script Completed." End Sub Last edited by darkoth; 08-27-2008 at 09:37 AM. |
#10
|
|||
|
|||
I was thinking I could just add this
Code:
For i = 0 to UBound(szHost) - 1 Code:
objTab.Screen.Send "ssh " & szHost & vbc |
#11
|
|||
|
|||
You could try inserting something like the following after the "For" statment:
Code:
if left(szHost,1) <> "#" then 'the rest of the code lines here 'and the following line before the "Next" statement end if Wes |
#12
|
|||
|
|||
Hi Darkoth.
Along the lines of what Wes said, you might want to change his first line to be slightly more robust to deal with empty lines or lines that include your prompt. Something like the following: If instr(szHost, "$>") = 0 and trim(szHost) <> "" then Does that help? Thanks JJH |
#13
|
|||
|
|||
Yup. Works like a champ now. Thanks
|
#14
|
|||
|
|||
Thanks for letting me know. I'm glad you have something
that is working for you now. If you have other questions, please feel free to post here again or send e-mail to support@vandyke.com JJH |
![]() |
Thread Tools | |
Display Modes | Rate This Thread |
|
|