首页 技术 正文
技术 2022年11月15日
0 收藏 415 点赞 4,553 浏览 3070 个字

一、Linxu服务器远程启动tomcat

1、首先确保linxu服务器上的tomcat jdk等必要软件正确安装,并且可以正常启动。

2、编写Java SSH工具类。

相关jar包:

        <dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.53</version>
<scope>provided</scope>
</dependency>

工具类:

package com.framework.code.controller;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();
}
}

SSH工具类

测试:

public class SSHHelper {
public static void main(final String[] args) throws Exception {
//shutdown.sh
SSHUtil sshUtil = new SSHUtil("136.16.19.82", "root", "123456");
String res = sshUtil.runShell("/usr/apache-tomcat-7.0.47/bin/startup.sh\n", "utf-8");
System.out.println(res);
sshUtil.close();
}
}

一定要注意的是 命令结尾一定要加\n   [\n代表命令行里敲回车]

例如启动tomcat 可以写成绝对路径

/usr/apache-tomcat-7.0.47/bin/startup.sh\n

还可以写成这样

cd /usr/apache-tomcat-7.0.47/bin/\n./startup.sh\n

相当于在命令行里先 cd /usr/apache-tomcat-7.0.47/bin 回车进入到tomcat的bin目录。

然后在通过./startup.sh启动tomcat  这里的\n就相当于回车了。

二、Windows服务器远程启动tomcat

1、首先下载ssh服务器段软件 http://www.freesshd.com/?ctt=download 我用的是freeSSHd.exe

2、安装软件,前面几部略过,这一步提示是否生成秘钥,选择是。

Java通过ssh连接到Linxu和Windos服务器远程启动Tomcat

3、这一步提示是否设置为系统服务,这里随便。

Java通过ssh连接到Linxu和Windos服务器远程启动Tomcat

4、打开软件进行设置,如果有绿色的已经开启的服务,先都把他们关闭了。

Java通过ssh连接到Linxu和Windos服务器远程启动Tomcat

5、创建用户,设置密码。

Java通过ssh连接到Linxu和Windos服务器远程启动Tomcat

6、配置SSH链接。

Java通过ssh连接到Linxu和Windos服务器远程启动Tomcat

7、启动SSH服务。

Java通过ssh连接到Linxu和Windos服务器远程启动Tomcat

8、用上面的Java客户端代码即可发送脚本到服务器上执行了。

如果提示链接密码错误链接不上,就从安装路径打开软件并重启服务。D:\Program\freeSSHd\FreeSSHDService.exe

多试几次就好了,不知道是不是软件的BUG。

    public static void main(final String[] args) throws Exception {
//shutdown.sh
SSHUtil sshUtil = new SSHUtil("121.92.115.217", "root", "vstar123");
//String res = sshUtil.runShell("cd /usr/apache-tomcat-7.0.47/bin/\n./startup.sh\n", "utf-8");
String res = sshUtil.runShell("D:\\apache-tomcat-7.0.35\\bin\\startup.bat \n", "utf-8");
//String res = sshUtil.runShell("echo %CATALINA_HOME% \n", "utf-8");
System.out.println(res);
sshUtil.close();
}

如果提示缺少tomcat环境变量,则需要在服务器上配置tomcat的环境变量。

CATALINA_HOME = D:\Java\Software\apache-tomcat-7.0.47 配置到这种路径即可,然后需要重启服务器让环境变量生效,不然读不到。

D:\Program\freeSSHd>D:\Java\Software\apache-tomcat-7.0.47\bin\startup.bat
The CATALINA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294