java通過Linux獲取命令信息並顯示出來
阿新 • • 發佈:2018-08-06
用戶 com username rac blog RoCE host end jsch 代碼如下:
package test; import java.io.*; import java.nio.charset.Charset; import java.util.Properties; import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import org.apache.commons.io.IOUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; /** * @author lw * @createTime 2018/8/3 15:49 * @description ssh登陸主機 輸入密碼登陸並執行命令 */ public class LinuxShh { private static Logger logger = LogManager.getLogger(LinuxShh.class); final static String userName = "test";// 用戶名 final static String password = "87654321";// 密碼 final static String host = "174.31.514.222";// 服務器地址 final static int port = 22;// 端口號 final static int timeout = 60000000; /** * <dependency> * <groupId>com.jcraft</groupId> * <artifactId>jsch</artifactId> * <version>0.1.54</version> * </dependency> */ public static void main(String[] args) { String cmd = "uname -a && date && uptime && who && vmstat 1 60 "; /*try { String rs = LinuxShh.exeCommand(host,port,userName,password,cmd); System.out.println(rs); } catch (JSchException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }*/ try { LinuxShh.getLog(host,port,userName,password,cmd); } catch (JSchException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** *@Author:lw *@Description: 實時獲取命令日誌 * @param :host,port user,password cmmmand *@Date:14:00 2018/8/6 */ public static void getLog(String host, int port, String user, String password, String command)throws JSchException, IOException{ JSch jsch = new JSch(); // 創建JSch對象 //String cmd = "vmstat 1 1";// 要運行的命 String cmd = "uname -a && date && uptime && vmstat 1 60 "; //>/home/linux_shell/m/vmstat.txt Session session = jsch.getSession(user, host, port); // 根據用戶名,主機ip,端口獲取一個Session對象 session.setPassword(password); // 設置密碼 Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); // 為Session對象設置properties session.setTimeout(timeout); // 設置timeout時間 session.connect(); // 通過Session建立鏈接 ChannelExec channelExec = (ChannelExec) session.openChannel("exec"); channelExec.setCommand(command); channelExec.setInputStream(null); channelExec.setErrStream(System.err); channelExec.connect(); InputStream in = channelExec.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8"))); //寫入相應的文件 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\\2.txt"),"UTF-8")); String buf = null; StringBuffer sb = new StringBuffer(); System.out.println("您的IP是:" + host); logger.info("您的IP是:" + host); System.out.println("以下是:系統資源信息:"); while ((buf = reader.readLine()) != null) { sb.append(buf); //寫入相關文件 out.write(buf); out.newLine(); System.out.println(buf);// 打印控制臺輸出 } //清楚緩存 out.flush(); //關閉流 reader.close(); out.close(); channelExec.disconnect(); if (null != session) { session.disconnect(); } } /** *@Author:lw *@Description: 去除全部結果後,才顯示處理,不是實時獲取數據 *@Date:13:53 2018/8/6 */ public static String exeCommand(String host, int port, String user, String password, String command) throws JSchException, IOException { JSch jsch = new JSch(); Session session = jsch.getSession(user, host, port); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword(password); session.connect(); session.setTimeout(timeout); // 設置timeout時間 ChannelExec channelExec = (ChannelExec) session.openChannel("exec"); InputStream in = channelExec.getInputStream(); channelExec.setCommand(command); channelExec.setErrStream(System.err); channelExec.connect(); String out = IOUtils.toString(in, "UTF-8"); channelExec.disconnect(); session.disconnect(); return out; } }
結果:
java通過Linux獲取命令信息並顯示出來