2

I am trying to reboot remote server from my java code. I have password-less ssh setup in place and sudoers file has argument in place so that no password is required for executing the system commands. I run the following command from "bash shell" on my local machine and my remote system shutsdown and comes up just fine

ssh -t user1@192.168.2.125 'sudo reboot'

Above command restarts the remote server without checking for any passwords; and now I pass the above command thru the java code as follows (I surrounded 'sudo reboot' in special characters so that it goes as one command)

String cmd="ssh -t user1@192.168.2.125 \'sudo reboot\'";
log.info("Executing command.."+cmd);
p = Runtime.getRuntime().exec(cmd);
//open error stream and input stream and process

However I get the following error

[2014-10-30 19:07:55,593] INFO Executing command..ssh -t user1@192.168.2.125 'sudo reboot'  
[2014-10-30 19:07:57,070] ERROR ERROR while executing command: ssh -t user1@192.168.2.125 'sudo reboot'  
[2014-10-30 19:07:57,070] ERROR Pseudo-terminal will not be allocated because stdin is not a terminal.
bash: sudo reboot: command not found

What am I doing wrong? I am trying to avoid 3rd party libraries(ex:JSch). I am using centos6 and Java 7

SOLUTION

1)took out single quotes as suggested by @hcs. Then I get another error mesg about ".. tty.."

2)updated sudoers file as suggested in the link https://serverfault.com/questions/15667/remotely-reboot-tomcat

Community
  • 1
  • 1
kashili kashili
  • 955
  • 4
  • 15
  • 31
  • 1
    Try removing the single quotes from around `'sudo reboot'`, I think that is trying to run a command with a space in the middle rather than running `sudo` with `reboot` as an argument. – hcs Oct 30 '14 at 19:03
  • Some commands cannot be done from Java. That's all it is. It may be sudo, which requires security. or reboot, which should be fine. Java cannot handle everything that the CLI console will. E.g. using carriage return to change text of the output. Native apps can, but Java has always had this issue. There was a few Console classes made and one bu Sun, but it's still lacking. – ldmtwo Oct 30 '14 at 19:14
  • @hcs. thanks that solved it. Post it as answer and I will accept it – kashili kashili Oct 30 '14 at 19:28

2 Answers2

3

I sometimes had more luck using the exec() method that takes a String-array as parameters, instead of a single String. The String-array allows one to better specify what the different arguments are, which is in my experience more stable, especially if one has spaces within arguments as you have. So try the following:

String[] cmd = new String[]{"ssh", "-t", "user1@192.168.2.125", "sudo reboot"};
Runtime.getRuntime.exec(cmd);
cello
  • 5,356
  • 3
  • 23
  • 28
0

Remove the single quotes from around 'sudo reboot', that is trying to run a command with a space in the middle rather than running sudo with reboot as an argument.

What confuses me is why it was working originally when you ran it directly from the command line...

hcs
  • 1,514
  • 9
  • 14
  • I think(?) there are some bugs (or misinterpretation of how the command strings are passed from java memory space to ssh). Here is one such discussion http://stackoverflow.com/questions/2146727/runtime-getruntime-exec – kashili kashili Oct 30 '14 at 19:37