Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3.  
  4. import org.apache.commons.io.IOUtils;
  5.  
  6. import com.jcraft.jsch.Channel;
  7. import com.jcraft.jsch.ChannelExec;
  8. import com.jcraft.jsch.JSch;
  9. import com.jcraft.jsch.JSchException;
  10. import com.jcraft.jsch.Session;
  11.  
  12. public class Conn {
  13.  
  14. public static void main(String[] args) throws IOException {
  15.  
  16.  
  17. try {
  18. final String host = "hostname";
  19. final String username = "username";
  20. final String password = "password";
  21. final int port = 22;
  22. final String hostKey = "no";
  23.  
  24.  
  25. final String command5 ="sh metrics.sh";
  26.  
  27.  
  28. final String openSession1 = openSession1(host, username,password,port, hostKey, command5);
  29.  
  30.  
  31.  
  32. }catch (Exception ase) {
  33. System.out.println("Caught an Exception:");
  34. System.out.println("Error Message: " + ase.getMessage());
  35. }
  36.  
  37.  
  38. }
  39.  
  40. public static final String HOST_KEY_CHECKING = "StrictHostKeyChecking";
  41. public static final String EXCUTE_CHANNEL = "exec";
  42.  
  43. public static String openSession1(String host, String username,String password, int port, String hostKey, String command5) {
  44.  
  45. JSch jsch = new JSch();
  46. Session session = null;
  47. Channel channel = null;
  48. StringBuilder log = new StringBuilder();
  49.  
  50. String response = null;
  51.  
  52.  
  53. try {
  54. session = jsch.getSession(username, host, port);
  55. session.setPassword(password);
  56. session.setConfig(HOST_KEY_CHECKING, hostKey);
  57. session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
  58. session.connect();
  59. // check if connect was successful
  60. if (session.isConnected()) {
  61. System.out.println("Connected sucessfully to server :" + host);
  62.  
  63. try{
  64. channel = session.openChannel(EXCUTE_CHANNEL);
  65.  
  66. ((ChannelExec) channel).setCommand(command5);
  67. // channel.setInputStream(System.in);
  68. channel.setInputStream(null);
  69.  
  70. ((ChannelExec) channel).setErrStream(System.err);
  71.  
  72. InputStream in = channel.getInputStream();
  73.  
  74. channel.connect();
  75. response = IOUtils.toString(in);
  76.  
  77. }
  78. catch(Exception e){
  79. return e.toString();
  80. }
  81. } else {
  82. System.out.println("Connection Failed" + host);
  83. }
  84. } catch (JSchException e) {
  85. System.out.println("Connection Failed" + host + " Error:" + e.getMessage());
  86. e.printStackTrace();
  87. } catch (Exception e) {
  88. System.out.println("Connection Failed" + host + " Error:" + e.getMessage());
  89. e.printStackTrace();
  90. }
  91.  
  92. System.out.println( "Sent:"+ response.substring((response.indexOf("Sent: ") + ("Sent: ").length()),
  93. (response.indexOf("Connections: "))));
  94.  
  95. System.out.println( "Received:"+ response.substring((response.indexOf("Received: ") + ("Received: ").length()),
  96. (response.indexOf("Sent: "))));
  97.  
  98. System.out.println( "Connections:"+ response.substring((response.indexOf("Connections: ") + ("Connections: ").length()),
  99. response.indexOf("Outstanding: ")));
  100.  
  101.  
  102.  
  103. System.out.println( "Response received :"+ response);
  104. return response;
  105.  
  106. }
  107.  
  108. #!/bin/bash
  109. echo stat | nc hostname 2181
  110. echo bytes_in_per_second
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement