Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.InputStream;
- import org.apache.commons.io.IOUtils;
- import com.jcraft.jsch.Channel;
- import com.jcraft.jsch.ChannelExec;
- import com.jcraft.jsch.JSch;
- import com.jcraft.jsch.JSchException;
- import com.jcraft.jsch.Session;
- public class Conn {
- public static void main(String[] args) throws IOException {
- try {
- final String host = "hostname";
- final String username = "username";
- final String password = "password";
- final int port = 22;
- final String hostKey = "no";
- final String command5 ="sh metrics.sh";
- final String openSession1 = openSession1(host, username,password,port, hostKey, command5);
- }catch (Exception ase) {
- System.out.println("Caught an Exception:");
- System.out.println("Error Message: " + ase.getMessage());
- }
- }
- public static final String HOST_KEY_CHECKING = "StrictHostKeyChecking";
- public static final String EXCUTE_CHANNEL = "exec";
- public static String openSession1(String host, String username,String password, int port, String hostKey, String command5) {
- JSch jsch = new JSch();
- Session session = null;
- Channel channel = null;
- StringBuilder log = new StringBuilder();
- String response = null;
- try {
- session = jsch.getSession(username, host, port);
- session.setPassword(password);
- session.setConfig(HOST_KEY_CHECKING, hostKey);
- session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
- session.connect();
- // check if connect was successful
- if (session.isConnected()) {
- System.out.println("Connected sucessfully to server :" + host);
- try{
- channel = session.openChannel(EXCUTE_CHANNEL);
- ((ChannelExec) channel).setCommand(command5);
- // channel.setInputStream(System.in);
- channel.setInputStream(null);
- ((ChannelExec) channel).setErrStream(System.err);
- InputStream in = channel.getInputStream();
- channel.connect();
- response = IOUtils.toString(in);
- }
- catch(Exception e){
- return e.toString();
- }
- } else {
- System.out.println("Connection Failed" + host);
- }
- } catch (JSchException e) {
- System.out.println("Connection Failed" + host + " Error:" + e.getMessage());
- e.printStackTrace();
- } catch (Exception e) {
- System.out.println("Connection Failed" + host + " Error:" + e.getMessage());
- e.printStackTrace();
- }
- System.out.println( "Sent:"+ response.substring((response.indexOf("Sent: ") + ("Sent: ").length()),
- (response.indexOf("Connections: "))));
- System.out.println( "Received:"+ response.substring((response.indexOf("Received: ") + ("Received: ").length()),
- (response.indexOf("Sent: "))));
- System.out.println( "Connections:"+ response.substring((response.indexOf("Connections: ") + ("Connections: ").length()),
- response.indexOf("Outstanding: ")));
- System.out.println( "Response received :"+ response);
- return response;
- }
- #!/bin/bash
- echo stat | nc hostname 2181
- echo bytes_in_per_second
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement