Advertisement
Guest User

Untitled

a guest
May 26th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. public static void main(String args[]){
  2. {
  3. String user = "user";
  4. String password = "password";
  5. String host = "hostName";
  6. int port=22;
  7.  
  8.  
  9. //String remoteFile="/home/john/test.txt";
  10. // String yes="yes";
  11.  
  12. try
  13. {
  14. JSch jsch = new JSch();
  15. Session session = jsch.getSession(user, host, port);
  16. session.setPassword(password);
  17. session.setConfig("StrictHostKeyChecking", "no");
  18. System.out.println("Establishing Connection...");
  19. session.connect();
  20. Channel channel=session.openChannel("exec");
  21.  
  22.  
  23. ((ChannelExec)channel).setCommand("ssh myServerName");//after this it'll ask for confirmation and password
  24. channel.connect();
  25.  
  26.  
  27.  
  28.  
  29. InputStream output= channel.getInputStream();
  30.  
  31. System.out.println("aafter stream");
  32. int readByte= output.read();
  33. StringBuilder outputBuffer=new StringBuilder();
  34. while(readByte != 0xffffffff)
  35. {
  36. // System.out.println("read byte"+readByte);
  37.  
  38. outputBuffer.append((char)readByte);
  39. readByte = output.read();
  40.  
  41. }
  42. System.out.println(outputBuffer.toString());
  43. channel.disconnect();
  44.  
  45.  
  46.  
  47. }
  48. catch(Exception e){System.err.print("error message"+ e);}
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement