ftp免交互自动上传文件脚本

分享如何通过shell脚本实现FTP非交互上传,以及利用SSH密钥对实现无密码传输,包括sftp的可能应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

缘由

工作中经常用ftp上传文件,共享给他人,在免交互模式下,可以使用shell脚本,固化用户名、密码等固定输入的重复信息

shell脚本例子

#!/bin/bash
# 根据入参指定目录或当前目录下,根据固定目录结构和固定目标文件信息,自动免交互上传文件
# 借鉴jQuery自动推导思想

funciton ftpfile()
{
  # without any dir para
  if [ -z "$1" ]; then
    srcDir=$(pwd)
  else
    srcDir=$1
  fi
  
  # begin infer the path to file 
  if [ -d $srcDir/childDir ]; then
      srcFileDir=$srcDir/childDir1/childDir11
  fi

  if [ -d $srcDir/childDir1 ]; then
      srcFileDir=$srcDir/childDir11
  fi

  # the srcFile must be in the srcFileDir
  # find the target file if with glob variant using 'ls' child command
  srcFile=$(ls $srcFileDir/someFile*)
  dstDir="$(date +%Y%m%d_%H_%M_%S)"

  ftp -ivn 192.16.8.8 << EOF
      user ftpUser plainPassword
      bin
      cd    /someDstDir
      mkdir $dstDir
      cd    $dstDir
      put   $srcFile ${srcFile##/*/}
      bye
EOF

}

安全模式

借助ssh-keygenssh-copy-id免密配置,将登录用户的可信任公钥拷贝到ssh服务器,以实现ssh免密登录、scp免密拷贝,以及’ssh destination command '执行脚本等。

sftp因为没有尝试过,但是应该也可以免密、免交互

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值