3 windows环境与shell交互操作

 /**
     * 由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;
    }

}
时间: 2024-08-07 15:32:38

3 windows环境与shell交互操作的相关文章

windows环境下的eclipse操作虚拟机里面的hadoop相关配置

当电脑的配置不是很高的时候,在虚拟机里面安装上编译软件进行编程的话,卡的要命,所以总结一下在windows环境下eclipse配置链接虚拟机中的hadoop 在虚拟机中的hadoop要和主机的hadoop要是一样的版本(不知道不一样的版本会不会出现问题,确保成功,用一样的版本是最好的,我这里用的是hadoop2.7.0) 将虚拟机里面的环境配置好以后(虚拟机环境的配置,可见https://www.cnblogs.com/zhaochunhui/p/11451520.html),在window中进

Windows下如何实现Ruby操作MongoDB(环境安装配置)

最近在研究使用非关系型数据库,当前使用的是文档型数据库MongoDB.涉及到在Windows环境下使用Ruby操作MongoDB数据库.因此需在Windows环境下安装Ruby开发环境. 按照如下步骤进行操作: 1. 首先安装ruby 到http://rubyinstaller.org/downloads/下载相关软件:下载对应电脑所需的RubyInstaller版本.作者选中的是Ruby2.3.3(x64),url对应界面如下图所示: 成功下载安装包后,直接点击安装,可勾选所有选项,安装时必须

Windows环境下Ruby离线安装gem包

在上一篇博文中,我记录了如何在Windows环境下进行Ruby操作MongoDB数据库的环境配置.其中在最后一步讲述了安装MongoDB的驱动包.使用的是gem在线安装方式.本文章的目的是为了在目标机器或环境无法连接互联网时,如何使用gem进行gem工具包的离线安装. 1.在https://rubygems.org/,根据对应需安装的gem包名字设置相对应的url链接,如我需要安装bson, 则在浏览器输入https://rubygems.org/gems/bson,界面如下图所示: 继续下拉

第十一章、认识与学习 BASH Bash Shell 的操作环境

Bash Shell 的操作环境: 配置值分为系统整体配置值与各人喜好配置值, 仅是一些文件放置的地点不同! 路径与命令搜寻顺序 一个命令 (例如 ls) 被下达时, 到底是哪一个 ls 被拿来运行? 以相对/绝对路径运行命令,例如『 /bin/ls 』或『 ./ls 』: 由 alias 找到该命令来运行: 由 bash 内建的 (builtin) 命令来运行: 透过 $PATH 这个变量的顺序搜寻到的第一个命令来运行. 可以发现ls有颜色但是/bin/ls则没有颜色. 因为 /bin/ls

Python 3 利用 subprocess 实现管道( pipe )交互操作读/写通信

这里我们用Windows下的shell来举例: from subprocess import * #因为是举例,就全部导入了 为了方便你理解,我们用一个很简单的一段代码来说明: 可以看见我们利用Popen实例化了一个p,创建了子程序cmd.exe,然后我们给他的的Stdin(标准输入流)Stdout(标准输出流); 同时使用了subprocess.PIPE 作为参数,这个是一个特殊值,用于表明这些通道要开放.(在Python3.5,加入了run()方法来进行更好的操作) 然后我们继续 这些信息是

在windows环境下基于sublime text3的node.js开发环境搭建

首先安装sublime text3,百度一堆,自己找吧.理论上sublime text2应该也可以.我只能说一句:这个软件实在是太强悍了. 跨平台,丰富的插件体系,加上插件基本上就是一个强悍的ide了.目前我在使用的主要是Emmet.Python.还有一些格式化的插件(xml,json),加上这次安装的node.js. node.js的安装就不用多说了,直接http://nodejs.org/ 点击install下载window版本的安装程序后安装即可.默认的安装会将安装目录加到path环境变量

在linux与windows环境下配置JDK

一.准备     下载新版JDK:http://www.oracle.com/technetwork/java/javase/downloads/index.html 所有版本请戳:http://www.oracle.com/technetwork/java/archive-139210.html 二.安装与配置     先说说在linux环境下.为了方便,以下操作均以root用户执行 jdk有3种形式的包,分别是rpm,rpm.bin,tar.gz,下面分别说     1. 这里我下载jdk-

Hive Shell常用操作

1.本文命令的两种模式: 交互模式,即hive的shell环境:hive > …. 非交互模式:普通的Linux命令模式:%..... 2.Hive Shell常用操作 1) hive -e:从命令行执行指定的HQL,不需要分号: % hive -e 'select * from dummy' > a.txt 2) hive –f: 执行HQL脚本 % hive -f /home/my/hive-script.sql 3) hive -i:在进入交互模式之前,执行初始化sql文件 % hive

hadoop集群配置和在windows系统上运用java操作hdfs

安装 配置 概念 hadoop常用shell命令 使用java操作hadoop 本文介绍hadoop集群配置和在windows系统上运用java操作hdfs 安装 http://mirror.bit.edu.cn/apache/hadoop/common/hadoop-3.1.1/ sudo apt-get install ssh sudo apt-get install rsync mkdir /usr/local/hadoop tar -zxvf hadoop-3.1.1.tar.gz -C