java远程操作linux系统(JSch)

记得先引入依赖:

<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.55</version>
        </dependency>

利用JSch工具远程操作linux,下面这个是shell类型的,还有exec和sftp的类型,后续再补充
具体代码如下:

public class SshUtil {
    private static final Logger logger = LoggerFactory.getLogger(SshUtil.class);
    private Vector<String> stdout;
    // 会话session
    Session session;
    //输入IP、端口、用户名和密码,连接远程服务器
    public SshUtil(final String ipAddress, final String username, final String password, int port) {
        try {
            JSch jsch = new JSch();
            session = jsch.getSession(username, ipAddress, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect(100000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int execute(final ArrayList<String> cmds) {
        int returnCode = 0;
        ChannelShell channel = null;
        PrintWriter printWriter = null;
        BufferedReader input = null;
        stdout = new Vector<String>();
        try {
            channel = (ChannelShell) session.openChannel("shell");
            channel.connect();
            input = new BufferedReader(new InputStreamReader(channel.getInputStream()));
            printWriter = new PrintWriter(channel.getOutputStream());
            for (int i = 0; i < cmds.size(); i++) {
                printWriter.println(cmds.get(i));
            }
            printWriter.println("exit");
            printWriter.flush();

            String line;
            while ((line = input.readLine()) != null) {
                stdout.add(line);
                System.out.println(line);
                logger.info("The remote command is: " + line);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }finally {
            printWriter.close();
            try {
                input.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            if (channel != null) {
                channel.disconnect();
            }
        }
        return returnCode;
    }


    // 断开连接
    public void close(){
        if (session != null) {
            session.disconnect();
        }
    }
    // 执行命令获取执行结果
    public String executeForResult(ArrayList<String> cmds) {
        execute(cmds);
        StringBuilder sb = new StringBuilder();
        for (String str : stdout) {
            sb.append(str);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        ArrayList<String> cmds = new ArrayList<>();
        SshUtil execute = new SshUtil("192.168.86.150","root","123456",22);
        // 执行命令
        cmds.add("su - dmdba");
        cmds.add("cd /dm8/dmhs/bin");
        cmds.add("./DmhsService start");
        cmds.add("./DmhsService stop");
        cmds.add("exit");
        String result = execute.executeForResult(cmds);
        System.out.println(result);
        execute.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

打孔猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值