[Gambas-user] How to interrupt a process waiting for a response in non interactive application
Benoît Minisini
g4mba5 at gmail.com
Thu Mar 24 00:54:15 CET 2022
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,
--
Benoît Minisini
More information about the User
mailing list