JSch - Java Secure Channel : java 代码实现服务器远程操作 - Go语言中文社区

JSch - Java Secure Channel : 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
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. 调用:


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 xxxn 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.shn", "utf-8");
        System.out.println(res);
        sshUtil.close();
	}
}


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

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


版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/u011314442/article/details/80416888
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-02-13 12:30:51
  • 阅读 ( 993 )
  • 分类:前端

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢