JSch-远程调用linux命令和文件上传

pom.xml配置依赖

        
            com.jcraft
            jsch
            0.1.53  
         

一、远程调用linux命令

1、如果服务器只是单纯的用户名,密码登录代码如下:

public class RemoteShellUtils {
    private static final Logger LOG = Logger.getLogger(RemoteShellUtils.class);
    private Channel channel = null;
    private Session session = null;
    // 用户名
    private static String user;
    // 密码
    private static String passwd;
    // ip地址
    private static String host;
    // 端口号
    private static int port;

    static {
        InputStream in = ClassLoader
                .getSystemResourceAsStream("store.properties");
        Properties prop = new Properties();
        try {
            prop.load(in);
            user = prop.getProperty("server.user");
            passwd = prop.getProperty("server.passwd");
            host = prop.getProperty("server.host");
            port = Integer.valueOf(prop.getProperty("server.port"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取session
     * 
     * @param user服务器用户名
     * @param passwd服务器密码
     * @param host服务器ip地址
     * @param port服务器端口号
     * @return
     * @throws JSchException
     */
    public Session getSession() throws JSchException {
        // 创建JSch对象
        JSch jsch = new JSch();
        session = jsch.getSession(user, host, port);
        LOG.info("session创建成功");
        // 设置密码
        session.setPassword(passwd);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        // 设置timeout时间
        session.setTimeout(timeout);
        // 通过Session建立连接
        session.connect();
        LOG.info("session连接成功");
        return session;
    }

    /**
     * 创建ChannelExec
     * @return
     * @throws JSchException
     */
    private ChannelExec getExecChannel() throws JSchException {
        if (session == null) {
            session = getSession();
        }
        // 打开SFTP通道
        channel = session.openChannel("exec");
        // LOG.info("Connected successfully to " + host + ":" + port);
        return (ChannelExec) channel;
    }
    /**
     * 执行shell命令
     * 
     * @param command
     */

    public boolean execShell(String command) {
        BufferedReader reader = null;
            channel = getExecChannel();
            ((ChannelExec) channel).setCommand(command);
            channel.setInputStream(null);
            channel.connect(); // 建立SFTP通道的连接
            InputStream in = channel.getInputStream();
            reader = new BufferedReader(new InputStreamReader(in));
            String buf = null;

            while ((buf = reader.readLine()) != null) {
                System.out.println(buf);
            }
        }
      /**
      *关闭连接资源
      */
      public void closeChannel() {
        if (channel != null) {
            channel.disconnect();
        }
        if (session != null) {
            session.disconnect();
        }
    }
    }

2、如果服务器是秘钥加密码登录,则创建session方法需要修改

    /**
     * 获取session
     * 
     * @param user
     * @param passwd
     * @param host
     * @param port
     * @return
     * @throws JSchException
     */
    public Session getSession() throws JSchException {
        // 创建JSch对象
        JSch jsch = new JSch();
    // 根据用户名,主机ip,端口号,获取一个Session对象
        //获取秘钥
        String privateKey = ClassLoader.getSystemResource("lie_rsa").getPath()
            .substring(1);
        jsch.addIdentity(privateKey, passwd);
        session = jsch.getSession(user, host, port);
        LOG.info("session创建成功");
        // 设置密码
        //session.setPassword(passwd);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        // 设置timeout时间
        session.setTimeout(timeout);
        // 通过Session建立连接
        session.connect();
        LOG.info("session连接成功");
        return session;
    }

二、上传文件

    /**
     * 文件上传
     * 
     * @param src本地文件路径
     * @param dst上传到服务器的地址
     */
    public void uploadFile(String src,String dst) {
        ChannelSftp channelSftp = null;
        FileInputStream fileInputStream = null;
        channelSftp = getSFTPChannel();
        fileInputStream = new FileInputStream(src);
        channelSftp.put(fileInputStream,dst,ChannelSftp.OVERWRITE);
    }

你可能感兴趣的:(JSch-远程调用linux命令和文件上传)