Advertisement
Guest User

Untitled

a guest
Feb 21st, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. public class SshConnectionManager {
  2.  
  3. private static Session session;
  4. private static ChannelShell channel;
  5. private static String username = "";
  6. private static String password = "";
  7. private static String hostname = "";
  8.  
  9.  
  10. private static Session getSession(){
  11. if(session == null || !session.isConnected()){
  12. session = connect(hostname,username,password);
  13. }
  14. return session;
  15. }
  16.  
  17. private static Channel getChannel(){
  18. if(channel == null || !channel.isConnected()){
  19. try{
  20. channel = (ChannelShell)getSession().openChannel("shell");
  21. channel.setPty(false);
  22. channel.connect();
  23.  
  24. }catch(Exception e){
  25. System.out.println("Error while opening channel: "+ e);
  26. }
  27. }
  28. return channel;
  29. }
  30.  
  31. private static Session connect(String hostname, String username, String password){
  32.  
  33. JSch jSch = new JSch();
  34.  
  35. try {
  36.  
  37. session = jSch.getSession(username, hostname, 22);
  38. Properties config = new Properties();
  39. config.put("StrictHostKeyChecking", "no");
  40. session.setConfig(config);
  41. session.setPassword(password);
  42.  
  43. System.out.println("Connecting SSH to " + hostname + " - Please wait for few seconds... ");
  44. session.connect();
  45. System.out.println("Connected!");
  46. }catch(Exception e){
  47. System.out.println("An error occurred while connecting to "+hostname+": "+e);
  48. }
  49.  
  50. return session;
  51.  
  52. }
  53.  
  54. private static void executeCommands(List<String> commands){
  55.  
  56. try{
  57. Channel channel=getChannel();
  58.  
  59. System.out.println("Sending commands...");
  60. sendCommands(channel, commands);
  61.  
  62. readChannelOutput(channel);
  63. System.out.println("Finished sending commands!");
  64.  
  65. }catch(Exception e){
  66. System.out.println("An error ocurred during executeCommands: "+e);
  67. }
  68. }
  69.  
  70. private static void sendCommands(Channel channel, List<String> commands){
  71.  
  72. try{
  73. PrintStream out = new PrintStream(channel.getOutputStream());
  74.  
  75. out.println("#!/bin/bash");
  76. for(String command : commands){
  77. out.println(command);
  78. }
  79. out.println("exit");
  80.  
  81. out.flush();
  82. }catch(Exception e){
  83. System.out.println("Error while sending commands: "+ e);
  84. }
  85.  
  86. }
  87.  
  88. private static void readChannelOutput(Channel channel){
  89.  
  90. byte[] buffer = new byte[1024];
  91.  
  92. try{
  93. InputStream in = channel.getInputStream();
  94. String line = "";
  95. while (true){
  96. while (in.available() > 0) {
  97. int i = in.read(buffer, 0, 1024);
  98. if (i < 0) {
  99. break;
  100. }
  101. line = new String(buffer, 0, i);
  102. System.out.println(line);
  103. }
  104.  
  105. if(line.contains("logout")){
  106. break;
  107. }
  108.  
  109. if (channel.isClosed()){
  110. break;
  111. }
  112. try {
  113. Thread.sleep(1000);
  114. } catch (Exception ee){}
  115. }
  116. }catch(Exception e){
  117. System.out.println("Error while reading channel output: "+ e);
  118. }
  119.  
  120. }
  121.  
  122. public static void close(){
  123. channel.disconnect();
  124. session.disconnect();
  125. System.out.println("Disconnected channel and session");
  126. }
  127.  
  128.  
  129. public static void main(String[] args){
  130. List<String> commands = new ArrayList<String>();
  131. commands.add("ls -l");
  132.  
  133. executeCommands(commands);
  134. close();
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement