windows(非linux系统)执行linux命令,向linux上传文件 JSch使用

JSch基本使用

JSch 是SSH2的一个纯Java实现。它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等。你可以将它的功能集成到你自己的 程序中。同时该项目也提供一个J2ME版本用来在手机上直连SSHD服务器。

官网:http://www.jcraft.com/jsch/中有很多例子http://www.jcraft.com/jsch/examples/,这里先采用(已做修改)其中2个来进行简单论述,希望对大家有所帮助。
本文采用的jsch版本是0.1.51. 下载地址:http://sourceforge.net/projects/jsch/files/jsch/0.1.54/jsch-0.1.54.zip/download
本文采用的Linux操作系统是CentOS6.5.

TIPS: 查看Linux操作系统(内核)版本可以使用:uname -a; uname -r; cat /etc/issue; cat /etc/redhat-release等命令。

package com.lls.jsch;

import com.jcraft.jsch.*;


import java.io.*;
import java.util.Properties;

public class JschTest {



    public Session getSshSession(SshInfo sshInfo ){
        JSch jSch = new JSch();
        Session session=null;
        try{
            session = jSch.getSession(sshInfo.getUserName(),sshInfo.getIp(),sshInfo.getPort());
            session.setPassword(sshInfo.getPassword());
            Properties conf = new Properties();
            conf.setProperty("StrictHostKeyChecking","no");
            session.setConfig(conf);
            session.connect();

        } catch (JSchException e) {
            e.printStackTrace();
        }
        return session;
    }

    /**
     * 其他操作系统执行linux 命令
     * @param sshInfo
     * @param linuxCmd
     */
    public void exeLinuxCmd(SshInfo sshInfo ,String linuxCmd){
        Session session = null;
        ChannelExec exec = null;
        try{
            session = getSshSession(sshInfo);
            exec = (ChannelExec) session.openChannel("exec");
            exec.setCommand(linuxCmd);
            exec.setInputStream(null);
            exec.setErrStream(System.err);
            exec.connect();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
            String msg = null;
            while ((msg = bufferedReader.readLine()) != null){
                System.out.println(String.format("执行命令$%1$s返回消息%2$s",linuxCmd,msg));
            }

        } catch (JSchException | FileNotFoundException | SftpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(exec != null && exec.isConnected()){
                exec.disconnect();
            }
            if(session != null && session.isConnected()){
                session.disconnect();
            }
        }

    }

    /**
     * 其他操作系统向linux 系统传文件
     * @param sshInfo
     * @param localFilePath
     * @param remoteDirPath
     * @throws IOException
     */
    public void uploadFile(SshInfo sshInfo,String localFilePath,String remoteDirPath) throws IOException {
        Session session = null;
        ChannelSftp sftp = null;
        InputStream inputStream = null;
        try{
            session = getSshSession(sshInfo);
            sftp = (ChannelSftp) session.openChannel("sftp");
            sftp.connect();
            sftp.cd(remoteDirPath);

            File file = new File(localFilePath);
            if(file.exists()){
                inputStream = new FileInputStream(file);
                sftp.put(inputStream,file.getName());
            }
        } catch (JSchException | FileNotFoundException | SftpException e) {
            e.printStackTrace();
        }finally {
            if(inputStream != null){
                inputStream.close();
            }
            if(sftp != null && sftp.isConnected()){
                sftp.disconnect();
            }
            if(session != null && session.isConnected()){
                session.disconnect();
            }
        }


    }







    class SshInfo{
        private String userName;
        private String password;
        private String ip;
        private int port;

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getIp() {
            return ip;
        }

        public void setIp(String ip) {
            this.ip = ip;
        }

        public int getPort() {
            return port;
        }

        public void setPort(int port) {
            this.port = port;
        }
    }



}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值