Advertisement
Guest User

Untitled

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