一、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(); } }
測試:
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