0

My desktop is windows. And I would like to connect a Linux server remotely. The middle target is to using script to ssh(using password, not key pairs) remote linux server from windows and run a command. I have putty installed in my machine. The final target here is that I could use java to collect output from this script(remote connect a server via ssh password and run a command) and then parse the output and extract the data I want and put it in excel sheet. The remote server needs login and password.

user84592
  • 4,750
  • 11
  • 55
  • 91

3 Answers3

3

Why don't you try JSch?

Francisco R
  • 4,032
  • 1
  • 22
  • 37
  • I downloaded jsch, see the example Exec.java. I simply could not connect remote server. Remote server use ssh password. But I got following error message: "com.jcraft.jsch.JSchException: reject HostKey:xxxxx". I do not know where to put my ssh password. (it is not login password). – user84592 Jun 28 '11 at 12:28
2

use jsch of jcraft

here is one example

http://www.jcraft.com/jsch/examples/JTAJSch.java

you can find it on SO as well

Jsch or SSHJ or Ganymed SSH-2?

Community
  • 1
  • 1
Nirmal- thInk beYond
  • 11,847
  • 8
  • 35
  • 46
  • I downloaded jsch, see the example Exec.java. I simply could not connect remote server. Remote server use ssh password. But I got following error message: "com.jcraft.jsch.JSchException: reject HostKey:xxxxx". I do not know where to put my ssh password. (it is not login password). – user84592 Jun 28 '11 at 12:26
  • see http://stackoverflow.com/questions/5097514/jsch-or-sshj-or-ganymed-ssh-2 for nikunj's answer its working fine here is line `Session session = jsch.getSession(username, hostname, port);` – Nirmal- thInk beYond Jun 28 '11 at 12:50
  • try{ JSch jsch=new JSch(); String host=null; if(arg.length>0){ host=arg[0]; } else{ host=JOptionPane.showInputDialog("Enter username@hostname", System.getProperty("user.name")+ "@localhost"); } String user=host.substring(0, host.indexOf('@')); host=host.substring(host.indexOf('@')+1); Session session=jsch.getSession(user, host,2030); – user84592 Jun 28 '11 at 13:09
  • port here is 2030 and it uses password as SSH password, not key pair. – user84592 Jun 28 '11 at 13:09
  • ssh port is 22, and just make sure that user and hosts are valid – Nirmal- thInk beYond Jun 28 '11 at 13:14
  • I change port to 22, then it says "Tha authenticity of hos 'XX.XXX.XXX.XX' can't be established. RSA key fingerprint is a2:18:44:43:15:0b:ca:6f:93:cc:af:16:04:3c:48:35, Are you sure you want to continue connecting?" – user84592 Jun 28 '11 at 13:23
  • have you tried to login with your credentials using putty? if not then check that – Nirmal- thInk beYond Jun 28 '11 at 13:25
  • Yes, putty does not work either. But in the work, we are only allowed to use remote server with port 2030. From windows to remote Linux, there are some firewall. – user84592 Jun 28 '11 at 13:47
  • then port forwarding must be enabled on remote machine, can u login with putty on 2030 port? – Nirmal- thInk beYond Jun 29 '11 at 03:50
  • @Nirmal-thInkbeYond let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/962/discussion-between-user84592-and-nirmal-think-beyond) – user84592 Jun 29 '11 at 12:37
  • Yes. you are right. I shall whether port forwarding is enable or not. – user84592 Jun 29 '11 at 12:38
0

you could run plink (from PuTTY's developers) for running batches and writing the output to files, then process that with Java. I'm not sure how simple linking Java to PuTTY would be.

Yoni H
  • 453
  • 2
  • 7