您的位置:首页 > 运维架构 > Tomcat

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

2015-12-03 15:36 555 查看
一、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、安装软件,前面几部略过,这一步提示是否生成秘钥,选择是。



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



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



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



6、配置SSH链接。



7、启动SSH服务。



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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: