Guest User

Untitled

a guest
Oct 18th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. public static void connectServcer(){
  2. String host="xxx.xxx.xxx.xxx";
  3. String user="id";
  4. String password="pw";
  5. String[] command = {
  6. "sh"
  7. , "/usr/local/bin/runShell.sh"
  8. };
  9.  
  10. System.out.println("cmd run");
  11. try{
  12.  
  13. java.util.Properties config = new java.util.Properties();
  14. config.put("StrictHostKeyChecking", "no");
  15. JSch jsch = new JSch();
  16. Session session=jsch.getSession(user, host, 22);
  17. session.setPassword(password);
  18. session.setConfig(config);
  19. session.connect();
  20. System.out.println("Connected");
  21.  
  22. Runtime r = Runtime.getRuntime();
  23.  
  24. com.jcraft.jsch.Channel channel=session.openChannel("exec");
  25. Process process = r.exec(command);
  26. process.getInputStream();
  27. channel.setInputStream(null);
  28. ((ChannelExec)channel).setErrStream(System.err);
  29.  
  30. InputStream in=channel.getInputStream();
  31. channel.connect();
  32. byte[] tmp=new byte[1024];
  33. while(true){
  34. while(in.available()>0){
  35. int i=in.read(tmp, 0, 1024);
  36. if(i<0)break;
  37. System.out.print(new String(tmp, 0, i));
  38. }
  39. if(channel.isClosed()){
  40. System.out.println("exit-status: "+channel.getExitStatus());
  41. break;
  42. }
  43. try{Thread.sleep(1000);}catch(Exception ee){}
  44. }
  45. channel.disconnect();
  46. session.disconnect();
  47. System.out.println("DONE");
  48. }catch(Exception e){
  49. e.printStackTrace();
  50. }
  51. }
  52.  
  53. cmd run
Add Comment
Please, Sign In to add comment