首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Google sshxcute优化版+二次封装类

Google sshxcute优化版+二次封装类

作者头像
程裕强
发布2018-01-02 16:36:49
1.6K0
发布2018-01-02 16:36:49
举报

1.Google sshxcute优化版

源码下载: https://gitee.com/chengyuqiang/sshxcute

2.二次封装

package cn.hadron.util;

import java.util.HashMap;
import java.util.Map;
import net.neoremind.sshxcute.core.ConnBean;
import net.neoremind.sshxcute.core.IOptionName;
import net.neoremind.sshxcute.core.Result;
import net.neoremind.sshxcute.core.SSHExec;
import net.neoremind.sshxcute.exception.TaskExecFailException;
import net.neoremind.sshxcute.task.CustomTask;
import net.neoremind.sshxcute.task.impl.ExecCommand;
import net.neoremind.sshxcute.task.impl.ExecShellScript;

/**
 * sshxcute.jar二次封装类
 *  在执行命令时,命令前面已经是他的完全路径,多条命令用分号隔开
 *  参考文献 https://my.oschina.net/u/1866821/blog/479409
 * @author chengyuqiang
 *
 */
public final class SSHUtil {
    private SSHExec sshx = null;
    private ConnBean cb = null;

    public SSHUtil(String ip, String username, String password) {
        // 如果遇到失败,仍然想继续执行剩下的任务
        SSHExec.setOption(IOptionName.HALT_ON_FAILURE, true);
        // 修改错误日志输入目录
        SSHExec.setOption(IOptionName.ERROR_MSG_BUFFER_TEMP_FILE_PATH, "/var/log/sshxcute_err.msg");
        this.cb = new ConnBean(ip, username, password);
    }

    /**
     * 连接SSH,私有方法
     * @return
     */
    private boolean connect() {
        if (null == cb) {
            return false;
        }
        sshx = SSHExec.getInstance(cb);
        return sshx.connect();
    }

    /**
     * 执行命令
     * @param command
     * @return
     */
    public Result exec(String... cmd) {
        this.connect();
        // 执行的命令行任务
        CustomTask sampleTask = new ExecCommand(cmd);
        // 执行,并对执行后的结果进行处理
        try {
            return sshx.exec(sampleTask);
        } catch (TaskExecFailException e) {
            e.printStackTrace();
            return null;
        } finally {
            sshx.disconnect();
        }
    }

    /**
     * shellPath 代表脚本执行路径
     * @param shellPath
     * @return
     */
    public Result exeScript(String shellPath) {
        this.connect();
        CustomTask task = new ExecShellScript(shellPath);
        try {
            return sshx.exec(task);
        } catch (TaskExecFailException e) {
            e.printStackTrace();
            return null;
        } finally {
            sshx.disconnect();
        }
    }

    /**
     * 带参执行脚本
     * @param shellPath
     * @param args
     * @return
     */
    public Result exeScript(String shellPath, String args) {
        this.connect();
        CustomTask task = new ExecShellScript(shellPath, args);
        try {
            return sshx.exec(task);
        } catch (TaskExecFailException e) {
            e.printStackTrace();
            return null;
        } finally {
            sshx.disconnect();
        }
    }

    /**
     * 上传文件
     * @param localFile
     * @param remotePath
     */
    public boolean putFile(String localFile, String remotePath) {
        this.connect();
        try {
            sshx.uploadSingleDataToServer(localFile, remotePath);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            sshx.disconnect();
        }
    }

    /**
     * 上传目录
     * @param localPath
     * @param remotePath
     */
    public boolean putDir(String localPath, String remotePath) {
        this.connect();
        try {
            sshx.uploadAllDataToServer(localPath, remotePath);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            sshx.disconnect();
        }
    }

    /**
     * 解析结果
     * @param result
     * @return
     */
    public Map<String, String> parseResult(Result result) {
        if(null==result){
            System.out.println("Result空值!");
            return null;
        }
        Map<String, String> map = new HashMap<>();
        // Check result and print out messages.
        if (result.isSuccess) {
            map.put("isSuccess", "true");
            map.put("returnCode", result.rc + "");
            map.put("sysout", result.sysout);
        } else {
            map.put("isSuccess", "false");
            map.put("returnCode", result.rc + "");
            map.put("sysout", result.error_msg);
        }
        return map;
    }
    /**
     * 解析结果
     * @param result
     * @return
     */
    public String getString(Result result) {
        if(null==result){
            System.out.println("Result空值!");
            return null;
        }
        if (result.isSuccess) {
            return result.sysout;
        } else {
            return result.error_msg;
        }
    }

    /**
     * 测试方法
     * @param args
     */
    public static void main(String[] args) {
        SSHUtil ssh = new SSHUtil("192.168.2.91", "root", "123456");
        Result result=ssh.exec("date");
        Map<String, String> map=ssh.parseResult(result);
        for(String key:map.keySet()){
            System.out.println(key+":"+map.get(key));
        }
        System.out.println("-------------------------------------");
        result=ssh.exec("ls -l", "uname");
        map=ssh.parseResult(result);
        for(String key:map.keySet()){
            System.out.println(key+":"+map.get(key));
        }
        System.out.println("-------------------------------------");
        boolean success=ssh.putFile("/root/sshxcuteTest.sh", "/root/sshxcuteTest.sh");
        if(success){
            System.out.println("上传成功!");
        }else{
            System.out.println("上传失败!");
        }
        System.out.println("###-------------------------------------");
        //参数放到同一个字符串中,通过空格分割
        result=ssh.exeScript("/root/sshxcuteTest.sh", "hello world");
        System.out.println(ssh.getString(result));

    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-09-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.Google sshxcute优化版
  • 2.二次封装
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档