Advertisement
Guest User

Untitled

a guest
Nov 16th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import com.jcraft.jsch.ChannelSftp;
  2. import com.jcraft.jsch.JSch;
  3. import com.jcraft.jsch.Session;
  4. import java.io.*;
  5.  
  6. /**
  7. *
  8. * @author World
  9. */
  10. public class SSHReadFile
  11. {
  12. public static void main(String args[])
  13. {
  14. String user = "john";
  15. String password = "mypassword";
  16. String host = "192.168.100.23";
  17. int port=22;
  18.  
  19. String remoteFile="/home/john/test.txt";
  20.  
  21. try
  22. {
  23. JSch jsch = new JSch();
  24. Session session = jsch.getSession(user, host, port);
  25. session.setPassword(password);
  26. session.setConfig("StrictHostKeyChecking", "no");
  27. System.out.println("Establishing Connection...");
  28. session.connect();
  29. System.out.println("Connection established.");
  30. System.out.println("Crating SFTP Channel.");
  31. ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
  32. sftpChannel.connect();
  33. System.out.println("SFTP Channel created.");
  34.  
  35.  
  36. InputStream out= null;
  37. out= sftpChannel.get(remoteFile);
  38. BufferedReader br = new BufferedReader(new InputStreamReader(out));
  39. String line;
  40. while ((line = br.readLine()) != null)
  41. System.out.println(line);
  42. br.close();
  43. }
  44. catch(Exception e){System.err.print(e);}
  45. }
  46. }
  47.  
  48. Establishing Connection...
  49. Connection established.
  50. Crating SFTP Channel.
  51. SFTP Channel created.
  52. This is content from file /home/john/test.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement