package com.platform.utils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session; import ch.ethz.ssh2.StreamGobbler; import ch.ethz.ssh2.*; public class GanymedSSH { public static Logger log = Logger.getLogger(GanymedSSH.class); Connection conn; public boolean status = true;// 锟角凤拷锟斤拷锟街达拷锟斤拷锟斤拷锟阶刺� public GanymedSSH() { // TODO Auto-generated constructor stub } public GanymedSSH(String host, String username, String password, int port) { // TODO Auto-generated constructor stub try { conn = getOpenedConnection(host, username, password, port); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Connection getOpenedConnection(String host, String username, String password, int port) throws IOException { conn = new Connection(host, port); conn.connect(); // make sure the connection is opened boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (isAuthenticated == false) throw new IOException("Authentication failed."); return conn; } public void execCmdNoWaitAcquiescent(String cmd) { String host=Constant.hostIp; String username=Constant.rootUser; String password=Constant.rootPasswd; int port=Constant.port; Session sess = null; try { conn = getOpenedConnection(host, username, password, port); sess = conn.openSession(); // 执锟斤拷cmd sess.execCommand(cmd); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { sess.close(); } } public void execCmdNoWait(String host, String username, String password, int port, String cmd) { Session sess = null; try { conn = getOpenedConnection(host, username, password, port); sess = conn.openSession(); // 执锟斤拷cmd sess.execCommand(cmd); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { sess.close(); } } public List execCmdWaitAcquiescent(String cmd) { String host=Constant.hostIp; String username=Constant.rootUser; String password=Constant.rootPasswd; int port=Constant.port; List reStrings = new ArrayList(); Session sess = null; try { sess = conn.openSession(); // 执锟斤拷cmd sess.execCommand(cmd); InputStream stdout = new StreamGobbler(sess.getStdout()); InputStream stderr = new StreamGobbler(sess.getStderr()); BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout)); BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr)); while (true) { String line = stdoutReader.readLine(); if (line != null) { // System.out.println(line); reStrings.add(line); } else { break; } } while (true) { String line = stderrReader.readLine(); if (line != null) { // System.out.println(line); reStrings.add(line); } else { break; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { sess.close(); } return reStrings; } public List execCmdWait(String host, String username, String password, int port, String cmd) { List reStrings = new ArrayList(); Session sess = null; try { sess = conn.openSession(); // 执锟斤拷cmd sess.execCommand(cmd); InputStream stdout = new StreamGobbler(sess.getStdout()); InputStream stderr = new StreamGobbler(sess.getStderr()); BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout)); BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr)); while (true) { String line = stdoutReader.readLine(); if (line != null) { // System.out.println(line); reStrings.add(line); } else { break; } } while (true) { String line = stderrReader.readLine(); if (line != null) { // System.out.println(line); reStrings.add(line); } else { break; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { sess.close(); } return reStrings; } public Map execMD5cmd(String host, String username, String password, int port, String cmd, String prefix) { Map md5 = new HashMap(); Session sess = null; try { sess = conn.openSession(); // 执锟斤拷cmd sess.execCommand(cmd); InputStream stdout = new StreamGobbler(sess.getStdout()); @SuppressWarnings("resource") BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); while (true) { String line = br.readLine(); if (line != null) { String[] lines = line.split(" "); String key = lines[1].replace(prefix, ""); String value = lines[0]; md5.put(key, value); // System.out.println(key+"\t"+value); } else { break; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { sess.close(); } return md5; } public String execGetSize(String cmd) { status = false; String str_size = "0"; Session sess = null; try { // 执锟斤拷cmd sess = conn.openSession(); sess.execCommand(cmd); InputStream stdout = new StreamGobbler(sess.getStdout()); @SuppressWarnings("resource") BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); while (true) { String line = br.readLine(); if (line != null) { // String[] lines=line.split(" "); // str_size=lines[0]; str_size = line; } else { break; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { sess.close(); } status = true; return str_size; } public static void main(String[] args) { PropertyConfigurator.configure("log4j.properties"); } }