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

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

  1. 获得应用程序所在的机器用户名和密码,然后执行shell脚本完成以上操作
    未采用:因为运维不提供应用服务器的用户名和密码
  2. 直接连接另一台服务器,执行复制文件,然后执行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);

后续shell命令见下一篇博客

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A: 要使用Java通过SFTP连接到Linux服务器,您需要使用JSch库。下面是简单的步骤: 1.下载JSch库并将其添加到您的Java项目中。 2.使用JSch中的Session对象建立连接: ``` JSch jsch = new JSch(); Session session = jsch.getSession(username, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); ``` 3.使用连接的Session对象打开SFTP通道: ``` ChannelSftp channel = (ChannelSftp) session.openChannel("sftp"); channel.connect(); ``` 4.使用SFTP通道上传或下载文件: 上传: ``` channel.put(localFilePath, remoteFilePath); ``` 下载: ``` channel.get(remoteFilePath, localFilePath); ``` 完整的代码示例: ``` import com.jcraft.jsch.*; public class SFTPDemo { public static void main(String[] args) { String username = "username"; String password = "password"; String host = "host"; int port = 22; try { JSch jsch = new JSch(); Session session = jsch.getSession(username, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); ChannelSftp channel = (ChannelSftp) session.openChannel("sftp"); channel.connect(); String remotePath = "/tmp/test.txt"; String localPath = "C:/test.txt"; channel.put(localPath, remotePath); channel.get(remotePath, localPath); channel.exit(); session.disconnect(); } catch (JSchException | SftpException e) { e.printStackTrace(); } } } ``` 请注意,如果您没有SFTP权限,则需要使用SSH密钥进行身份验证。同样,如果您的Linux服务器启用了严格的主机密钥检查,则需要设置“StrictHostKeyChecking”为“no”其他方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值