Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH Connection Java

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

like image 911
Benni Avatar asked Jul 21 '10 19:07

Benni


People also ask

What is SSH connection command?

The ssh command provides a secure encrypted connection between two hosts over an insecure network. This connection can also be used for terminal access, file transfers, and for tunneling other applications. Graphical X11 applications can also be run securely over SSH from a remote location.

How do I connect PuTTY through Java code and pass command to terminal?

To make the putty session interactive we need to add -t option as well as in the File given as input to -m option we need to add "/bin/bash" as the last command. The content of the D:\test. txt file is as below. So the complete program will be something like below.

How do I connect to a SSH port?

Type the host name or IP address of the SSH server into the “Host name (or IP address)” box. Ensure the port number in the “Port” box matches the port number the SSH server requires. SSH servers use port 22 by default, but servers are often configured to use other port numbers instead. Click “Open” to connect.


3 Answers

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

like image 71
theomodsim Avatar answered Oct 23 '22 10:10

theomodsim


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.

like image 3
ClutchDude Avatar answered Oct 23 '22 10:10

ClutchDude


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);}
    }
    }
like image 2
Ripon Al Wasim Avatar answered Oct 23 '22 10:10

Ripon Al Wasim