[Gambas-user] How to interrupt a process waiting for a response in non interactive application

Marc Guillaume alarch at alarch.pw
Thu Mar 24 02:21:31 CET 2022


Le Thu, 24 Mar 2022 00:54:15 +0100,
Benoît Minisini <g4mba5 at gmail.com> a écrit :

> Le 24/03/2022 à 00:20, Marc Guillaume a écrit :
> > Hello,
> > 
> > I am struggling with the management of a process. I want to launch a
> > ssh command on remote servers (more than 200 servers), connections
> > using an ssh key.
> > 
> > The server's key on which the gambas application is running is
> > normally deployed on the servers via ansible from another machine.
> > 
> > However sometimes the ssh configuration is faulty and does not allow
> > ssh key connection, and ask for a password. I don't have control
> > over this process.
> > 
> > The problem occurs if the server is configured to ask for a
> > password. In this case the process remains blocked indefinitely. I
> > would like to be able to abandon it after a timeout value that I
> > can determine in the configuration. If the server is unknow I get
> > the 255 error return, but il y have accepted it's key in
> > known_hosts an it ask for a password the process hang.
> > 
>  > [...]  
> 
> To deal with the 'ssh' command, you must use the 'gb.util' component 
> that adds an "Expect()" method to the process class that handles 
> automatic detection of command prompts and answers.
> 
> To deal with the timeout, you must use the "Timeout" argument of the 
> "Wait()" method of the Process class.
> 
> So you must do something like that (adapt to your needs!):
> 
> --8<-----------
> 
> hProcess = Exec ["ssh", "-v", "-o", "ConnectTimeout=10", "-p", "22", 
> "-q", "<user>@<server>"] For Input Output as "SshProcess"
> 
> ' Note: "For Input Output" is needed, as ssh needs a virtual terminal.
> 
> hProcess.Expect("(yes/no*)?", "yes")
> hProcess.Expect(":", "TheSshPassword")
> 
> hProcess.Wait(MyTimeout)
> 
> Public Sub SshProcess_Read()
>    ...
> End
> 
> --8<-----------
> 
> Regards,
> 

thanks for your help!

It is better to use exec according to what I read instead of shell to
save resources or in this case shell is really not adapted? 

I see that expect only appears with version 3.15 and for the moment I'm
stuck in 3.14 on the PPA of focal.

But already with the wait method I am not blocked anymore. And as I
don't have the passwords of the servers anyway, expect is not essential.

By keeping only the last line returned by the connection request and by
searching if it ends with : (the password request) I can eliminate this
case and not be blocked anymore. I need to do more tests but this code
seems to work well: 

Public Sub Button1_Click()

  Dim sCommande As String
  Dim iRetproc As Integer

  sAffichage = ""
  hConProcess = Exec ["ssh", "-p", "22", "-q", "root@" & TextBox1.Text]
  For Input Output As "SshProcess" hConProcess.Wait(15)
  If RegExp.Match(Trim(sAffichage), ".*:$") Then
    iRetproc = 1
  Else
    iRetproc = hConProcess.Value
  Endif
  If hConProcess.State <> 0 Then
    hConProcess.Kill
  Endif
  If iRetproc = 0 Then
    TextBox2.text = "connexion confirmée"
  Else
    TextBox2.text = "connexion échouée ou en timeout " 
  Endif
  
  Catch
    Print Error.Text

End


Public Sub SshProcess_Read()
  
  Dim sLine As String
    
  sLine = Read #Last, -256
  sAffichage = sLine
  
End


More information about the User mailing list