2014-09-13 20 views
1

我需要下载的文件结构(超过4GB)从FTPS服务器(超过TLS协议隐含的FTP)。不幸的是wget不支持FTPS,但卷曲does。但curl不支持递归。如何从递归获取文件和命令行FTPS服务器?

我需要在Ubuntu一个命令行工具解决方案。

任何想法?

+0

对不起,我忘了写,那我需要一个命令行工具。但看标题.-) – 2014-09-13 08:20:31

+0

'wget'和'curl'是程序员通常使用的**软件工具,所以它属于这里。见http://stackoverflow.com/help/on-topic – 2014-09-13 08:31:02

+0

你可以写一个简单的脚本它调用卷曲。 – jfly 2014-09-13 12:53:33

回答

0

没有找到解决办法,所以我写了一个简单的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:[email protected]:MyPort '/Directory to copy'