Report this

What is the reason for this report?

SSH tunnel using java client.

Posted on October 16, 2016

I am using this java client - https://github.com/jeevatkm/digitalocean-api-java and I have id_rsa and id_rsa.pub keys with me.

I have tried to create ssh tunnel connection using www.jcraft.com/jsch/ but it’s giving-

com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: connect

Could some one please help me to identify, how we can do it in java -

I tried this code to do it in java -

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class UserAuthPubKey {
    public static void main(String[] arg) {
        try {
            JSch jsch = new JSch();

            String user = "root";
            String host = "**.**.**.**";
            int port = 5555;
            String privateKey = "C:\\Users\\Test\\AppData\\Roaming\\Skype\\My Skype Received Files\\id_rsa";

            jsch.addIdentity(privateKey);
            //jsch.
            System.out.println("identity added ");

            Session session = jsch.getSession(user, host, port);
            System.out.println("session created.");

            // disabling StrictHostKeyChecking may help to make connection but makes it insecure
            // see http://stackoverflow.com/questions/30178936/jsch-sftp-security-with-session-setconfigstricthostkeychecking-no
            // 
            // java.util.Properties config = new java.util.Properties();
            // config.put("StrictHostKeyChecking", "no");
            // session.setConfig(config);

            session.connect();
            System.out.println("session connected.....");

            Channel channel = session.openChannel("sftp");
            channel.setInputStream(System.in);
            channel.setOutputStream(System.out);
            channel.connect();
            System.out.println("shell channel connected....");

            /*ChannelSftp c = (ChannelSftp) channel;

            String fileName = "test.txt";
            c.put(fileName, "./in/");
            c.exit();
            System.out.println("done");*/

        } catch (Exception e) {
            System.err.println(e);
        }
    }
}



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

The JSch library definitely makes this easy and supports port forwarding:


JSch jsch = new JSch();
Session session = jsch.getSession(user, host);
session.setPassword(password);
session.connect(timeout);
session.setPortForwardingL(listenPort, destHost, destPort);

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.