Send Remote and read from output (SSH2 subsystem)

Aug 4, 2011 at 11:22 AM

Hi

i have problem to figure out, how to send remote command to ssh and then how to read from servers output. Is it possible to do that with this library. Because I cant figure out how to do that.

My problem:

I know how to open ssh2 connection but then I need send remote comand to server to open ssh2 subsystem and then continuously send input and read output (in my case send a read xml tags) and again and agai to do that.

example in plink.exe

plink ipaddress -P 722 -l username -i PrivateKey.ppk -s command

UNIX example

ssh -l username -i PrivateKey.ppk -p ipaddress -s command 

send command  to server is my problem.

 

I will be very greatfull for any help

Thanx a lot

Coordinator
Aug 5, 2011 at 1:45 PM

If all you want to do is to execute single command and get output then you can use SshCommand for that.

Here is an example:

            using (var ssh = new SshClient(connectionInfo))
            {
                ssh.Connect();
                var cmd = ssh.RunCommand("echo 123");
                Console.WriteLine(cmd.Result);
                ssh.Disconnect();
            }

There is support for more complex scenarios if needed.

Also,

Can you please explain more about where and how you would like to use subsystem? What specific command?

The only one I am aware of right now is "sftp" but may be I can extend support for other commands too.

 

Thanks,

Oleg

Aug 17, 2011 at 6:57 AM
Edited Aug 22, 2011 at 10:38 AM

Thanks for your reply Oleg 

First of all I want to apology, that i didn't respond for long time, but I was on vacation, sorry

My scenario is bellow

Thanks for your time

Staro

Aug 22, 2011 at 10:36 AM
Edited Aug 22, 2011 at 10:37 AM
olegkap wrote:

If all you want to do is to execute single command and get output then you can use SshCommand for that.

Here is an example:

 

            using (var ssh = new SshClient(connectionInfo))
            {
                ssh.Connect();
                var cmd = ssh.RunCommand("echo 123");
                Console.WriteLine(cmd.Result);
                ssh.Disconnect();
            }

 

There is support for more complex scenarios if needed.

Also,

Can you please explain more about where and how you would like to use subsystem? What specific command?

The only one I am aware of right now is "sftp" but may be I can extend support for other commands too.

 

Thanks,

Oleg

Thanks for your reply Oleg



First of all I want to apology, that i didn't respond for long time, but I was on vacation, sorry

My scenario:

  1. ssh connection with private key 
  2. then send remote command linterface(I assume that it is some program on remote server)
  3. this program send me xml structure ( in plain text format)
  4. I need to read it (output), do something with XML and send another xml tag (plain text)
  5. the server recognize this tag and send be another xml text based on what i sent.
  6. This I need to do in 15min intervals, all the time without disconnection. 

 


This is basic idea of functionality.



EDIT: when I use right connectionInfo a I trie to debug program, after ssh.Connect only terminal show and I can't continue debug program because terminal shows only blink cursor. It it when ssh wait to responce from server??

EDIT: the subsystem linterface seems to be program like sftp. Is it any chance to connect like sftp but with other comamnd name (linterface) ?



Thanks for your time

Staro

Coordinator
Aug 22, 2011 at 12:34 PM

ok,

so I I think what I would do, eventually, is to create some base class where SFTP will inherit from it,

this will give an opportunity to create other subsystems more easily.

For now everything is hardcoded.

 

If you like you can play with SftpClient or actuallly with SftpSession, where all functionality is defined and let me know if you have any problem.

 

Another thing I would like to ask, since I lack knowledge in this area, is how can I have similar utility here, so I could test it correctly.

May be not linterface but something else, with similar behaviour.

 

Thanks,

Oleg

Coordinator
Aug 22, 2011 at 1:53 PM

In fact,

I just quickly split SftpSession class into 2 so you could take a look and implement your own.

Please take a look at 9631 code set and how I implemented SftpSession class by inhereting it from SubsystemSession class.

 

Hope it moves you into right direction,

Let me know if you have any problems,

 

Thanks,

Oleg

Aug 22, 2011 at 3:03 PM
Edited Aug 22, 2011 at 3:06 PM

thank you for quick reply Oleg

I'll try to code some experiments and post some experience, but for now I'm not very familiar with code that is why I must study the code.

 

EDIT: I don't know much about ssh2 subsystem either, but step by step I'm trying to figure out how it works. But this was first time I heard about it.

Thakns,

Staro

 

 

Aug 24, 2011 at 1:52 PM
Edited Aug 24, 2011 at 1:53 PM

Hi Olge

I tried to derivate from SubsystemSession.cs based on SftpSession. I thing I managed it right, but I have another break.

When I tried to debug where is the fail, I stoped at class PrivateKeyConnectionInfo.cs at line 85

80   if (this.KeyFiles.Count < 2)
81   {
82       // If only one key file provided then send signature for very first request
83       var signatureData = new SignatureData(message, this.Session.SessionId).GetBytes();
84
85       message.Signature = keyFile.HostKey.Sign(signatureData);
86   }

I assume that I have something with private key. Is it posible? Could you explain me (a bit) what goes in this condition.

I must generate  privat ekey in putygen and that is why in puttygen I converted private key  to OpenSSH format. do you have some experience whit it?

EDIT: I tried to connect with converted key in Linux and connection was successful.

Thanks a lot,

Staro

Coordinator
Aug 24, 2011 at 2:00 PM

what happens here is to cover the case where user have multiple keys for authentication purposes,

in this case process is a little different, I only send signature for the key which server accepts

but in majority of cases, like the one you described here, where only one private key file is specified, I send signature in the same request/packet to save on packet transmition,

this way I don't have to make another similar request with signature information.

Mar 13, 2012 at 1:14 PM

Hi Oleg,

I think I'm currently having the exact same problem that staro described on Aug 22 2011 at 11:36 AM.

I have to connect to a linux based service via SSH protocol, requesting invocation of a custom subsystem on the remote system. The subsystem (let's call it on its name, linterface, but just to point out that it's not sftp, for example) runs as a daemon, and as long as the connection is open, it receives data (as xml stream) on its standard input and emits data (as xml stream) on its standard output through the ssh channel/tunnel.

Like staro said, using a unix/linux shell command line command, it looks like this:

ssh -l username -i PrivateKey.ppk -p ipaddress -s linterface

Can you, by any chance, help me on how to accomplish this task (the "requesting invocation of a custom subsystem on the remote system" part)?

Thanks in advance,

airdeux

Coordinator
Mar 13, 2012 at 1:21 PM

Hi,

 

There is a SubSystem class which servers as a base class for all subsystems.

Then you can inherit from it and implement you specific functionlity.

As an example you can use SftpSession and SftpClient classes as well as NetConfSession and NetConfClient classes.

By the way, NetConf* classes were implemented by user that eventually was contributed to the library and I incorporated it so you might want to take similar approach as he did.

 

Hope it helps,

Thanks,

Oleg

Mar 13, 2012 at 1:56 PM

Hi,

Thanks a lot for your very quick response, and all the information within.

I will do check the source codes of SftpSession, SftpClient, and NetConf* classes and see how far I'll get.. :)

I think I've actually run across the other discussion you are referencing here (SubsystemSession is internal, discussion #277136), but just like this one, that one also seemed somewhat unfinished (abandoned without resolution) to me, and unfortunately I haven't checked in the source code if the mentioned modifications took place or not.

Thanks again, I'll get back when I come up with something that might be worth sharing,

airdeux