【问题标题】:How to get files recursively from and FTPS server with command line?如何使用命令行从 FTPS 服务器递归获取文件?
【发布时间】:2014-09-13 07:54:47
【问题描述】:

我需要下载文件结构(超过 4GB)从 FTPS 服务器(基于 TLS 协议的隐式 FTP)。不幸的是 wget 不支持 FTPS,但 curl does。但是curl 不支持递归。

我需要 Ubuntu 上的命令行工具解决方案。

有什么想法吗?

【问题讨论】:

  • 对不起,我忘了写,我需要一个命令行工具。但是看标题.-)
  • wgetcurl程序员常用的软件工具,所以属于这里。见stackoverflow.com/help/on-topic
  • 你可以编写一个调用 curl 的简单脚本。
  • 咖啡经常被程序员使用,但它不属于 StackOverflow。
  • wget 从 1.17 版(2015 年 11 月)开始支持 FTPS。

标签: ubuntu curl recursion wget ftps


【解决方案1】:

没有找到解决方案,所以我写了一个简单的 ruby​​ 脚本,可以作为大家的起点:

#!/usr/bin/env ruby
# encoding: utf-8

require 'optparse'

FILENAME_MATCH_REGEXP=/^[d\-rwx]+ [0-9]+ \w+ \w+[ ]+\d+[ ]+[a-zA-Z]+[ ]+\d+[ ]+\d+[ ]+(.*)$/

options = {}
opts = OptionParser.new do |opts|
  opts.banner = "Usage: curl_ftp_get_recursive.rb [options] url directory"

  opts.on("-v", "--[no-]verbose", "Run verbosely") do |o|
    options[:verbose] = o
  end

  options[:curl_options] = '-sS'
  opts.on("-c", "--curl-options OPTIONS", "Curl options") do |o|
    options[:curl_options] += " #{o}"
  end

  options[:output_dir] = '.'
  opts.on("-o", "--output DIR", "Output directory, default=current directory") do |o|
    options[:output_dir] = o
  end
end

begin
  opts.parse!
  raise "Invalid number of arguments" if ARGV.count != 2
rescue Exception => e
  p "#{$0}: #{e.message}"
  p opts
  exit 1
end

# Remove trailing '/' if any
url = ARGV[0].sub /\/*$/, ''
root_dir = ARGV[1].sub /\/*$/, ''
options[:output_dir] = options[:output_dir].sub /\/*$/, ''

def get_dir options, url, dir
  p "Reading directory '#{dir}'..." if options[:verbose]

  output = `/usr/bin/curl #{options[:curl_options]} "#{url}/#{dir}"/`
  exit 1 if $? != 0
  dir_list = output.split "\n"
  dir_list.each do |line|
    p "Processing line '#{line}'..." if options[:verbose]
    file_name = "#{dir}/#{line.match(FILENAME_MATCH_REGEXP)[1]}"
    local_dir = "#{options[:output_dir]}/#{dir}"
    if line.match /^d/
      get_dir options, url, file_name
    else
      p "Getting file '#{file_name}'..."
      `mkdir -p "#{local_dir}"`
      exit 1 if $? != 0
      `/usr/bin/curl -o "#{options[:output_dir]}/#{file_name}" #{options[:curl_options]} "#{url}/#{file_name}"`
      exit 1 if $? != 0
    end
  end
end

get_dir options, url, root_dir

使用示例:

./curl_ftp_get_recursive.rb -v -c --insecure ftps://MyUSerName:MayPassword@MyHost:MyPort '/Directory to copy'

【讨论】:

    猜你喜欢
    • 2015-06-11
    • 2010-11-15
    • 2016-04-21
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多