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 06-11-2018, 04:39 PM
BjoernH BjoernH is offline
Registered User
 
Join Date: Mar 2018
Posts: 19
Question regarding multiple logins using vbscripting

Good day!

I've been trying to write a script which will log into a list of servers, execute a command and then exit, log into the next server in the list and run the same command.

The script works partly in that it will run Case 3 (no password required), access all 9 servers, however, when it exits the last one, the Select case fails and it attempts to execute from Case 2.


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

crt.Screen.Synchronous = True

Sub Main()

Dim vHosts(9)

vHosts(0) = "192.168.210.50"
vHosts(1) = "192.168.210.51"
vHosts(2) = "192.168.210.52"
vHosts(3) = "192.168.210.53"
vHosts(4) = "192.168.210.54"
vHosts(5) = "192.168.210.55"
vHosts(6) = "192.168.210.56"
vHosts(7) = "192.168.210.143"
vHosts(8) = "192.168.210.144"



Dim SysCtlCmd
SysCtlCmd = crt.Dialog.Prompt("Please enter the required action (uname -n, hostname, df -h):")
	If SysCtlCmd = ""  Then Exit Sub


Auth = 	InputBox("Please enter the system root password:","","*")
	If Auth = ""  Then Exit Sub


For Each strHosts In vHosts
    If strHosts = "" Then Exit For

    crt.Screen.Send"ssh -q " & strHosts & chr(13)

Dim vWaitFors
vWaitFors = Array("(yes/no)?", _
					"password:", _
					">")
					
Do
	Dim nResult
	nResult = crt.Screen.WaitForStrings(vWaitFors, 5)

	Select Case nResult
		Case 0
'			MsgBox "Timed out waiting for strings!"
			Exit Do

		Case 1
		MsgBox " Case 1, yes or no "
			crt.Screen.Send"yes" & chr(13)
		If	crt.Screen.WaitForString("password:", 3) Then
			MsgBox " Case 1, password required. "
			crt.Screen.Send Auth & vbCr
			crt.Screen.WaitForString">"
		Else
			MsgBox " Case 1, no password required. "
			crt.Screen.WaitForString">"
		End If
		
		Case 2
			MsgBox " Case 2, password only. "
			crt.Screen.Send Auth & vbCr
			crt.Screen.WaitForString">"

		Case 3
			MsgBox " Case 3, no yes or password required.  "
			crt.Screen.Send"" & SysCtlCmd & chr(13)
			crt.Screen.WaitForString">"
			crt.Screen.Send"exit" & chr(13)
			crt.Screen.WaitForString">"


	end Select

Loop


' crt.Sleep 5

Next

End Sub
Any ideas?

Bjoern
Reply With Quote
  #2  
Old 06-12-2018, 08:58 AM
berdmann berdmann is offline
VanDyke Technical Support
 
Join Date: Aug 2017
Posts: 55
Hi BjoernH,

When you use ssh -q, you're hiding errors that could potentially be displayed (no route to host).

Some hosts may present the password prompt as "password:" while others may display it as "Password:" (case matters). You array should include all possible varieties and handle them appropriately.

The main do..loop you have is from an example that is designed to handle all of the interaction required for a particular host. You should not have any nested WaitForString() calls inside of the do..loop; these nested WaitFortString calls should be removed.

Ideally, your gateway/jumphost should have a different shell prompt than any of the secondary hosts to which you're attempting to connect. This way, you can tell the difference between a successful connection to a secondary host from a failed connection (that simply drops you back at your primary host's prompt).
__________________
Thanks,
--Brittney

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 06-12-2018, 09:47 AM
BjoernH BjoernH is offline
Registered User
 
Join Date: Mar 2018
Posts: 19
Hi Brittney.

We haven't seen any issues using -q as our servers are pretty reliable, however, I will remove the -q just in case something else comes up. I'll also change the "password:" to "assword:".

The reason why I attempted to nest the WaitForString is because sometimes when we are prompted for a 'yes/no' to save known hosts, the next result may be a prompt for the password, but not always. So if a WaitForString can't be nested in the loop, how else would you handle this case?

Unfortunately, the prompts for root user are all the same (>).

Regards,
Bjoern

Last edited by BjoernH; 06-12-2018 at 10:17 AM. Reason: Sorry, got your name wrong.
Reply With Quote
  #4  
Old 06-12-2018, 01:20 PM
berdmann berdmann is offline
VanDyke Technical Support
 
Join Date: Aug 2017
Posts: 55
Hi BjoernH,

The do...loop is designed to handle all of the prompts for a specific host, as each prompt is detected, no matter the order in which the prompts arrive. This is why you don't need any nested WaitForStrings. You're already WaitForStrings()'ing at the top of the do..loop block. When you find something you need to handle, you handle it -- that's what the do..loop is intended to, well, 'do'.

The importance of having a shell prompt on the primary host that is different than the secondary host is crucial for knowing whether or not the ssh command succeeded or failed, and also for knowing when the 'exit' command (once successfully ssh'd in to a secondary host) has completed and one can move on to the next host.

You may want to manually set the prompt on the primary host as part of the script *before* going into the for..next loop.

For example:

Code:
 ' Set our shell prompt on the primary host so that we know where we are:
 strPrimPrompt = "__PRIMARY_HOST_PROMPT__# "
 crt.Screen.Send "PS1='" & strPrimPrompt & "'"
This, of course, would be temporary and the shell prompt would last only as long as this logon session to the primary host. Once disconnected and reconnected, the prompt would return to standard/default.

Then, inside of your do..loop, one of the elements in the vWaitFors array would be the strPrimPrompt which, if found after running the ssh command would mean that the ssh command failed and you would need to exit the do..loop (cycling back to the next item in the for..next loop). The strPrimPrompt variable would also be what is WaitForString'd after sending the 'exit' command after successfully connecting to a secondary host and running the system command so that you will know when you are back at the primary host's prompt.
__________________
Thanks,
--Brittney

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 06-15-2018, 10:22 AM
BjoernH BjoernH is offline
Registered User
 
Join Date: Mar 2018
Posts: 19
Hi Brittney.

Good idea changing the prompt! I'll try that and your recommendation regarding the WaitForStrings as soon as I get a chance.


Happy weekend!

Bjoern
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 05:12 PM.