Using SSH or SFTP resources from a Java app
by ricardoz 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(); |
October 23rd, 2009 on 1:14 pm
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?
October 24th, 2009 on 10:30 am
@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.
February 17th, 2010 on 12:48 am
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.
February 22nd, 2010 on 2:49 am
which all lib need to b imported in order to run this program?
February 22nd, 2010 on 10:14 am
You need the SSH Tools JAR and its dependencies, you can find them both in the SSH Tools homepage.
February 23rd, 2010 on 1:59 am
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
}
}
February 23rd, 2010 on 9:09 am
@Rahul: what error are you getting?
February 25th, 2010 on 6:29 am
Hey M sorry for late reply..I did this.. Actually i missed one lib file..after adding that its working..Thanks..
February 28th, 2010 on 10:58 pm
Hey guys; anyone knows how to display the excel file content in Jsp page..
March 1st, 2010 on 8:36 am
@Rahul: check out Apache POI
March 2nd, 2010 on 11:35 pm
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.
March 9th, 2010 on 4:38 am
Does anyone know how to send SMS using java..??
April 24th, 2010 on 11:52 pm
Hey Rahul,
Which lib did you initially miss? Please find some time to respond.
Cheers,
Nirmal.