由于个人服务器资源有限,有些服务不需要一直开启,只是临时使用。但是又不想登陆服务器去关闭或开启。那么能不能将这个做成一个功能。在页面上点击一个按钮就可以开启或关闭呢。马上百度,还真让我查到了有这样的方法。
参考网上的例子:
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服务也随即关闭,也就是说我必须要一直保持连接才行。虽然我这个程序最后会部署在那台服务上,但是这个显然不是我需要的。差那么一点点。但是还是有点用的。
觉得有用的,也不用感谢我,点个赞就成。