sftp上传文件到linux服务器上(ssh验证)

需求:

以前,手动上传配置文件到服务器,然后手工复制到另外一台服务器上,然后登陆SSH Secure File Transfer Client客户端,执行相关shell命令号
现在这些操作需要一键完成,即文件复制到另一台服务器,登陆ssh客户端,切换用户,执行导入命令行
解决办法:

  • 获得应用程序所在的机器用户名和密码,然后执行shell脚本完成以上操作
  • 未采用:因为运维不提供应用服务器的用户名和密码
    直接连接另一台服务器,执行复制文件,然后执行shell脚本(采取)
    要上传文件到linux服务器上,使用FTP协议,公司linux服务器上需要ftp客户端需要安全验证,比较麻烦,所以想到使用sftp来解决,sftp是基于ssh的安全协议的ftp协议,Java中有JSch包来实现连接服务器。

第一步:下载JSch包,请从官网下载它:http://www.jcraft.com/jsch/
第二步:新建FtpsFileList.java文件

//只有上传方法,也可以有下载文件方法
public class FtpsFileList {
    private static final Logger LOG = LoggerFactory.getLogger(FtpsFileList.class);

    //将本地的dirFile这个文件复制到远程服务器上的desFile文件夹下
    public static void loadFile(String host, int port, String username, final String password, String dirFile,String desFile) {
        ChannelSftp sftp = null;
        Channel channel = null;
        Session sshSession = null;
        try {
            JSch jsch = new JSch();//创建一个jsch对象
            jsch.getSession(username, host, port);
            // 根据用户名,主机ip,端口获取一个Session对象
            sshSession = jsch.getSession(username, host, port);
            //设置密码
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            // 为Session对象设置properties
            sshSession.setConfig(sshConfig);
            sshSession.connect();
            LOG.debug("Session connected!");
            // 打开SFTP通道
            channel = sshSession.openChannel("sftp");
            // 建立SFTP通道的连接
            channel.connect();
            LOG.debug("Channel connected!");
            sftp = (ChannelSftp) channel;

            //InputStream is = sftp.get("/ftp/re/20140713.dat");
            InputStream fis = new FileInputStream(new File(dirFile));
            //文件上传(通过inputstream流来实现)  详见https://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html
            sftp.put(fis,desFile);
            LOG.info("文件复制到服务器成功!\r");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeChannel(sftp);
            closeChannel(channel);
            closeSession(sshSession);
        }
    }

    private static void closeChannel(Channel channel) {
        if (channel != null) {
            if (channel.isConnected()) {
                channel.disconnect();
            }
        }
    }

    private static void closeSession(Session session) {
        if (session != null) {
            if (session.isConnected()) {
                session.disconnect();
            }
        }
    }
}

调用方式:

FtpsFileList.loadFile("10.31.92.70", 22, "serviceop", "115LbUrAbEsZw","/nfsc/qhcs-ansir-stg1/ansir/HanLP/xiaodai/loan-freebase.ttl","/wls/serviceop/virtuoso_script/loanXD.ttl");
logger.info("ttl文件复制成功");

到这一步,已经完成上传文件到linux服务器上

第三步:写好shell脚本(关键核心),这一步学习了好多东西,发现shell命令挺好玩的

#!/usr/bin/expect
spawn ssh serviceop@10.31.92.70
set timeout 2
expect "password:"
send "115LbUrAbEsZw\r"
expect "*]#"
set password "wuxin952"
spawn su root
expect "password:"  
send "wuxin952\r" 
expect "#"
send "/wls/serviceop/virtuoso-opensource/home/bin/isql localhost:13002\r" 
send "DB.DBA.TTLP_MT(file_to_string_output('/wls/serviceop/virtuoso_script/loan.ttl'),'','http://www.xiaowei.com');\r"
interact

注释:因为需要交互式输入密码,所以选择使用expect命令环境来执行
具体释义见:

第四步:java代码调用shell脚本,代码如下,
Process类是一个抽象类,用于定义一个本地进程,Runtime.getRuntime().exec(sh)返回一个进程对象
具体见:process

//授予权利给shell脚本呢
Process ps1=Runtime.getRuntime().exec("chmod 777 /nfsc/qhcs-ansir-stg1/ansir/HanLP/xiaodai/bash_scp.sh");
//等待当前线程执行完,等待返回process类对象表示的进程结束
ps1.waitFor();
logger.info("chmod命令执行结束");

BufferedReader br1 = new BufferedReader(new InputStreamReader(ps1.getInputStream()));
while ((line = br1.readLine()) != null) {
    logger.info("chmod命令结果:"+line);
}
//实际调用使用expect 文件
Process ps2=Runtime.getRuntime().exec("expect /nfsc/qhcs-ansir-stg1/ansir/HanLP/xiaodai/bash_scp.sh");
ps2.waitFor();
logger.info("expect命令执行结束");

BufferedReader br2 = new BufferedReader(new InputStreamReader(ps2.getInputStream()));
while ((line = br2.readLine()) != null) {
    logger.info("expect命令结果:"+line);
}

String result = sb.toString();
logger.info("expect整理结果为:"+result);
标签:SSHLinux 发布于:2019-11-02 16:24:45