SSH Connection Java - java

Is it possible to make a ssh connection to a server with java?

Yes, I used http://sourceforge.net/projects/sshtools/ in a Java application to connect to a UNIX server over SSH, it worked quite well.

jsch and sshJ are both good clients. I'd personally use sshJ as the code is documented much more thoroughly.
jsch has widespread use, including in eclipse and apache ant. I've also had issues with jsch and AES encrypted private keys, which required re-encrypting in 3DES, but that could just be me.

Yes, it is possible. You can try the following code:
package mypackage;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.*;
public class SSHReadFile
{
public static void main(String args[])
{
String user = "user";
String password = "password";
String host = "yourhostname";
int port=22;
String remoteFile="/home/john/test.txt";
try
{
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
session.connect();
System.out.println("Connection established.");
System.out.println("Crating SFTP Channel.");
ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();
System.out.println("SFTP Channel created.");
}
catch(Exception e){System.err.print(e);}
}
}

To make connection to Java servers, you need an implementation of SSHD (ssh client is not enough). You can try Apache SSHD,
http://mina.apache.org/sshd/
Because sshd is already running on most systems, an easier alternative is to connect to the server through a SSH tunnel.

Related

Algorithm negotiation fail error while connecting to solaris server through jsch

I am trying to connect to remote sftp server over ssh with JSch (0.1.52) but during session.connect(); I am getting Algorithm negotiation fail exception:
code snippet which I tried given below:
log.info("trying to connect to ssh");
JSch.setLogger(new MyLogger());
JSch jsch = new JSch();
String host = "abcd";
int port =22;
String success;
Session session = jsch.getSession(username,host, port);
log.info("Recieved the username>>>>>>>>>>>>>"+ username);
session.setPassword(password);
log.info("Recieved the password>>>>>>>>>>>>>"+ password);
Properties config = new Properties();
config.put("kex", "curve25519-sha256,curve25519-sha256#libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256");
config.put("CheckKexes", "diffie-hellman-group-exchange-sha256");//diffie-hellman-group-exchange-sha256
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
ConfigRepository configRepository =
com.jcraft.jsch.OpenSSHConfig.parseFile("/abc/config_file.txt");
jsch.setConfigRepository(configRepository);
log.info("Establishing connection to host...");
log.info("Session.................."+session);
session.connect();
Channel channel=session.openChannel("sftp");
channel.connect();
I tried updating the jsch latest jar,jdk 1.8 in java & corrected the checkkexes & ciphers in server and client but getting the same Algorithm negotiation fail. Please provide your suggestions to resolve this error, Thank you

Can't connect to corporate machine with SSH JCraft JSch

I've got a problem with connecting to corporate machine with SSH using JCraft JSch.
public static void main(String[] args) throws JSchException {
JSch jSch = new JSch();
String username = "username";
String host = "host";
int port = 22;
jSch.addIdentity("Path\\to\\key\\file");
jSch.setKnownHosts("known_hosts");
/* In known_hosts file, I've got Host IP, and public key,
the same that's on server
*/
Session session = jSch.getSession(username,host,port);
Properties config = new Properties();
config.put("StrictHostKeyChecking","yes");
session.setConfig(config);
session.connect(5000);
}
That's my code, after running it, there is an error: HostKey has been changed: [host adress], if i change StrictHostKeyChecking to no, another error appears: Auth fail.
I can connect to that machine via PuTTy or WinSCP, and I dont need to use tunneling. Also the key format is right(I made that mistake allready). When I connect to the machine via PuTTy, I need to pick the right machine(there are 5), and each of them has their own key pair, and the main connection to the "menu", has also it's own key pair, it's CAPI key.
Does anyone had something like that before? Also I made a forwarding port, and tried to connect with that, but I'm not sure how it works. Can anyone tell me how to do the port forwarding, or maybe somebody has another idea.
Thanks in advice

JSCH librairy with signed publickkey

I use a public key signed by my client to connect to SFTP. In command line all functions properly.
On the other hand via JSCH Librairy, I have a message error: "Auth fail".
My code is working correctly on another server or the public key is not signed
the jsch version used is the : 0.1.55
my connection setup looks like this
JSch jsch = new JSch();
jsch.addIdentity(sftpSetting.getPrivateKey());
session = jsch.getSession(sftpSetting.getUser(), sftpSetting.getServer(), sftpSetting.getPort());
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("PreferredAuthentications", "publickey");
session.connect(sftpSetting.getConnectionTimeout());
// Initializing a channel
Channel channel = session.openChannel("sftp");
channel.connect(sftpSetting.getConnectionTimeout());
channelSftp = (ChannelSftp) channel;

How to provide sftp password through Java

I am using JSCH API to invoke shell commands from java. I am trying to invoke sftp command like this :
Channel channel = (ChannelShell)getSession().openChannel("shell");
channel.connect();
PrintStream out = new PrintStream(channel.getOutputStream());
out.println("#!/bin/bash");
out.println("sftp akumar#sindh");
out.flush();
On Java console i see that it is connecting to this sindh server and then it asks for password.
Connecting to sindh...
akuamr#sindh's password:
How do i provide password to it. I tried
out.println("sftp akumar#sindh");
out.println("password123")
But this dosen't work out. Thanks in advance.
You need to set the password in the Session before connecting:
JSch jsch=new JSch();
Session session = jsch.getSession("akumar", "sindh");
session.setPort(22);
session.setPassword("password123");
session.connect();
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
And then you can use the channel to execute sftp commands.

Using JSCH Java [duplicate]

This question already has answers here:
Skipping Kerberos authentication prompts with JSch [duplicate]
(2 answers)
Closed 7 years ago.
I am trying to use JsCH for connecting to a remote server and then manipulate PostgreSQL.
Everything going fine if I use PUTTY and also with JSCH.
My only problem is the JsCH only accepts username and password from the Eclipse console even if I set these properties.
public void connect() {
//
int assigned_port;
final int local_port=5432;
// Remote host and port
final int remote_port=5432;
final String remote_host="myserver011";
try {
JSch jsch = new JSch();
// Create SSH session. Port 22 is your SSH port which
// is open in your firewall setup.
Session session = jsch.getSession("companyusername", remote_host, 22);
session.setPassword("password");
// Additional SSH options. See your ssh_config manual for
// more options. Set options according to your requirements.
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
config.put("Compression", "yes");
config.put("ConnectionAttempts","2");
session.setConfig(config);
// Connect
session.connect();
// Create the tunnel through port forwarding.
// This is basically instructing jsch session to send
// data received from local_port in the local machine to
// remote_port of the remote_host
// assigned_port is the port assigned by jsch for use,
// it may not always be the same as
// local_port.
assigned_port = session.setPortForwardingL(local_port,
remote_host, remote_port);
System.out.println("SSH Connection succes!");
} catch (JSchException e) {
e.printStackTrace();
LOGGER.log(Level.SEVERE, e.getMessage()); return;
}
if (assigned_port == 0) {
LOGGER.log(Level.SEVERE, "Port forwarding failed !");
return;
}
}
But the console still needs the username and password.
Kerberos username [myusername]: companyusername <---- here I set again the username
Kerberos password for companyusername : password <---- and the password
SSH Connection succes!
Opened database successfully
And then everything works. But I don't want to set these again, and I cant find anywhere the source of this "bug"?! I don't know, maybe it's a server config parameter to force the user to manually give the input?
Any advice or hint would be great.
//this works pretty well for me.
Session session;
ssh = new JSch();
session = ssh.getSession(username,hostname,port);
session.setConfig(config);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking","no");
session.connect();

Categories

Resources