zoukankan      html  css  js  c++  java
  • Java通过ssh连接到Linxu和Windos服务器远程启动Tomcat

    一、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
    ", "utf-8");
            System.out.println(res);
            sshUtil.close();
        }
    }

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

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

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

    还可以写成这样

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

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

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

    二、Windows服务器远程启动tomcat

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

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

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

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

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

     6、配置SSH链接。

    7、启动SSH服务。

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

    如果提示链接密码错误链接不上,就从安装路径打开软件并重启服务。D:ProgramfreeSSHdFreeSSHDService.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/
    ./startup.sh
    ", "utf-8");
            String res = sshUtil.runShell("D:\apache-tomcat-7.0.35\bin\startup.bat 
    ", "utf-8");
            //String res = sshUtil.runShell("echo %CATALINA_HOME% 
    ", "utf-8");
            System.out.println(res);
            sshUtil.close();
        }

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

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

    D:ProgramfreeSSHd>D:JavaSoftwareapache-tomcat-7.0.47instartup.bat 
    The CATALINA_HOME environment variable is not defined correctly
    This environment variable is needed to run this program
  • 相关阅读:
    Cypress系列(32)- url() 命令详解
    Cypress系列(31)- title() 命令详解
    Cypress系列(30)- 操作浏览器的命令
    Cypress系列(29)- 获取页面全局对象的命令
    Cypress系列(28)- scrollTo() 命令详解
    Cypress系列(27)- scrollIntoView() 命令详解
    Cypress系列(26)- 聚焦与失焦命令的详解
    Cypress系列(25)- submit() 命令详解
    Cypress系列(24)- 操作页面元素的命令
    urlencoded、json 格式详解
  • 原文地址:https://www.cnblogs.com/daxin/p/5016438.html
Copyright © 2011-2022 走看看