前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JSch - Java Secure Channel : java 代码实现服务器远程操作

JSch - Java Secure Channel : java 代码实现服务器远程操作

作者头像
微风-- 轻许--
发布2022-04-13 15:28:27
1.8K0
发布2022-04-13 15:28:27
举报
文章被收录于专栏:java 微风

一、前言

JSch是SSH2的纯Java实现 。

JSch允许您连接到sshd服务器并使用端口转发,X11转发,文件传输等,您可以将其功能集成到您自己的Java程序中。JSch获得BSD格式许可证

最初,我们开发这些东西的动机是允许我们的纯Java X服务器 WiredX的用户享受安全的X会话。所以,我们的努力主要是为了实现用于X11转发的SSH2协议。当然,我们现在也有兴趣添加端口转发,文件传输,终端仿真等其他功能。

官网上有很详细说明和例子:

官网:http://www.jcraft.com/jsch/

----------------------------------------------------------------------------------------------------------------------------------

二、 实现demo

1. 工具类:

  • USER:所连接的Linux主机登录时的用户名
  • PASSWORD:登录密码
  • HOST:主机地址
  • DEFAULT_SSH_PROT=端口号,默认为22
代码语言:javascript
复制
package util;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class SSHUtil {
	 private Channel channel;
	    private Session session = null;
	    private int timeout = 60000;

	    public SSHUtil(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();
	            TimeUnit.SECONDS.sleep(2);
	            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();
	    }
}

2. 调用:

代码语言:javascript
复制
import util.SSHUtil;

public class Test {

	public static void main(String[] args) throws Exception{
        SSHUtil sshUtil = new SSHUtil("xx.xx.xx.2", "root", "xxxxxng");
       
        String res = sshUtil.runShell("cd xxx\n ps -ef | grep java | awk '{print $2}' | xargs kill -9 \n nohup java -jar xxxx-0.0.1-SNAPSHOT.jar & \n", "utf-8");
        //重启数据库
        //String res = sshUtil.runShell("docken restart JY_mysql \n", "utf-8");
        //String res = sshUtil.runShell("nohup java -jar forlovehome-0.0.1-SNAPSHOT.jar & \n", "utf-8");
       // String res = sshUtil.runShell("/usr/apache-tomcat-7.0.47/bin/startup.sh\n", "utf-8");
        System.out.println(res);
        sshUtil.close();
	}
}

参考:http://www.importnew.com/22322.html

http://www.jcraft.com/jsch/

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/05/23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、前言
  • 二、 实现demo
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档