50 likes | 171 Vues
This guide provides a straightforward example of transferring files over SSH using SCP and SFTP with the JSch library in Java. You will learn how to set up the JSch library, manage SSH keys for secure connections, and implement file transfer functionality with separate threading for efficiency. We will also discuss the importance of user authentication and demonstrate how to handle the session and channels appropriately. This tutorial is aimed at developers looking to incorporate secure file transfer capabilities within their applications.
E N D
File transfer over ssh via scp and sftp A two second dirty example
Jsch • Limited documentation, but seems pretty good • Get jar file from http://www.jcraft.com/jsch/ • Examples: http://www.jcraft.com/jsch/examples/ • Documentionhttp://show.docjava.com/book/cgij/jdoc/net/ssh/jsch/JSch.html (or search for jsch doc) • To use, add the jar file to the set of external libraries in eclipse • Jsch requires a couple of classes to be implemented • UserInfo
UserInfo • UserInfo • User info contains user names and passwords • The online examples show how to make a dialog box appear to get the user name and password. However, these examples don’t work on android. • By hardcoding, we can skip using UserInfo
HostKeyRepository • Ssh stores the keys of machines it has previously communicated with. This avoids man-in-the-middle attacks • Jsch uses the class HostKeyRepository to decide how to store and retrieve keys • A hardcoded example is as follows • Add • MyHostKeyRepositorymyHostKeyRepository = new MyHostKeyRepository(); • class MyHostKeyRepository implements HostKeyRepository{}; • Let eclipse make required functions • Go to getHostKeyand add • byte[] key = new byte[16]; • String[] keyStr = {"ae","71","c6","56","fa","38","76","65","3c","ad","e8","8d","a1","71","00","23"}; • for(inti=0; i<keyStr.length; i++) • key[i] = Byte.parseByte(keyStr[i], 16); • HostKey[] hk = new HostKey[1]; • try { • hk[0] = new HostKey("128.4.35.59",key); • } catch (JSchException e) { • e.printStackTrace(); • } • return hk; • However, you must set the keyStr to be the key of the machine you will communicate with. • Sometimes this key is given in various config information. • You can get the key by running the program with the above, incorrect key. • The android log will show you an error and show the correct key. • Get that key string and put it into the code above
File transfer with sftp • class MyRunnable implements Runnable { • int _arg; // this can be some argument, but we skip using arguments in this simple exmaple • MyRunnable(intarg) { • _arg = arg; • } • @Override • public void run() { • moveFile(arg); • } • } • public void moveFile() { // since the file transfer will take some time, we must put the file transfer in a separate thread. • MyRunnablemyrunnable = new MyRunnable(0); • new Thread(myrunnable).start(); • } • public void moveFileWithSFTP { • String user = “user"; // user name • String host = "128.4.1.45"; // machine name could be a string that is resolved with dns • String sfile= “filename.txt"; // file to move • String dfile = “filename.txt”; // name of destination file • JSchjsch=new JSch(); • jsch.setHostKeyRepository(myHostKeyRepository); • Session session; • session = jsch.getSession(user, host, 22); • session.setPassword(“password"); // hard coded password.. Use UserInfo to improve • session.connect(); • if(!session.isConnected()) { • Log.e("sch",“connect failed"); • session.disconnect(); • return; • } • Log.e("sch","connected"); • channel=session.openChannel("sftp"); • channel.connect(); • ChannelSftp c=(ChannelSftp)channel; • c.put(sfile,dfile,ChannelSftp.OVERWRITE); • if (((SftpATTRS)c.stat("LocationTracker.txt")).getSize()==(new File(lfile)).length()) { • Log.e("sfpt","success"); • } else { • Log.e("sfpt","file transfer faield"); • } • Log.e("sftp","done"); // this line is run only after the transfer finishes • channel.disconnect(); • session.disconnect(); • }