3 windows環境與shell互動操作

來源:互聯網
上載者:User

標籤: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互動操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.