Linux – Remote SSH command via Java program

linuxremote-accesssshterminaltty

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!

Best Answer

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.

Related Topic