View Single Post
  #4  
Old 10-16-2019, 06:10 PM
ashiosee ashiosee is offline
Member
 
Join Date: Oct 2019
Posts: 24
Hi mmckeehan,

I'm not sure I have your current script code, but if it's anything like the prior script code you shared, I think you have an ordering problem. You're waiting for the results of the 'ssh' command before you've even sent the 'ssh' command.

Code:
  Set file = fso.OpenTextFile("C:\Hostfiles\scphosts.txt")

  crt.Screen.Synchronous = True

  Do While file.AtEndOfStream <> True

    str = file.Readline
     
   nResult = crt.Screen.WaitForStrings (result, 30)

    ' the below will send the commands shown and send the line with an appended CR
    ' note the wait for system prompt between each command this is required or the script will run to fast and not execute commands properly on the servers.

    ' check the failed login and reset then set the password, unlock the password then set it temp
    ' just edit the user ID
    '
    crt.Screen.Send "ssh -n -q " & str & " 'mkdir .ssh'" &chr(13)
     Select Case nResult
          Case 1
               'File Exist:  This will loop the process'
               crt.Screen.Send "ssh -n -q " & str & " 'mkdir .ssh'" &chr(13)
     
          Case 2
               'Asking for password: this will continue the process'
               crt.Screen.Send "password" &chr(13)
               crt.Screen.WaitForString "$"
               crt.Screen.Send "scp /local_home/uname/.ssh/authorized_keys uname@" & str & ":/local_home/uname/.ssh/authorized_keys" &chr(13)
               crt.Screen.WaitForString "sword:"
               crt.Screen.Send "password" &chr(13)
               crt.Screen.WaitForString "$"
          End Select
          
Loop
In general, it's not going to work if you first wait for a response to a command that you have not yet sent.

One fix would be to move your WaitForStrings() call down to the line AFTER the line where the prerequisite ssh command has been sent. For example:
Code:
  Set file = fso.OpenTextFile("C:\Hostfiles\scphosts.txt")

  crt.Screen.Synchronous = True

  Do While file.AtEndOfStream <> True

    str = file.Readline
     
    ' the below will send the commands shown and send the line with an appended CR
    ' note the wait for system prompt between each command this is required or the script will run to fast and not execute commands properly on the servers.

    ' check the failed login and reset then set the password, unlock the password then set it temp
    ' just edit the user ID
    '
    crt.Screen.Send "ssh -n -q " & str & " 'mkdir .ssh'" &chr(13)

   nResult = crt.Screen.WaitForStrings (result, 30)
     Select Case nResult
          Case 1
               'File Exist:  This will loop the process'
               crt.Screen.Send "ssh -n -q " & str & " 'mkdir .ssh'" &chr(13)
     
          Case 2
               'Asking for password: this will continue the process'
               crt.Screen.Send "password" &chr(13)
               crt.Screen.WaitForString "$"
               crt.Screen.Send "scp /local_home/uname/.ssh/authorized_keys uname@" & str & ":/local_home/uname/.ssh/authorized_keys" &chr(13)
               crt.Screen.WaitForString "sword:"
               crt.Screen.Send "password" &chr(13)
               crt.Screen.WaitForString "$"
          End Select
          
Loop
Note that I've highlighted the timeout parameter (30) for your attention. Your scenario doesn't warrant the use of any timeout parameters -- after all, you're not even handling the case where WaitForStrings() times out.

If you feel you must keep the time out in place, you should at least add a Case 0 statement and corresponding block aimed to help you bail -- if that's what you want to do. For example:

Code:
          Case 0
               ' Timed out waiting for one of our strings. Gotta bail.
               crt.Dialog.MessageBox "Timed out waiting for one of our strings. Stopping the script before things get worse."
               Exit Sub
__________________
--Allen

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