0 votes
by (170 points)

I would like to be able to receive and send commands as XML strings via SSH. But I receive only "<?xml" instead of the whole XML string.

Why XML: because my client is an application.

My server init:

_secureServer = new FileServer();
_secureServer.Bind(2222, FileServerProtocol.Shell);
_secureServer.Bind(2222, FileServerProtocol.Sftp);
...
_secureServer.ShellCommand += server_ShellCommand;
 _secureServer.Start();

Receive function:

void server_ShellCommand(object sender, ShellCommandEventArgs e)
{
  if (e.Command.Equals("sh"))
    return;

   string command = e.Command;
}

I'm sending a normal XML string beginning with

<?xml version="1.0" encoding="utf-16"?>
<ShellCommandBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-.....

Before sending it to the server I remove all new lines.

Makes no sense why it ends after a space??

1 Answer

0 votes
by (73.5k points)

The remote behaves as UNIX-like shell. It means that:

  1. commands are separated by new line.
  2. command is the first word on the line, then command arguments follows.

In your case, the commands are:

  1. <?xml and arguments are version="1.0" encoding="utf-16"?>
  2. <ShellCommandBase and arguments are the rest of line xmlns:xsi=...

To achieve what you need, I suggest you to:

  1. Upload the XML using SFTP, to some directory under unique name.
  2. Run custom command to execute uploaded XML, e.g. runxml unique-name-from-previous-step

Alternatively, you can read the XML in custom runxml command like this:

    if (e.Command == "runxml")
    {
        e.Action = new Func<string[], SshConsole, int>((args, console) =>
        {
            while (true)
            {
                console.WriteLine("Waiting for XML:");

                var line1 = console.ReadLine();
                var line2 = console.ReadLine();

                var xml = new XmlDocument();
                xml.LoadXml(line1 + line2);

                console.WriteLine("XML OK");

                return 0;
            }
        });
    }

Please note, that for the above example the input XML has to end with new line.

by (170 points)
Ahh, just thinking about the new line, not the command + arguments. Thanks a lot!
...