• Ganymed

    SSH-2 for Java是一个纯Java实现的SHH2库,官网为http://www.ganymed.ethz.ch/ssh2/,最新的更新时间为2006年10月,在用之前,请仔细看一下FAQ,真的能避免很多很多问题

    在google上找到的ganymed-ssh2的官网是http://www.ganymed.ethz.ch/ssh2/,进去看官网的英文简介可以看到该网站已经不维护该项目,并已经迁移到http://www.cleondris.ch/,在这个网站点击右上角的Contact,再点击open source就可以看到这个项目的新家,http://www.cleondris.ch/opensource/ssh2/,上面简单介绍了该项目能远程连接上远程机器,支持命令模式和shell模式,本地和远程端口转发,没有任何JCE依赖等,最后特别指出这个项目是为瑞士苏黎世的一个项目所创建。下面提供了2010-08-23发布的ganymed-ssh2-build251beta1.zip可供下载使用,下面还有在线文档和FAQ供开发者参考。
  • JSch

    采用java编写,使用ssh来操作远程服务器。JSch 是SSH2的一个纯Java实现。它允许你连接到一个sshd 服务器,支持文件的上传和下载,支持在远程机上执行shell命令、shell脚本、重启等操作。

    但是这个类库偏向底层,仅是ssh2的实现,连文件夹的上传下载都不支持,并不是针对自动化部署的,编写的代码比较长,上手和实际使用起来不太方便,所以要对其进行必要的封装,比如封装连接的获取释放、文件夹和文件的拷贝、远程命令的执行等。
  • sshxcute

    sshxcute 框架是对JSch 的简单封装,提供了更为便捷的 API 接口,提供了更加灵活实用的功能,从而可以让开发人员更加得心应手的使用。sshxcute 是一个框架,它允许工程师利用 Java 代码通过 SSH 连接远程执行 Linux/UNIX 系统上的命令或者脚本,这种方式不管是针对软件测试还是系统部署,都简化了自动化测试与系统环境部署的步骤。

    但是它的封装比较简单,功能比较弱,只有上传和执行命令或脚本的功能。

  • 包装Ganymed。实现了文件的上传下载,文件夹的上传,远程执行命令,执行本地命令等基础API:

package base;

import java.io.IOException;
import java.io.InputStream;

public final class ExecLocakCommand {

    public static final String processUseBasic(String cmd) {
        Process p = null;
        StringBuilder sb = new StringBuilder();
        try {
            String os = System.getProperty("os.name").toLowerCase();
            if (os.startsWith("win")) {
                String commands = "cmd /c " + cmd;
                p = Runtime.getRuntime().exec(commands);
            } else if (os.startsWith("linux")) {
                String[] commands = new String[] { "/bin/sh", "-c", cmd };
                p = Runtime.getRuntime().exec(commands);
            }

            String error = read(p.getErrorStream());
            String outInfo = read(p.getInputStream());

            String resultCode = "0";// 脚本中输出0表示命令执行成功
            if (error.length() != 0) { // 如果错误流中有内容,表明脚本执行有问题
                resultCode = "1";
            }

            sb.append(resultCode).append("\n");
            sb.append(error).append("\n");
            sb.append(outInfo);

            p.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                p.getErrorStream().close();
                p.getInputStream().close();
                p.getOutputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    public static final String read(InputStream in) throws IOException {
        StringBuilder sb = new StringBuilder();
        int ch;
        while (-1 != (ch = in.read()))
            sb.append((char) ch);
        return sb.toString();
    }

    public static void main(String[] args) {
         String comands = "dir";
        //String comands = "ls ";
        String ret = ExecLocakCommand.processUseBasic(comands);
        System.out.println(ret);
    }
}
package base;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class propertyUtil {

    private static Properties prop = new Properties();

    private static void load(String fileName) {
        try {
            prop.load(new FileInputStream(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getProperty(String fileName, String key) {
        load(fileName);
        return prop.getProperty(key);
    }

    public static void setProper(String fileName, String key, String value) {
        try {
            load(fileName);
            prop.setProperty(key, value);
            FileOutputStream fos = new FileOutputStream(fileName);
            prop.store(fos, null);
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        System.out.println(propertyUtil.getProperty("test.properties", "key"));
        propertyUtil.setProper("test.properties", "key", "xxxx");
        System.out.println(propertyUtil.getProperty("test.properties", "key"));
    }
}
package base;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.google.common.base.Splitter;

import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

public class RemoteExecutionApi {
    private int port = 22;
    private String username;
    private String password;

    public RemoteExecutionApi(int port, String username, String password) {
        super();
        this.port = port;
        this.username = username;
        this.password = password;
    }

    public RemoteExecutionApi(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    // 下载文件,目前只能下载单个文件
    public void getFile(String remoteFile, String localTargetDirectory, String ips) {
        Iterable<String> result = Splitter.on(',').trimResults().omitEmptyStrings().split(ips);

        for (String ip : result) {
            Connection conn = new Connection(ip, port);
            try {
                conn.connect();
                boolean isAuthenticated = conn.authenticateWithPassword(username, password);
                if (isAuthenticated == false) {
                    System.err.println("authentication failed");
                }
                SCPClient client = new SCPClient(conn);
                client.get(remoteFile, localTargetDirectory);
                conn.close();
            } catch (IOException ex) {
                ex.printStackTrace();
                // Logger operator
                System.exit(2);
            }
        }
    }

    //上传文件或者文件夹
    public void putFile(String localFile, String remoteTargetDirectory, String ips) {
        Iterable<String> result = Splitter.on(',').trimResults().omitEmptyStrings().split(ips);
        for (String ip : result) {
            Connection conn = new Connection(ip, port);
            try {
                conn.connect();
                boolean isAuthenticated = conn.authenticateWithPassword(username, password);
                if (isAuthenticated == false) {
                    System.err.println("authentication failed");
                }
                // folder
                if (new File(localFile).isDirectory()) {
                    // 先创建根目录
                    String dirName = new File(localFile).getName();
                    remoteTargetDirectory = remoteTargetDirectory + "/" + dirName;
                    Session sess1 = conn.openSession();
                    sess1.execCommand("mkdir -p " + remoteTargetDirectory);
                    sess1.waitForCondition(ChannelCondition.EOF, 0);
                    sess1.close();
                    putDir(conn, localFile, remoteTargetDirectory);

                } else if (new File(localFile).isFile()) {// file
                    SCPClient client = new SCPClient(conn);
                    client.put(localFile, remoteTargetDirectory);
                }
                conn.close();

            } catch (IOException ex) {
                ex.printStackTrace();
                // Logger operator
                System.exit(2);
            }
        }
    }

    private void putDir(Connection conn, String localDirectory, String remoteTargetDirectory) throws IOException {
        String[] fileList = new File(localDirectory).list();
        for (String file : fileList) {
            String fullFileName = localDirectory + new File(localDirectory).separator + file;

            if (new File(fullFileName).isDirectory()) {
                final String subDir = remoteTargetDirectory + "/" + file;
                Session sess = conn.openSession();
                sess.execCommand("mkdir " + subDir);
                sess.waitForCondition(ChannelCondition.EOF, 0);
                sess.close();
                putDir(conn, fullFileName, subDir);
            } else {
                SCPClient client = new SCPClient(conn);
                client.put(fullFileName, remoteTargetDirectory);
            }
        }
    }

    // 执行命令
    public String runCommand(String command, String ips) {
        StringBuilder sb = new StringBuilder();
        Iterable<String> result = Splitter.on(',').trimResults().omitEmptyStrings().split(ips);
        for (String ip : result) {
            Connection conn = new Connection(ip, port);
            try {
                conn.connect();
                boolean isAuthenticated = conn.authenticateWithPassword(username, password);
                if (isAuthenticated == false) {
                    System.err.println("authentication failed");
                }

                Session sess = conn.openSession();
                sess.execCommand(command);

                InputStream stdout = new StreamGobbler(sess.getStdout());
                BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
                while (true) {
                    String line = br.readLine();
                    if (line == null)
                        break;
                    sb.append(line).append("\n");
                }

                System.out.println("ExitCode: " + sess.getExitStatus());
                br.close();
                sess.close();
                conn.close();

            } catch (IOException ex) {
                ex.printStackTrace(System.err);
                // Logger operator
                System.exit(2);
            }
        }

        return sb.toString();
    }

    // 删除临时文件
    public void delTempDir(String remotePath, String ips) {
        runCommand("rm -rf " + remotePath, ips);
    }

    // 修改配置文件
    public void modfiyPropertyFile(String remoteFileName, String key, String value, String ips) {
        String tempDir = "tempDir";
        File folder = new File(tempDir);
        folder.mkdirs();

        Iterable<String> result = Splitter.on(',').trimResults().omitEmptyStrings().split(ips);
        for (String ip : result) {
            Connection conn = new Connection(ip, port);
            try {
                conn.connect();
                boolean isAuthenticated = conn.authenticateWithPassword(username, password);
                if (isAuthenticated == false) {
                    System.err.println("authentication failed");
                }

                SCPClient client = new SCPClient(conn);
                client.get(remoteFileName, tempDir);

                String tmpFileName = tempDir + File.separator
                        + remoteFileName.substring(remoteFileName.lastIndexOf("/"));

                propertyUtil.setProper(tmpFileName, key, value);

                client.put(tmpFileName, remoteFileName.substring(0, remoteFileName.lastIndexOf('/')));

                conn.close();

            } catch (IOException ex) {
                ex.printStackTrace(System.err);
                // Logger operator
                System.exit(2);
            }
        }

        clearDir(folder);
    }

    private void clearDir(File file) {
        if (file.isDirectory()) {
            for (File f : file.listFiles()) {
                clearDir(f);
                f.delete();
            }
        }
        file.delete();
    }

    // 在配置文件后添加新行
    public void propertyFileAddNewline(String remoteFileName, String newline, String ips) {
        runCommand("echo " + newline + " >> " + remoteFileName, ips);
    }

    // 重启机器
    public void reboot(String ips) {
        runCommand("reboot", ips);
    }

    // 执行本地命令
    public String runLoaclCommand(String command) {
        return ExecLocakCommand.processUseBasic(command);
    }

    public static void main(String[] args) {
        RemoteExecutionApi client = new RemoteExecutionApi("root", "123456");
        // client.getFile("/root/test.txt","C:", "192.168.238.129");
        //client.putFile("D:\\test", "/root", "192.168.238.129");
        // String ret = client.runCommand("ls /", "192.168.238.129");
        // System.out.println(ret);
        // client.putDir("D:\\test", "/root", "192.168.238.129");
        // client.modfiyPropertyFile("/root/test.proprety", "key", "yyy",
        // "192.168.238.129");
        // client.propertyFileAddNewline("/root/xx.txt", "yyyyy=xxxxx",
        // "192.168.238.129");
        String ret = client.runLoaclCommand("dir");
        System.out.println(ret);
        System.out.println("----");
    }

}

Ganymed实现基本的自动化部署API的更多相关文章

  1. 品尝阿里云容器服务:初步尝试ASP.NET Core Web API站点的Docker自动化部署

    部署场景是这样的,我们基于 ASP.NET Core 2.0 Preview 1 开发了一个用于管理缓存的 Web API ,想通过阿里云容器服务基于 Docker 部署为内网服务. 在这篇博文中分享 ...

  2. .Net Core自动化部署系列(三):使用GitLab CI/CD 自动部署Api到Docker

    之前写过使用Jenkins实现自动化部署,最近正好没事研究了下GitLab的自动化部署,顺便记录一下. 使用GitLab部署我们需要准备两件事,第一个起码你得有个GitLab,自己搭建或者使用官方的都 ...

  3. fabric自动化部署django

    使用fabric部署django应用 使用fabric部署django应用 本文是我的网站易读中文网自动化部署的脚本实现,以下代码在ubuntu和debian中测试通过 由于网站使用的是python技 ...

  4. Cobbler自动化部署最佳实践

    第1章 Cobbler自动化部署最佳实践 运维自动化在生产环境中占据着举足轻重的地位,尤其是面对几百台,几千台甚至几万台的服务器时,仅仅是安装操作系统,如果不通过自动化来完成,根本是不可想象的. 面对 ...

  5. Linux-GitLab+Jenkins持续集成+自动化部署

    GitLab+Jenkins持续集成+自动化部署 什么是持续集成? (1)Continuous integration (CI) 持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个 ...

  6. 搭建jenkins实现自动化部署

    搭建jenkins实现自动化部署 一.安装jenkins 1.添加yum repos,然后安装 sudo wget -O /etc/yum.repos.d/jenkins.repo https://p ...

  7. 使用第三方容器服务,自动化部署.Net Core

    1.为什么用第三方,而不自建,有哪些第三方,最后实现的效果 a.尝试过自建,并成功了,但是很麻烦,要敲一堆命令,无法达到全自动化部署的要求. b.自建,就算用第三方的镜像包,感觉下载还是不快,不知道为 ...

  8. 利用Jenkins实现JavaWeb项目的自动化部署

    修改代码,打包,上传,重启... 大把的时间花费在这些重复无味的工作上.笔者与当前主流的价值观保持一致:我们应该把时间花费在更有意义的事情上.我们可以尝试借助一些工具,让这些重复机械的工作交给计算机去 ...

  9. Linux 自动化部署

    1.pexpect Pexpect 是 Don Libes 的 Expect 语言的一个 Python 实现,是一个用来启动子程序,并使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的 Py ...

随机推荐

  1. jquery 获取checkbox 选中值并拼接字符集

    1.代码示例: var chk_value =[]; $('input[name="rewardids"]:checked').each(function(){   chk_val ...

  2. 437. Path Sum III(路径可以任意点开始,任意点结束)

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  3. SqlHelper简单实现(通过Expression和反射)7.MySql数据处理类

    MySql的数据处理类和Sql Server没有太大差别,从思路上来说没有区别,所以此处只是给出代码,不再多加解释了. using System; using System.Configuration ...

  4. OpenCL 学习step by step (5) 使用二维NDRange workgroup

    http://www.cnblogs.com/mikewolf2002/archive/2012/09/07/2675634.html 在本教程中,我们使用二维NDRange来设置workgroup, ...

  5. jQuery UI 自定义样式的日历控件

    在线演示 本地下载

  6. [CF]Cycling City

    题目大意:给定一张无向图,问图中是否存在两个点,使得这两个点之间有三条路径,而且三条路径没有公共点. 解法: 我们可以先走出来一个环,再出环上任意一点走到另外一点.就像这样:

  7. 第十节课-RNN介绍

    2017-08-21 这次的课程介绍了RNN的相关知识: 首先是RNN的几种模型: 分别又不同的应用场景,包括机器翻译,视频的分类... RNN的解释: 主要的特点就是用到了上一个隐含状态的信息,所以 ...

  8. adjacent cache line prefetch

    adjacent cache line prefetch 预读取邻近的缓存数据. 计算机在读取数据时,会智能的认为要读取的数据旁边或邻近的数据也是需要的, 那么其在处理的时候就会将这些邻近的数据预先读 ...

  9. mysql的binlog查看

    1.如果是在window下可以进入mysql的安装bin下,把从linux上拷贝出来的binlog进行查看,步骤如下 a.在mysql的安装bin下右键在此处打开命令行 b.执行命令 C:\Progr ...

  10. sqlserver一般需求

    1.创建一个函数:将用逗号分隔的字符串变成一个表的一列,这样就可以运用在select 语句的in中查询 create Function StrToTable(@str varchar()) Retur ...