Advertisement
Guest User

Untitled

a guest
Nov 13th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. import com.jcraft.jsch.*;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataInputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9.  
  10. public class SSHClient {
  11. public static void main(String[] args) throws JSchException, IOException {
  12. String endLineStr = " # "; // it is dependant to the server
  13. String host = ""; // host IP
  14. String user = ""; // username for SSH connection
  15. String password = ""; // password for SSH connection
  16. int port = 22; // default SSH port
  17.  
  18. JSch shell = new JSch();
  19. // get a new session
  20. Session session = shell.getSession(user, host, port);
  21.  
  22. // set user password and connect to a channel
  23. session.setUserInfo(new SSHUserInfo(password));
  24. session.connect();
  25. Channel channel = session.openChannel("shell");
  26. channel.connect();
  27.  
  28. ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
  29.  
  30. InputStream in = channelExec.getInputStream();
  31.  
  32. channelExec.setCommand("ls");
  33. channelExec.connect();
  34.  
  35. BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  36. String line;
  37. int index = 0;
  38.  
  39. while ((line = reader.readLine()) != null)
  40. {
  41. System.out.println(++index + " : " + line);
  42. }
  43.  
  44. int exitStatus = channelExec.getExitStatus();
  45. channelExec.disconnect();
  46. session.disconnect();
  47. if(exitStatus < 0){
  48. System.out.println("Done, but exit status not set!");
  49. }
  50. else if(exitStatus > 0){
  51. System.out.println("Done, but with error!");
  52. }
  53. else{
  54. System.out.println("Done!");
  55. }
  56.  
  57. // DataInputStream dataIn = new DataInputStream(channel.getInputStream());
  58. // DataOutputStream dataOut = new DataOutputStream(channel.getOutputStream());
  59. //
  60. // // send ls command to the server
  61. // dataOut.writeBytes("ls");
  62. // dataOut.flush();
  63. //
  64. // // and print the response
  65. // String line = dataIn.readLine();
  66. // System.out.println(line);
  67. // while(!line.endsWith(endLineStr)) {
  68. // System.out.println(line);
  69. // line = dataIn.readLine();
  70. // }
  71. // dataIn.close();
  72. // dataOut.close();
  73. channel.disconnect();
  74. session.disconnect();
  75. }
  76.  
  77. // this class implements jsch UserInfo interface for passing password to the session
  78. static class SSHUserInfo implements UserInfo {
  79. private String password;
  80.  
  81. SSHUserInfo(String password) {
  82. this.password = password;
  83. }
  84.  
  85. public String getPassphrase() {
  86. return null;
  87. }
  88.  
  89. public String getPassword() {
  90. return password;
  91. }
  92.  
  93. public boolean promptPassword(String arg0) {
  94. return true;
  95. }
  96.  
  97. public boolean promptPassphrase(String arg0) {
  98. return true;
  99. }
  100.  
  101. public boolean promptYesNo(String arg0) {
  102. return true;
  103. }
  104.  
  105. public void showMessage(String arg0) {
  106. System.out.println(arg0);
  107. }
  108. }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement