Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.InputStream;
  4.  
  5. import com.jcraft.jsch.ChannelExec;
  6. import com.jcraft.jsch.JSch;
  7. import com.jcraft.jsch.Session;
  8. import com.jcraft.jsch.UserInfo;
  9.  
  10. public class Question
  11. {
  12. public static void main(String[] args)
  13. {
  14. JSch jsch = new JSch();
  15. Session session = null;
  16.  
  17. try
  18. {
  19. String user = "custpeak";
  20. String host = "172.23.54.253";
  21. String password = "paas@tju";
  22. int port = 22;
  23. session = jsch.getSession(user, host, port);
  24.  
  25. UserInfo userInfo = new MyUserInfo(null);
  26. session.setUserInfo(userInfo);
  27. session.setPassword(password);
  28. session.connect();
  29.  
  30. ChannelExec channel = (ChannelExec) session.openChannel("exec");
  31. String command = "cd /home && sh pressure.sh" + "n";
  32. channel.setCommand(command);
  33. channel.setInputStream(null);
  34. InputStream instream = channel.getErrStream();
  35. byte[] tmp = new byte[1024];
  36. channel.connect();
  37.  
  38. String errOut = "";
  39. int exitStatus = -1;
  40. while (true)
  41. {
  42. while (instream.available() > 0)
  43. {
  44. int i = instream.read(tmp, 0, 1024);
  45. if (i < 0)
  46. {
  47. break;
  48. }
  49.  
  50. String tempOut = new String(tmp, 0, i, "UTF-8");
  51. errOut += tempOut;
  52. }
  53.  
  54. if (channel.isClosed())
  55. {
  56. if (instream.available() > 0)
  57. {
  58. continue;
  59. }
  60.  
  61. exitStatus = channel.getExitStatus();
  62. break;
  63. }
  64. }
  65. System.out.println("exitStatus:" + exitStatus);
  66. System.out.println("errMsg:" + errOut);
  67. }
  68. catch (Exception e)
  69. {
  70. e.printStackTrace();
  71. }
  72. }
  73.  
  74. if(!session.isConnected())
  75. {
  76. break;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement