Execution of SSH command from Java sends status code 255, but in the terminal if it works












-1















I am trying to develop a small application that allows me to send certain commands by SSH to a remote server. If I try it from the Linux terminal or from the Windows Command Prompt it works without problems, but when I do it from my Java application it always responds with a status code of 255.



I have disabled the firewall and I have changed the port where I have listening SSH on the server to 22, because I use another, but nothing works. It does not throw me exceptions or anything and if it connects without problems. Any ideas?



I have tried with the sshj and JSch libraries and with both I have the same problem.



ForwardAgent is turn off



sshj example



private void sshj() throws Exception {
SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier((s, i, publicKey) -> true);
ssh.connect("host", 22);
Session session = null;
try {
ssh.authPassword("username", "password");
session = ssh.startSession();
Session.Command cmd = session.exec("command");
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
cmd.join(5, TimeUnit.SECONDS);
System.out.println("Exit status: " + cmd.getExitStatus());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}

ssh.disconnect();
}
}


JSch example



private static void jsch() throws Exception {
JSch js = new JSch();
Session s = js.getSession("username", "host", 22);
s.setPassword("password");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();

Channel c = s.openChannel("exec");
ChannelExec ce = (ChannelExec) c;
ce.setCommand("command");
ce.connect();

BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

ce.disconnect();
s.disconnect();

System.out.println("Exit status: " + ce.getExitStatus());
}









share|improve this question

























  • Use ce.getErrStream() to see error messages.

    – Martin Prikryl
    Nov 21 '18 at 7:40













  • Though I guess you have a problem similar to this one: Certain Unix commands fail with “… not found”, when executed through Java using JSch.

    – Martin Prikryl
    Nov 21 '18 at 7:41











  • Possible duplicate of Certain Unix commands fail with "... not found", when executed through Java using JSch

    – Martin Prikryl
    Nov 28 '18 at 6:32
















-1















I am trying to develop a small application that allows me to send certain commands by SSH to a remote server. If I try it from the Linux terminal or from the Windows Command Prompt it works without problems, but when I do it from my Java application it always responds with a status code of 255.



I have disabled the firewall and I have changed the port where I have listening SSH on the server to 22, because I use another, but nothing works. It does not throw me exceptions or anything and if it connects without problems. Any ideas?



I have tried with the sshj and JSch libraries and with both I have the same problem.



ForwardAgent is turn off



sshj example



private void sshj() throws Exception {
SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier((s, i, publicKey) -> true);
ssh.connect("host", 22);
Session session = null;
try {
ssh.authPassword("username", "password");
session = ssh.startSession();
Session.Command cmd = session.exec("command");
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
cmd.join(5, TimeUnit.SECONDS);
System.out.println("Exit status: " + cmd.getExitStatus());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}

ssh.disconnect();
}
}


JSch example



private static void jsch() throws Exception {
JSch js = new JSch();
Session s = js.getSession("username", "host", 22);
s.setPassword("password");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();

Channel c = s.openChannel("exec");
ChannelExec ce = (ChannelExec) c;
ce.setCommand("command");
ce.connect();

BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

ce.disconnect();
s.disconnect();

System.out.println("Exit status: " + ce.getExitStatus());
}









share|improve this question

























  • Use ce.getErrStream() to see error messages.

    – Martin Prikryl
    Nov 21 '18 at 7:40













  • Though I guess you have a problem similar to this one: Certain Unix commands fail with “… not found”, when executed through Java using JSch.

    – Martin Prikryl
    Nov 21 '18 at 7:41











  • Possible duplicate of Certain Unix commands fail with "... not found", when executed through Java using JSch

    – Martin Prikryl
    Nov 28 '18 at 6:32














-1












-1








-1


1






I am trying to develop a small application that allows me to send certain commands by SSH to a remote server. If I try it from the Linux terminal or from the Windows Command Prompt it works without problems, but when I do it from my Java application it always responds with a status code of 255.



I have disabled the firewall and I have changed the port where I have listening SSH on the server to 22, because I use another, but nothing works. It does not throw me exceptions or anything and if it connects without problems. Any ideas?



I have tried with the sshj and JSch libraries and with both I have the same problem.



ForwardAgent is turn off



sshj example



private void sshj() throws Exception {
SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier((s, i, publicKey) -> true);
ssh.connect("host", 22);
Session session = null;
try {
ssh.authPassword("username", "password");
session = ssh.startSession();
Session.Command cmd = session.exec("command");
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
cmd.join(5, TimeUnit.SECONDS);
System.out.println("Exit status: " + cmd.getExitStatus());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}

ssh.disconnect();
}
}


JSch example



private static void jsch() throws Exception {
JSch js = new JSch();
Session s = js.getSession("username", "host", 22);
s.setPassword("password");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();

Channel c = s.openChannel("exec");
ChannelExec ce = (ChannelExec) c;
ce.setCommand("command");
ce.connect();

BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

ce.disconnect();
s.disconnect();

System.out.println("Exit status: " + ce.getExitStatus());
}









share|improve this question
















I am trying to develop a small application that allows me to send certain commands by SSH to a remote server. If I try it from the Linux terminal or from the Windows Command Prompt it works without problems, but when I do it from my Java application it always responds with a status code of 255.



I have disabled the firewall and I have changed the port where I have listening SSH on the server to 22, because I use another, but nothing works. It does not throw me exceptions or anything and if it connects without problems. Any ideas?



I have tried with the sshj and JSch libraries and with both I have the same problem.



ForwardAgent is turn off



sshj example



private void sshj() throws Exception {
SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier((s, i, publicKey) -> true);
ssh.connect("host", 22);
Session session = null;
try {
ssh.authPassword("username", "password");
session = ssh.startSession();
Session.Command cmd = session.exec("command");
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
cmd.join(5, TimeUnit.SECONDS);
System.out.println("Exit status: " + cmd.getExitStatus());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}

ssh.disconnect();
}
}


JSch example



private static void jsch() throws Exception {
JSch js = new JSch();
Session s = js.getSession("username", "host", 22);
s.setPassword("password");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();

Channel c = s.openChannel("exec");
ChannelExec ce = (ChannelExec) c;
ce.setCommand("command");
ce.connect();

BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

ce.disconnect();
s.disconnect();

System.out.println("Exit status: " + ce.getExitStatus());
}






java ssh jsch sshj






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 1:20







E. Betanzos

















asked Nov 21 '18 at 0:43









E. BetanzosE. Betanzos

1563




1563













  • Use ce.getErrStream() to see error messages.

    – Martin Prikryl
    Nov 21 '18 at 7:40













  • Though I guess you have a problem similar to this one: Certain Unix commands fail with “… not found”, when executed through Java using JSch.

    – Martin Prikryl
    Nov 21 '18 at 7:41











  • Possible duplicate of Certain Unix commands fail with "... not found", when executed through Java using JSch

    – Martin Prikryl
    Nov 28 '18 at 6:32



















  • Use ce.getErrStream() to see error messages.

    – Martin Prikryl
    Nov 21 '18 at 7:40













  • Though I guess you have a problem similar to this one: Certain Unix commands fail with “… not found”, when executed through Java using JSch.

    – Martin Prikryl
    Nov 21 '18 at 7:41











  • Possible duplicate of Certain Unix commands fail with "... not found", when executed through Java using JSch

    – Martin Prikryl
    Nov 28 '18 at 6:32

















Use ce.getErrStream() to see error messages.

– Martin Prikryl
Nov 21 '18 at 7:40







Use ce.getErrStream() to see error messages.

– Martin Prikryl
Nov 21 '18 at 7:40















Though I guess you have a problem similar to this one: Certain Unix commands fail with “… not found”, when executed through Java using JSch.

– Martin Prikryl
Nov 21 '18 at 7:41





Though I guess you have a problem similar to this one: Certain Unix commands fail with “… not found”, when executed through Java using JSch.

– Martin Prikryl
Nov 21 '18 at 7:41













Possible duplicate of Certain Unix commands fail with "... not found", when executed through Java using JSch

– Martin Prikryl
Nov 28 '18 at 6:32





Possible duplicate of Certain Unix commands fail with "... not found", when executed through Java using JSch

– Martin Prikryl
Nov 28 '18 at 6:32












1 Answer
1






active

oldest

votes


















0














change to code so that you get the inputStream before doing connect



InputStream in = ce.getInputStream();
ce.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));


Also, the getSession is wrong as it should be



public Session getSession(String username,
String host,
int port)


edit



The below code works for me



JSch js = new JSch();
Session s = js.getSession("username", "127.0.0.1", 22);
s.setPassword("password");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();

Channel c = s.openChannel("exec");
ChannelExec ce = (ChannelExec) c;
ce.setCommand("uptime");

InputStream in = ce.getInputStream();
ce.connect();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

ce.disconnect();
s.disconnect();

System.out.println("Exit status: " + ce.getExitStatus());


output



 10:26:08 up 149 days, 58 min,  3 users,  load average: 0.61, 0.68, 0.68
Exit status: 0





share|improve this answer


























  • No work, same behavior.

    – E. Betanzos
    Nov 21 '18 at 1:14











  • I have made more edits, also I am assuming that this is replaced with a real command ce.setCommand("command");

    – Scary Wombat
    Nov 21 '18 at 1:18













  • I have the getSession call good in my code, like you. That was an translattion error. Yes, I replace command with a real command, and work fine in terminal but in my code no.

    – E. Betanzos
    Nov 21 '18 at 1:23











  • see my latest edit

    – Scary Wombat
    Nov 21 '18 at 1:28











  • Hmm, Mr Betanzos has gone away?

    – Scary Wombat
    Nov 21 '18 at 1:48











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403744%2fexecution-of-ssh-command-from-java-sends-status-code-255-but-in-the-terminal-if%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














change to code so that you get the inputStream before doing connect



InputStream in = ce.getInputStream();
ce.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));


Also, the getSession is wrong as it should be



public Session getSession(String username,
String host,
int port)


edit



The below code works for me



JSch js = new JSch();
Session s = js.getSession("username", "127.0.0.1", 22);
s.setPassword("password");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();

Channel c = s.openChannel("exec");
ChannelExec ce = (ChannelExec) c;
ce.setCommand("uptime");

InputStream in = ce.getInputStream();
ce.connect();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

ce.disconnect();
s.disconnect();

System.out.println("Exit status: " + ce.getExitStatus());


output



 10:26:08 up 149 days, 58 min,  3 users,  load average: 0.61, 0.68, 0.68
Exit status: 0





share|improve this answer


























  • No work, same behavior.

    – E. Betanzos
    Nov 21 '18 at 1:14











  • I have made more edits, also I am assuming that this is replaced with a real command ce.setCommand("command");

    – Scary Wombat
    Nov 21 '18 at 1:18













  • I have the getSession call good in my code, like you. That was an translattion error. Yes, I replace command with a real command, and work fine in terminal but in my code no.

    – E. Betanzos
    Nov 21 '18 at 1:23











  • see my latest edit

    – Scary Wombat
    Nov 21 '18 at 1:28











  • Hmm, Mr Betanzos has gone away?

    – Scary Wombat
    Nov 21 '18 at 1:48
















0














change to code so that you get the inputStream before doing connect



InputStream in = ce.getInputStream();
ce.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));


Also, the getSession is wrong as it should be



public Session getSession(String username,
String host,
int port)


edit



The below code works for me



JSch js = new JSch();
Session s = js.getSession("username", "127.0.0.1", 22);
s.setPassword("password");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();

Channel c = s.openChannel("exec");
ChannelExec ce = (ChannelExec) c;
ce.setCommand("uptime");

InputStream in = ce.getInputStream();
ce.connect();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

ce.disconnect();
s.disconnect();

System.out.println("Exit status: " + ce.getExitStatus());


output



 10:26:08 up 149 days, 58 min,  3 users,  load average: 0.61, 0.68, 0.68
Exit status: 0





share|improve this answer


























  • No work, same behavior.

    – E. Betanzos
    Nov 21 '18 at 1:14











  • I have made more edits, also I am assuming that this is replaced with a real command ce.setCommand("command");

    – Scary Wombat
    Nov 21 '18 at 1:18













  • I have the getSession call good in my code, like you. That was an translattion error. Yes, I replace command with a real command, and work fine in terminal but in my code no.

    – E. Betanzos
    Nov 21 '18 at 1:23











  • see my latest edit

    – Scary Wombat
    Nov 21 '18 at 1:28











  • Hmm, Mr Betanzos has gone away?

    – Scary Wombat
    Nov 21 '18 at 1:48














0












0








0







change to code so that you get the inputStream before doing connect



InputStream in = ce.getInputStream();
ce.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));


Also, the getSession is wrong as it should be



public Session getSession(String username,
String host,
int port)


edit



The below code works for me



JSch js = new JSch();
Session s = js.getSession("username", "127.0.0.1", 22);
s.setPassword("password");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();

Channel c = s.openChannel("exec");
ChannelExec ce = (ChannelExec) c;
ce.setCommand("uptime");

InputStream in = ce.getInputStream();
ce.connect();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

ce.disconnect();
s.disconnect();

System.out.println("Exit status: " + ce.getExitStatus());


output



 10:26:08 up 149 days, 58 min,  3 users,  load average: 0.61, 0.68, 0.68
Exit status: 0





share|improve this answer















change to code so that you get the inputStream before doing connect



InputStream in = ce.getInputStream();
ce.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));


Also, the getSession is wrong as it should be



public Session getSession(String username,
String host,
int port)


edit



The below code works for me



JSch js = new JSch();
Session s = js.getSession("username", "127.0.0.1", 22);
s.setPassword("password");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();

Channel c = s.openChannel("exec");
ChannelExec ce = (ChannelExec) c;
ce.setCommand("uptime");

InputStream in = ce.getInputStream();
ce.connect();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

ce.disconnect();
s.disconnect();

System.out.println("Exit status: " + ce.getExitStatus());


output



 10:26:08 up 149 days, 58 min,  3 users,  load average: 0.61, 0.68, 0.68
Exit status: 0






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 1:28

























answered Nov 21 '18 at 0:55









Scary WombatScary Wombat

35.2k32252




35.2k32252













  • No work, same behavior.

    – E. Betanzos
    Nov 21 '18 at 1:14











  • I have made more edits, also I am assuming that this is replaced with a real command ce.setCommand("command");

    – Scary Wombat
    Nov 21 '18 at 1:18













  • I have the getSession call good in my code, like you. That was an translattion error. Yes, I replace command with a real command, and work fine in terminal but in my code no.

    – E. Betanzos
    Nov 21 '18 at 1:23











  • see my latest edit

    – Scary Wombat
    Nov 21 '18 at 1:28











  • Hmm, Mr Betanzos has gone away?

    – Scary Wombat
    Nov 21 '18 at 1:48



















  • No work, same behavior.

    – E. Betanzos
    Nov 21 '18 at 1:14











  • I have made more edits, also I am assuming that this is replaced with a real command ce.setCommand("command");

    – Scary Wombat
    Nov 21 '18 at 1:18













  • I have the getSession call good in my code, like you. That was an translattion error. Yes, I replace command with a real command, and work fine in terminal but in my code no.

    – E. Betanzos
    Nov 21 '18 at 1:23











  • see my latest edit

    – Scary Wombat
    Nov 21 '18 at 1:28











  • Hmm, Mr Betanzos has gone away?

    – Scary Wombat
    Nov 21 '18 at 1:48

















No work, same behavior.

– E. Betanzos
Nov 21 '18 at 1:14





No work, same behavior.

– E. Betanzos
Nov 21 '18 at 1:14













I have made more edits, also I am assuming that this is replaced with a real command ce.setCommand("command");

– Scary Wombat
Nov 21 '18 at 1:18







I have made more edits, also I am assuming that this is replaced with a real command ce.setCommand("command");

– Scary Wombat
Nov 21 '18 at 1:18















I have the getSession call good in my code, like you. That was an translattion error. Yes, I replace command with a real command, and work fine in terminal but in my code no.

– E. Betanzos
Nov 21 '18 at 1:23





I have the getSession call good in my code, like you. That was an translattion error. Yes, I replace command with a real command, and work fine in terminal but in my code no.

– E. Betanzos
Nov 21 '18 at 1:23













see my latest edit

– Scary Wombat
Nov 21 '18 at 1:28





see my latest edit

– Scary Wombat
Nov 21 '18 at 1:28













Hmm, Mr Betanzos has gone away?

– Scary Wombat
Nov 21 '18 at 1:48





Hmm, Mr Betanzos has gone away?

– Scary Wombat
Nov 21 '18 at 1:48


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403744%2fexecution-of-ssh-command-from-java-sends-status-code-255-but-in-the-terminal-if%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$