栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用java 远程登录 Linux

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用java 远程登录 Linux

最近需要使用java远程连接Linux,就了解到了linux的expect命令。
首先要在本地安装expect。

yum install -y expect

expect远程登录代码

#!/usr/bin/expect
spawn ssh root@//这里填你的主机//
expect {
  "password:" {send "//这里填你的主机密码//n"}
}
interact

spawn:交互程序开始,执行后面的命令或程序。需要进入到expect环境才可以执行,不能直接在shell环境下直接执行。
expect:从交互程序进程中指定接收信息, 如果匹配成功, 就执行send的指令交互;否则等待timeout秒后自动退出expect语句。例如当我们登录时,会让我们填写密码,其中交互的句子里面包括"password"这个字符串,它就能将value填写进去,注意要在尾部加入回车或者r
interact:执行完代码后保持交互状态,将控制权交给用户。没有该命令执行完后自动退出而不是留在远程终端上。

java代码实现:

    @Test
    public void shell() {
        ShellInfo shellInfo = new ShellInfo();
        shellInfo.setCmd("ls");
        shellInfo.setIp("你的主机");
        shellInfo.setName("root");
        Map map = new HashMap<>();
//        map.put("yes/no","yesn");
        map.put("password:","你的密码n");

        shellInfo.setExpectMap(map);
        List res = exec(shellInfo);

        for (String re : res) {
            System.out.println(re);
        }
    }


    
    public List exec(ShellInfo info) {
        String cmdStr = getCmd(info);
        String cmd[] = {"sh", "-c", cmdStr};
        List outputs = new ArrayList<>();
        BufferedReader reader = null;
        try {
            Process process = Runtime.getRuntime().exec(cmd);
            reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            while (Objects.nonNull(line = reader.readLine())) {
                outputs.add(line);

            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (Objects.nonNull(reader)) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return outputs;
    }

    
    private String getCmd(ShellInfo info) {
        if (StringUtils.isBlank(info.getCmd())) {
            return info.getCmd();
        }
        String cmd = "";
        if (info.isSsh()) {
            cmd = "ssh " + info.getName() + "@" + info.getIp() + " "" + info.getCmd() + """;
            cmd = "/usr/bin/expect <<-EOFn" +
                    "set timeout " + 5 + "n" +
                    "spawn " + cmd + "n" +
                    "expect {n";

            if (!CollectionUtils.isEmpty(info.getExpectMap())) {
                //添加except
                int size = info.getExpectMap().size();
                Set keys = info.getExpectMap().keySet();
                int i = 0;
                for (String key : keys) {
                    i++;
                    if (i != size) {
                        cmd += """ + key + "" { send "" + info.getExpectMap().get(key) + "\r"; exp_continue }n";
                    } else {
                        cmd += """ + key + "" { send "" + info.getExpectMap().get(key) + "\r" }n";
                    }
                }
                cmd += "n" +
                        "}n" +
                        "expect eofn" +
                        "EOFn";

            }
        return cmd;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1035070.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号