Guest User

Untitled

a guest
Dec 11th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. import com.jcraft.jsch.*;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.util.Scanner;
  6.  
  7. public class Javassh {
  8.  
  9. public static void main(String args[]) throws IOException {
  10. String user = "<YOUR LOGIN>";
  11. String password = "<YOUR PASSWORD>";
  12. String host = "<YOUR HOST>";
  13. int port = 22;
  14. String remoteFile = "/home/root/test.txt";
  15.  
  16. try {
  17. JSch jsch = new JSch();
  18. Session session = jsch.getSession(user, host, port);
  19. session.setPassword(password);
  20. session.setConfig("StrictHostKeyChecking", "no");
  21. System.out.println("Establishing Connection...");
  22. session.connect();
  23. System.out.println("Connection established.");
  24. System.out.println("Crating SFTP Channel.");
  25. ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
  26. int lport;
  27. String rhost = "localhost";
  28. int rport;
  29. sftpChannel.connect();
  30.  
  31. String command = "nc localhost 5000";
  32. Channel channel = session.openChannel("exec");
  33. ((ChannelExec) channel).setCommand(command);
  34. channel.setInputStream(null);
  35. ((ChannelExec) channel).setErrStream(System.err);
  36. InputStream in = channel.getInputStream();
  37.  
  38. channel.connect();
  39.  
  40. byte[] tmp = new byte[1024];
  41. while (true)
  42. {
  43. while (in.available() > 0)
  44. {
  45. int i = in.read(tmp, 0, 1024);
  46. if (i < 0)
  47. break;
  48. System.out.print(new String(tmp, 0, i));
  49. }
  50. if (channel.isClosed())
  51. {
  52. System.out.println("exit-status: " + channel.getExitStatus());
  53. break;
  54. }
  55. try
  56. {
  57. Thread.sleep(1000);
  58. }
  59. catch (Exception ee)
  60. {
  61. }
  62. }
  63.  
  64. channel.disconnect();
  65.  
  66. System.out.println("SFTP Channel created.");
  67.  
  68. InputStream inputStream = sftpChannel.get(remoteFile);
  69.  
  70. try (Scanner scanner = new Scanner(new InputStreamReader(inputStream))) {
  71. while (scanner.hasNextLine()) {
  72. String line = scanner.nextLine();
  73. System.out.println(line);
  74. }
  75. }
  76. } catch (JSchException | SftpException e) {
  77. e.printStackTrace();
  78. }
  79. }
  80.  
  81. }
Add Comment
Please, Sign In to add comment