码迷,mamicode.com
首页 > Windows程序 > 详细

3 windows环境与shell交互操作

时间:2016-11-18 11:40:55      阅读:207      评论:0      收藏:0      [点我收藏+]

标签: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交互操作

标签:10个   bsp   ring   eth   服务器   shell   false   ann   计算   

原文地址:http://www.cnblogs.com/yangh2016/p/6076968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!