標籤:緩衝 sch 通過 username script try desc uptime main
代碼如下:
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擷取命令資訊並顯示出來