[jira] [Commented] (SSHD-744) An SSH client receives an echo as a response from the server
[ https://issues.apache.org/jira/browse/SSHD-744?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=15997074#comment-15997074 ] Miron Aseev commented on SSHD-744: -- No biggie. Thanks. > An SSH client receives an echo as a response from the server > > > Key: SSHD-744 > URL: https://issues.apache.org/jira/browse/SSHD-744 > Project: MINA SSHD > Issue Type: Question >Affects Versions: 1.4.0 >Reporter: Miron Aseev >Priority: Minor > > I'm trying to integrate my Java application with a device which provides a > custom protocol over SSH. > The whole point of this protocol is about exchanging XML documents back and > forth. It can be described with the following steps: > # a client connects to the server > # the client sends an XML document which contains the request data > # the client waits for the XML document which contains a response data from a > server > # the server receives the XML document that was sent by the client and sends > the response XML document back to the client > # the client receives the XML response from the server and closes the > connection > The recommended way to send requests to this device is to use `cat` and `ssh` > tools. Here's a simple example: > {code} > cat example.xml | ssh @ > {code} > where `example.xml` is a file that contains an XML document with the request > data. > Calling directly 'cat' and 'ssh' via Runtime.exec would be very problematic > because this approach has a lot of drawbacks. So, I decided to implement this > protocol using MINA SSHD library. > The problem I've run into is I receive my own request as a response from the > server. So there's some kind of echoing stuff going on, I think. > For example, when I send this XML document as a request to the server: > {code} > > > > test > > > {code} > I receive the following data as a response: > {code} > Last login: Wed May 3 07:33:43 2017 from ip > Header > > > > test > > > {code} > The funny this is when I send the same request via cat and ssh (the example I > mentioned above), I receive the correct response from the device. > Here's an excerpt from my application: > {code} > public class Test { > private static final long AWAIT_TIMEOUT = TimeUnit.SECONDS.toMillis(5); > private static final String XML_PAYLOAD_HEADER = " public void init() throws IOException, JAXBException { > try (SshClient client = initSshClient(); > ClientSession session = >initClientSession(client, "test", "localhost", 22, "pass")) { > sendRequest(session); > } > } > private static SshClient initSshClient() { > SshClient client = SshClient.setUpDefaultClient(); > client.start(); > return client; > } > private ClientSession initClientSession(SshClient client, String user, > String host, int port, > String password) throws IOException > { > ClientSession session = > client.connect(user, host, port).verify(AWAIT_TIMEOUT).getSession(); > session.addPasswordIdentity(password); > session.auth().verify(AWAIT_TIMEOUT); > return session; > } > private void sendRequest(ClientSession session) throws IOException { > try (PipedOutputStream out = new PipedOutputStream(); > PipedInputStream channelIn = new PipedInputStream(out); > PipedOutputStream channelOut = new PipedOutputStream(); > PipedInputStream in = new PipedInputStream(channelOut); > BufferedReader reader = new BufferedReader(new > InputStreamReader(in)); > BufferedWriter writer = new BufferedWriter(new > OutputStreamWriter(out)); > ClientChannel channel = initClientChannel(session, channelIn, > channelOut)) { > String xmlRequest = ""; // Here goes a real XML document; > writer.write(xmlRequest); > writer.newLine(); > writer.flush(); > String response = readFully(reader); > } > } > private static ClientChannel initClientChannel(ClientSession session, > InputStream input, > OutputStream output) throws > IOException { > ClientChannel channel = session.createChannel(Channel.CHANNEL_SHELL); > channel.setIn(input); > channel.setOut(output); > channel.setErr(new ByteArrayOutputStream()); > channel.open().verify(AWAIT_TIMEOUT); > return channel; > } > private static String readFully(BufferedReader reader) throws IOException { > StringBuilder builder = new StringBuilder(); > String line; > boolean foundXmlPayload = false; > while ((line = reader.readLine()) != null) { > if (foundXmlPayload && line.isEmpty()) { > break; > } > builder.append(line); > if
[jira] [Commented] (SSHD-744) An SSH client receives an echo as a response from the server
[ https://issues.apache.org/jira/browse/SSHD-744?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=15997023#comment-15997023 ] Goldstein Lyor commented on SSHD-744: - There are too many variables here - basically the "server" is as you say {quote}custom protocol over SSH{quote} so it is anybody's guess as to how it behaves when your code opens a SHELL channel. It is beyond the scope of this project to deal with custom "protocols" - the correct forum is whoever wrote the custom server must provide answers as to the echo behavior. There are so many possible problems that I wouldn't know were to begin: - Is there some "prefix" command that must be sent before the XML document ? - Is the server using correctly the STDIN/STDOUT/STDERR streams ? - Is it sending standard channel control messages ? - Etc., etc., etc. > An SSH client receives an echo as a response from the server > > > Key: SSHD-744 > URL: https://issues.apache.org/jira/browse/SSHD-744 > Project: MINA SSHD > Issue Type: Question >Affects Versions: 1.4.0 >Reporter: Miron Aseev >Priority: Minor > > I'm trying to integrate my Java application with a device which provides a > custom protocol over SSH. > The whole point of this protocol is about exchanging XML documents back and > forth. It can be described with the following steps: > # a client connects to the server > # the client sends an XML document which contains the request data > # the client waits for the XML document which contains a response data from a > server > # the server receives the XML document that was sent by the client and sends > the response XML document back to the client > # the client receives the XML response from the server and closes the > connection > The recommended way to send requests to this device is to use `cat` and `ssh` > tools. Here's a simple example: > {code} > cat example.xml | ssh @ > {code} > where `example.xml` is a file that contains an XML document with the request > data. > Calling directly 'cat' and 'ssh' via Runtime.exec would be very problematic > because this approach has a lot of drawbacks. So, I decided to implement this > protocol using MINA SSHD library. > The problem I've run into is I receive my own request as a response from the > server. So there's some kind of echoing stuff going on, I think. > For example, when I send this XML document as a request to the server: > {code} > > > > test > > > {code} > I receive the following data as a response: > {code} > Last login: Wed May 3 07:33:43 2017 from ip > Header > > > > test > > > {code} > The funny this is when I send the same request via cat and ssh (the example I > mentioned above), I receive the correct response from the device. > Here's an excerpt from my application: > {code} > public class Test { > private static final long AWAIT_TIMEOUT = TimeUnit.SECONDS.toMillis(5); > private static final String XML_PAYLOAD_HEADER = " public void init() throws IOException, JAXBException { > try (SshClient client = initSshClient(); > ClientSession session = >initClientSession(client, "test", "localhost", 22, "pass")) { > sendRequest(session); > } > } > private static SshClient initSshClient() { > SshClient client = SshClient.setUpDefaultClient(); > client.start(); > return client; > } > private ClientSession initClientSession(SshClient client, String user, > String host, int port, > String password) throws IOException > { > ClientSession session = > client.connect(user, host, port).verify(AWAIT_TIMEOUT).getSession(); > session.addPasswordIdentity(password); > session.auth().verify(AWAIT_TIMEOUT); > return session; > } > private void sendRequest(ClientSession session) throws IOException { > try (PipedOutputStream out = new PipedOutputStream(); > PipedInputStream channelIn = new PipedInputStream(out); > PipedOutputStream channelOut = new PipedOutputStream(); > PipedInputStream in = new PipedInputStream(channelOut); > BufferedReader reader = new BufferedReader(new > InputStreamReader(in)); > BufferedWriter writer = new BufferedWriter(new > OutputStreamWriter(out)); > ClientChannel channel = initClientChannel(session, channelIn, > channelOut)) { > String xmlRequest = ""; // Here goes a real XML document; > writer.write(xmlRequest); > writer.newLine(); > writer.flush(); > String response = readFully(reader); > } > } > private static ClientChannel initClientChannel(ClientSession session, > InputStream input, > OutputStream output)