Presentation is loading. Please wait.

Presentation is loading. Please wait.

File transfer over ssh via scp and sftp A two second dirty example.

Similar presentations


Presentation on theme: "File transfer over ssh via scp and sftp A two second dirty example."— Presentation transcript:

1 File transfer over ssh via scp and sftp A two second dirty example

2 Jsch Limited documentation, but seems pretty good – Get jar file from http://www.jcraft.com/jsch/http://www.jcraft.com/jsch/ – Examples: http://www.jcraft.com/jsch/examples/http://www.jcraft.com/jsch/examples/ – Documention http://show.docjava.com/book/cgij/jdoc/net/ssh/jsch /JSch.html (or search for jsch doc) http://show.docjava.com/book/cgij/jdoc/net/ssh/jsch /JSch.html To use, add the jar file to the set of external libraries in eclipse Jsch requires a couple of classes to be implemented – UserInfo

3 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 dont work on android. – By hardcoding, we can skip using UserInfo

4 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 – MyHostKeyRepository myHostKeyRepository = new MyHostKeyRepository(); – class MyHostKeyRepository implements HostKeyRepository {}; – Let eclipse make required functions – Go to getHostKey and 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(int i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/3/1398858/slides/slide_4.jpg", "name": "HostKeyRepository Ssh stores the keys of machines it has previously communicated with.", "description": "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 – MyHostKeyRepository myHostKeyRepository = new MyHostKeyRepository(); – class MyHostKeyRepository implements HostKeyRepository {}; – Let eclipse make required functions – Go to getHostKey and 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(int i=0; i

5 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(int arg) { _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. – MyRunnable myrunnable = 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 – JSch jsch=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(); }


Download ppt "File transfer over ssh via scp and sftp A two second dirty example."

Similar presentations


Ads by Google