Google sshxcute优化版+二次封装类

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));

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值