Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 Go to next message
omari is currently offline  omari
Messages: 264
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 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1091
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. Smile

Best regards,
Oblivion






[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 Go to previous messageGo to next message
omari is currently offline  omari
Messages: 264
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 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1091
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.


Re: Why SshExec close the connexion ? [message #53094 is a reply to message #53084] Tue, 25 February 2020 13:00 Go to previous message
omari is currently offline  omari
Messages: 264
Registered: March 2010
Experienced Member
Thanks Oblivion.

regards
omari.
Previous Topic: HttpRequest cannot get the server [SOLVED]
Next Topic: [NOT REPRODUCIBLE] HttpRequest gzip format error
Goto Forum:
  


Current Time: Thu Mar 28 15:13:03 CET 2024

Total time taken to generate the page: 0.01596 seconds