標籤:10個 bsp ring eth 伺服器 shell false ann 計算
/** * 由SshConfig配置擷取一個Session * @param conf * @return */ public static Session createSession(SshConfig conf){ LOG.info("try connect to " + conf.getHostIp() + ",username: " + conf.getUsername() + ",password: " + conf.getPassword() + ",port: " + conf.getPort()); JSch jSch=new JSch(); //建立JSch對象 Session session= null;//根據使用者名稱,主機ip和連接埠擷取一個Session對象 try { session = jSch.getSession(conf.getUsername(), conf.getHostIp(), conf.getPort()); session.setPassword(conf.getPassword()); //設定密碼 Properties config=new Properties(); //StrictHostKeyChecking no //"StrictHostKeyChecking"如果設為"yes",ssh將不會自動把電腦的密匙加入"$HOME/.ssh/known_hosts"檔案,且一旦電腦的密匙發生了變化,就拒絕串連 config.put("StrictHostKeyChecking", "no"); session.setConfig(config);//為Session對象設定properties session.setTimeout(conf.getTimeOut());//設定逾時 session.connect();//通過Session建立串連 LOG.info("connect to {} success",conf.getHostIp()); } catch (JSchException e) { LOG.error("connect to {} fail, connect infos:{}",conf.getHostIp(),conf.toString()); e.printStackTrace(); } return session; } /** * 關閉session * @param session */ public static void closeSession(Session session){ session.disconnect(); }
/** * 從src檔案拿到本地dst檔案 * @param session * @param src * @param dst * @return */ public static Boolean downLoad(Session session,String src,String dst){ Boolean success=true; //src linux伺服器檔案地址,dst 本地存放地址 ChannelSftp channelSftp= null; try { channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); channelSftp.get(src, dst); channelSftp.quit(); } catch (JSchException e) { success=false; e.printStackTrace(); LOG.error(e.getMessage()); } catch (SftpException e) {
success=false; e.printStackTrace(); LOG.error(e.getMessage()); } return success; }
/** * 由ip 使用者名稱 密碼 擷取該ip對應的hostname * @param hostIp * @param username * @param password * @return */ public static String getHostName(String hostIp, String username, String password){ Session session=createSession(new SshConfig(hostIp,username,password)); String hostname = exeCommand(session,"hostname"); return hostname.trim(); } /** * 執行command命令,把執行結果返回 * @param session * @param command * @return */ public static String exeCommand(Session session,String command){ ChannelExec channelExec = null; String out=null; try { channelExec = (ChannelExec) session.openChannel("exec"); InputStream in = channelExec.getInputStream(); channelExec.setCommand(command); channelExec.setErrStream(System.err); channelExec.connect(); out = IOUtils.toString(in, "UTF-8"); channelExec.disconnect(); //session.disconnect(); } catch (JSchException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if(null==out){ LOG.error("execute {} error",command); out=""; } return out; }
public class SshConfig { private String hostIp; private String username; private String password; //預設連接埠22 private int port=22; //10個小時逾時 private int timeOut=1000*60*60*10; public SshConfig(String hostIp, String username, String password) { this.hostIp = hostIp; this.password = password; this.username = username; } public String getHostIp() { return hostIp; } public void setHostIp(String hostIp) { this.hostIp = hostIp; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public int getTimeOut() { return timeOut; } public void setTimeOut(int timeOut) { this.timeOut = timeOut; } @Override public String toString() { String builder="\nhostIp"+" : " + hostIp +"\nusername"+" : " + username +"\npassword"+" : " + password +"\nport"+" : " + port +"\ntimeOut"+" : " + timeOut; return builder; }}
3 windows環境與shell互動操作