JAVA实现利用Jsch管理服务器端相关服务

由于个人服务器资源有限,有些服务不需要一直开启,只是临时使用。但是又不想登陆服务器去关闭或开启。那么能不能将这个做成一个功能。在页面上点击一个按钮就可以开启或关闭呢。马上百度,还真让我查到了有这样的方法。

参考网上的例子:

package com.zlw.common;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * @author zhoulw
 * @version 创建时间:2019-5-13 下午3:49:44
 * @ClassName 类名称
 * @Description 类描述
 */
public class SSHCommand {

	private static final Logger log = LoggerFactory.getLogger(SSHCommand.class);

	private Session session = null;
	private Channel channel = null;

	private String HostName = "主机IP";
	private int HostPort = 22;
	private String UserName = "用户名";
	private String UserPassword = "密码";
	private int timeout = 60000;

	/**
	 * 获取连接服务器
	 * @return
	 */
	private ChannelExec getChannelExec() {
		try {
			if (channel!= null && channel.isConnected()) {
				return (ChannelExec)channel;
			}
			JSch jSch = new JSch();
			if (session == null || !session.isConnected()) {
				session = jSch.getSession(UserName, HostName, HostPort);
				session.setPassword(UserPassword);
				Properties config = new Properties();
				config.put("StrictHostKeyChecking", "no");
				session.setConfig(config);
				session.setTimeout(timeout);
				session.connect();
			}
			channel = session.openChannel("exec");
		} catch (Exception e) {
			// TODO: handle exception
			if (session != null) {
				session.disconnect();
				session = null;
			}
			channel = null;
		}
		return channel == null ? null : (ChannelExec) channel;
	}

	/**
	 * 关闭连接
	 */
	private void closeChannel() {
		try {
			if (channel != null) {
				channel.disconnect();
				channel = null;
			}
			if (session != null) {
				session.disconnect();
				session = null;
			}
		} catch (Exception e) {
			System.out.println(e);
		}
	}

	/**
	 * 执行服务器端命令
	 * @param command
	 * @return
	 */
	public boolean executeCommand(String command) {
		boolean flag = false;
		ChannelExec channelExec = this.getChannelExec();
		if (channelExec == null) {
			return false;
		}
		try {
			channelExec.setInputStream(null);
			channelExec.setErrStream(System.err);
			channelExec.setCommand(command);
			InputStream in = channelExec.getInputStream();
			channelExec.connect();
			BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
			String line = null;
			while ((line = reader.readLine()) != null) {
				System.out.println(line);
			}
			reader.close();
			this.closeChannel();
			flag = true;
		} catch (Exception e) {
			System.out.println(e);
		}
		return flag;
	}

	public static void main(String[] args) {
		SSHCommand sshCommand = new SSHCommand();
        //多个命令请用";"隔开
		String command = "cd /usr/local/ngrok/bin" + ";" + "nohup ./ngrokd -domain=\"ngrok.test.cn\" -httpAddr=\":8080\" -httpsAddr=\":8089\" &";
		// String command = "cd /usr/local; ls -l";
		System.out.println(sshCommand.executeCommand(command));
	}
}

这样就基本完成了。测试了一下,还是蛮方便的。有些命名提示找不到,可能是需要转一下吧。这个就需要自己去摸索了。

这里需要个jsch的jar包。csdn里面的可真鸡儿贵呀,承受不了。好在我找到了免费的了jsch-0.1.50.jar 免积分

但是这个jar包加入后会导致如下的问题:

Could not open windows registry node Software\JavaSoft\Prefs at root 0x80000002. Windows RegOpenKey(...) returned error code 2

解决办法如下:

1、win+r 输入 regedit

 找到下面的文件 HKEY_CURRENT_USER\Softwar\JavaSoft\Prefs,修改权限为完全控制

修改之后,结果还是不行

2、找HKEY_LOCAL_MACHINE \SOFTWARE\JavaSoft\Prefs, 如果不存在Prefs就新建一个,修改权限为完全控制

然后重新启动项目,可以运行了。

然而还是有点问题,我开启了一个nrgok服务,测试服务确实开启了,但是一旦我关闭了Tomcat,ngrok服务也随即关闭,也就是说我必须要一直保持连接才行。虽然我这个程序最后会部署在那台服务上,但是这个显然不是我需要的。差那么一点点。但是还是有点用的。

觉得有用的,也不用感谢我,点个赞就成

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值