Spartan Java

Using SSH or SFTP resources from a Java app

by on May.22, 2009, under Security

There are many commercial SSH client libraries for Java, but it was hard to find a good open source one. Finally I stumbled upon SSHTools and I have to say it works wonderfully and the API is clean and simple.

For example the following code snippet connects to a SFTP server and downlads a file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
SshClient ssh = new SshClient();
ssh.connect("someHost", 22);
 
// Authenticate
PasswordAuthenticationClient passwordAuthenticationClient = new PasswordAuthenticationClient();
passwordAuthenticationClient.setUsername("jhondoe");
passwordAuthenticationClient.setPassword("trustno1");
 
if (ssh.authenticate(passwordAuthenticationClient) != AuthenticationProtocolState.COMPLETE) {
   throw new IOException("Login failed");
}
 
//Open the SFTP client
SftpClient client = ssh.openSftpClient();
 
// We want to download the file to the local /tmp folder
client.lcd("/tmp");
 
// Download the file
client.get("/home/jhondoe/dummy.txt");
 
//disconnect
client.quit();
ssh.disconnect();
:, , , , , ,

13 Comments for this entry

  • The Professor

    When I try this:
    SshClient ssh = new SshClient();

    My java class just dies. No exceptions, no messages, no fuss it just dies. Using jre 1.4.2_18

    Any ideas on where to look for trouble?

  • ricardoz

    @The Professor:

    I would check that both the sshtools and the required JARs (packed with sshtools distribution) are in your classpath, and that an appropriate version of the JCE is available for you Java VM.

  • bigbird

    I can say from experience with working with a variety of SFTP servers that j2ssh is pretty buggy.

    A commercial option is edtFTPj/PRO:

    http://www.enterprisedt.com/products/edtftpjssl/overview.html

    It is well documented and well supported.

  • Rahul

    which all lib need to b imported in order to run this program?

  • ricardoz

    You need the SSH Tools JAR and its dependencies, you can find them both in the SSH Tools homepage.

  • Rahul

    I wrote the program accodingly what has given above but m getting error. m pasting the code..can someone tell where m going wrong.
    //*****************************//
    import com.sshtools.j2ssh.SftpClient;
    import com.sshtools.j2ssh.SshClient;
    import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
    import com.sshtools.j2ssh.sftp.SftpFile;

    public class downloadlog1
    {
    public String hostname=null;
    public String username=null;
    public String password=null;
    public String remotepath=null;
    public String localpath=null;
    //public String fileExtention=”log”;
    public String remoteFilename=null;
    public void con()
    {
    SshClient ssh=null;
    SftpClient sftp=null;

    try
    {
    hostname=”172.19.3.106″;
    username=”root”;
    password=”root123″;
    remotepath=” “;// here i mentioned the server path
    localpath=” “;// here i mentioned the path where i have to save file after downloading
    ssh=new SshClient();
    ssh.connect(hostname);
    PasswordAuthenticationClient auth=new PasswordAuthenticationClient();
    auth.setUsername(username);
    auth.setPassword(password);
    ssh.authenticate(auth);
    sftp=ssh.openSftpClient();
    sftp.lcd(localpath);
    sftp.get(” “);// here i mentioned the file with full path which i need to download
    sftp.quit();
    ssh.disconnect();
    }
    catch(Exception e)
    {
    System.out.println(“Exception while connecting to the remote server” + e);
    }
    }
    public static void main(String args[]) throws Exception
    {
    downloadlog1 obj=new downloadlog1();
    obj.con();// calling the function
    }
    }

  • ricardoz

    @Rahul: what error are you getting?

  • Rahul

    Hey M sorry for late reply..I did this.. Actually i missed one lib file..after adding that its working..Thanks..

  • Rahul

    Hey guys; anyone knows how to display the excel file content in Jsp page..

  • Rahul

    Hey I used POI to read excel file and again create after result..Now i want to display the row by row content of excel on web page..web page is created using JSP.which i m not able to do..so can u help in that.

  • Rahul

    Does anyone know how to send SMS using java..??

  • nemo

    Hey Rahul,

    Which lib did you initially miss? Please find some time to respond.

    Cheers,
    Nirmal.

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...