Home » U++ Library support » U++ MT-multithreading and servers » Why SshExec close the connexion ?
Why SshExec close the connexion ? [message #53074] |
Thu, 20 February 2020 18:32  |
omari
Messages: 276 Registered: March 2010
|
Experienced Member |
|
|
Hi Oblivion,
i want to automate file deployment using ssh,
SshSession session;
session.Connect(...);
SshExec exec(session);
Scp scp(session);
exec.Execute("mkdir target/");
scp.SaveFile("target/binary", LoadFile(localFile));
exec.Execute("target/binary&");
but, the first use of SshExec close the session .
how can i achieve this, and keep the session open after remote command execution ?
thanks in advance.
regards
omari.
|
|
|
Re: Why SshExec close the connexion ? [message #53077 is a reply to message #53074] |
Sat, 22 February 2020 11:18   |
Oblivion
Messages: 1206 Registered: August 2007
|
Senior Contributor |
|
|
Hello Omari,
Quote:but, the first use of SshExec close the session .
In SSH2 protocol, exec channels are fire-and-forget type channels - they are unique to single execution target.
The protocol does not allow the reuse of exec channels.
That's why you need to create a new SshExec instance for each command instance. (They are relatively cheap).
So what you need to do is:
String cout, cerr;
SshExec ExecMkDir(session);
SshExec ExecRun(session);
Scp scp(session);
ExecMkDir("mkdir target/", cout, cerr);
scp.SaveFile("target/binary", LoadFile(localFile));
ExecRun("target/binary&", cout, cerr);
Yet, as mentioned in SSH package's docs, If you need batch operations, I'd recommend SshShell instead.
OTOH, server does not close the connection on my setups (OpenSSH/sshd). It simply fails to reuse the exec channel.
This may be due to different server implementations, I am not sure.
Now if you are talking about the session, then you have to remember that all SSH objects are scope-bound, including the session. Typical C++ rule.
Still, the output of a brief trace (Ssh::Trace()) would be more helpful.
If you have any other questions regarding SSH package, or run into any other issues, feel free to ask. 
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Sat, 22 February 2020 13:05] Report message to a moderator
|
|
|
Re: Why SshExec close the connexion ? [message #53082 is a reply to message #53077] |
Mon, 24 February 2020 10:31   |
omari
Messages: 276 Registered: March 2010
|
Experienced Member |
|
|
Thanks Oblivion,
I have confused channel and session, I have thought that the session is closed after each SshExec call.
based on your response, i create this helper class :
struct SshExecHelper
{
SshSession& session;
SshExecHelper(SshSession& _session): session(_session){}
int operator()(const String& cmd, String& out, String& err)
{
SshExec e(session);
return e.Execute(cmd, out, err);
}
}
regards
omari.
|
|
|
Re: Why SshExec close the connexion ? [message #53084 is a reply to message #53082] |
Mon, 24 February 2020 13:41   |
Oblivion
Messages: 1206 Registered: August 2007
|
Senior Contributor |
|
|
Hello Omari,
I made your suggestion into three helper functions, instead of a structure, similar to Upp::sys:
int SshExecute(SshSession& session, const String& cmd, String& out, String& err);
int SshExecute(SshSession& session, const String& cmd, String& out);
String SshExecute(SshSession& session, const String& cmd);
I've committed the changes to Upp-Svn. So they'll be available in Upp nightly builds.
Best regards,
Oblivion.
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
|
|
|
|
Goto Forum:
Current Time: Fri May 09 22:28:48 CEST 2025
Total time taken to generate the page: 0.00926 seconds
|