Quantcast
Channel: Active questions tagged servlets - Stack Overflow
Viewing all articles
Browse latest Browse all 778

Call remote linux Command over SSH, Java client vs WebServlet

$
0
0

I'm trying to call remote Linux Command over SSH connection by using maverick-synergy-client 3.1.1 on:

Rocky Linux 9.JDK 17.0.3

Firstly, I tried run it as Java Client Application:

public class ShhClient {  public static void main(String[] args) {  Integer response = null;    try{        SshClient ssh = SshClient.SshClientBuilder.create().withHostname("10.0.1.90").withPort(22).withUsername("xxxxxx").withPassword("yyyyyyy").withConnectTimeout(Duration.ofMillis(40000)).build();        response = ssh.executeCommandWithResult("~/bin/test.sh");    } catch (IOException | SshException e) {        e.printStackTrace();    }finally {        System.out.println(response);    }  }  public static Integer callSmsClientOnRemoteLinux(){    Integer response = null;    try{        SshClient ssh = SshClient.SshClientBuilder.create().withHostname("10.0.1.90").withPort(22).withUsername("xxxxxx").withPassword("yyyyyyy").withConnectTimeout(Duration.ofMillis(40000)).build();        response = ssh.executeCommandWithResult("~/bin/test.sh");    } catch (IOException | SshException e) {        e.printStackTrace();    }finally {        return response;    }}}

I got response code "0", that means test.sh was executed and exited properly on remote host.

but when I put that code into my RESTful web services as:

@WebServlet("/smsForwardAccepter")public class TeltonikaSMSForwardEndPoint extends HttpServlet implements Serializable {    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {      logger.info("hit by post request.");      String inString = getBody(req);      logger.info("Income request context: "+inString);      Integer responseForRemoteSSH = ShhClient.callSmsClientOnRemoteLinux();      logger.info("SSH client call to remote Linux: "+responseForRemoteSSH);      JSONObject jo = new JSONObject();      jo.put("result", "touched");      try {          sendAsJson(resp, jo);      } catch (IOException e) {          throw new RuntimeException(e);     }  }  }

And when I hit that WebServlet over http by curl POST command. Webservlet was hit and get the request parameter properly.

But unfortunately, failed to call my remote test.sh, return code is "null". and don't produce any response to my curl call as well.

[2024-09-09T09:52:22.751+1000] [Payara 6.2024.6] [INFO] [] [com.longz.thss.web.thss.sms.TeltonikaSMSForwardEndPoint] [tid: _ThreadID=89 _ThreadName=http-thread-pool::http-listener-1(1)] [timeMillis: 1725839542751] [levelValue: 800] [[  SSH client call to remote Linux: null]]

My question is: Why call remote command over ssh by Java client and put it into WebServlet will have different result? or I did anything wrong?

Please advise!


Viewing all articles
Browse latest Browse all 778

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>