1

First of all my Linux knowledge is limited! I'm more a developer, who casually manages some servers :-)

So, I'm operating on a farm of RHEL5/6 (mixed) servers and here is what I want to do: I have a Java program on one master server, which runs commands on some remote machines. The Java program sends my commands to the OS correctly - local commands can be executed without problems. My problem is the execution of commands on the remote servers:

  • I've got the SSH keys, which allows my master to simply obtain root privileges on the slaves
  • My command works, if I trigger it directly via a terminal (e.g. ssh root@192.168.1.1 "cd /opt/bla")
  • I've tried different approaches, with different results - none of them correct :)

Attempt #1

$ ssh root@192.168.1.1
Pseudo-terminal will not be allocated because stdin is not a terminal.
Host key verification failed.

So, being familiar with Google, I've read about tty which was supposed to solve the problem but only lead to:

Attempt #2

$ ssh -t -t root@192.168.1.1
tcgetattr: Invalid argument
The authenticity of host '192.168.1.1 (192.168.1.1)' can't be established.
RSA key fingerprint is <somehex>.

So my application got stuck at this point, weirdly after Ctrl+C'ing it I've seen the following text:

Are you sure you want to continue connecting (yes/no)? Killed by signal 2.

So I'm assuming that something was working, it just got stuck on the input. However, I was unable to find anything conclusive about this error. One hint somewhere was to edit /etc/sudoers and comment out (I know it's not recommend)

# Defaults    requiretty

which didn't do anything, although I have no idea if I should change this setting on the master and/or the slave and if a reboot or some restart of a daemon is required.

My question now is: does this lead anywhere or are my attempts worthless? Or is there any better way to run remote commands on slaves triggered by a program on the master machine? Please keep in mind I won't recompile my kernel or do any complicated shenanigan, since I'm simply lacking the experience with Linux..

Help is highly appreciated!

Peter Ilfrich
  • 111
  • 1
  • 5
  • 2
    Check http://superuser.com/questions/421074/ssh-the-authenticity-of-host-host-cant-be-established - you could run the ssh-command manually once to add the host to the known-hosts file, later your app will get thru. – ott-- Nov 20 '12 at 18:11
  • Unfortunately doesn't work like that. I did try my command many times manually before wrapping it in some Java automation (using the same target slave). – Peter Ilfrich Nov 20 '12 at 19:16
  • If security really doesn't matter, you could try rsh instead of ssh. – ott-- Nov 20 '12 at 20:43

2 Answers2

2

I've got the SSH keys

Are these keys setup under the root account?

Host key verification failed.

The authenticity of host '192.168.1.1 (192.168.1.1)' can't be established. RSA key fingerprint is .

Your program is not writing out to the known_hosts file. The host you are trying to connect to hasn't been used from the account that the java process is using. You are going to need to generate a valid known_hosts file before you use that. Or perhaps if you are a somewhat trusting person you could set the StrictHostKeyChecking no option.

If it has been used under that account, then something is messed up about the environment. You might want to strongly consider setting up a configuration file for the ssh client and defining the path for the keys, and known_host file. Have your java program reference the configuration file by using ssh -f configfile.

Zoredache
  • 130,897
  • 41
  • 276
  • 420
  • Yes, the keys are setup under the root account. Weirdly I have already connected to the slave before as root (I was executing my stuff manually before) and my Java program is running as root as well. The problem only came up, when I was wrapping the SSH call in a Java scheduler. I'm going to try the StrictHostKeyChecking option, which sounds promising (especially since security plays no role in my scenario). – Peter Ilfrich Nov 20 '12 at 19:09
0

Solved my problem by using the Java library Jsch, which is a pure Java implementation of SSH2. I figured, that my SSH settings in my environment are messed up as well, so this seemed to me like the more effective alternative.

Took me 1-2 hours to get it working - very easy to implement.

Thanks anyway for the suggestions. I especially had to use the StrictHostKeyChecking=no option (which is available in the Java library as well) to be able to connect.

Pros and Cons:

  • (-) The documentation of Jsch is pretty bad
  • (-) There are some assumptions being made, which you need to figure out when you first use it.
  • (+) However, there are many questions and answers out there in the interweb, because the library is used by quite a lot of people.
  • (+) There are some very well illustrated examples out there, which demonstrate how different functions are working - this doesn't completely replace a good documentation though.
Peter Ilfrich
  • 111
  • 1
  • 5