分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

jsch通过SSH2执行linux命令

发布时间:2023-09-06 02:19责任编辑:蔡小小关键词:js
public class SSHUtils { ???private Channel channel; ???private Session session = null; ???private int timeout = 60000; ???public SSHUtils(final String ipAddress, final String username, final String password) throws Exception { ???????JSch jsch = new JSch(); ???????this.session = jsch.getSession(username, ipAddress, 22); ???????this.session.setPassword(password); ???????this.session.setConfig("StrictHostKeyChecking", "no"); ???????this.session.setTimeout(this.timeout); ???????this.session.connect(); ???????this.channel = this.session.openChannel("shell"); ???????this.channel.connect(1000); ???} ???public String runShell(String cmd, String charset) throws Exception { ???????String temp = null; ???????InputStream instream = null; ???????OutputStream outstream = null; ???????try { ???????????instream = this.channel.getInputStream(); ???????????outstream = this.channel.getOutputStream(); ???????????outstream.write(cmd.getBytes()); ???????????outstream.flush(); ???????????Thread.sleep(2000); ???????????if (instream.available() > 0) { ???????????????byte[] data = new byte[instream.available()]; ???????????????int nLen = instream.read(data); ???????????????if (nLen < 0) { ???????????????????throw new Exception("network error."); ???????????????} ???????????????temp = new String(data, 0, nLen, "UTF-8"); ???????????} ???????} ?finally { ???????????outstream.close(); ???????????instream.close(); ???????} ???????return temp; ???} ???public void close() { ???????this.channel.disconnect(); ???????this.session.disconnect(); ???} ???public static void main(final String[] args) throws Exception { ???????SSHUtils sshUtil = new SSHUtils("10.5.31.157", "root", "codyy#123456"); ???????String res = sshUtil.runShell("/usr/local/baseframe/server.sh start ConfigServer-1.0.0.jar\n", "utf-8"); ???????System.out.println(res); ???????sshUtil.close(); ???}}

jsch通过SSH2执行linux命令

原文地址:https://www.cnblogs.com/LOVE0612/p/9855601.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved