Ruby NET::包含通配符的SCP
我需要每天从我有SCP但没有SSH访问权限的客户端下载一个文件 文件名将始终为/outgoing/Extract/visit_u[date]-[timestamp].dat.gz' 例如,昨天的文件名为visions_20130604-090003.dat.gz 我不能相信时间戳永远是一样的,但日期应该永远是昨天的日期: 到目前为止,我的设置: 我的主目录包含名为downloads\u fullname和downloads\u wildcard的子目录 它还包含一个名为foo.rb的简单ruby脚本 foo.rb的内容如下`Ruby NET::包含通配符的SCP,ruby,scp,net-ssh,Ruby,Scp,Net Ssh,我需要每天从我有SCP但没有SSH访问权限的客户端下载一个文件 文件名将始终为/outgoing/Extract/visit_u[date]-[timestamp].dat.gz' 例如,昨天的文件名为visions_20130604-090003.dat.gz 我不能相信时间戳永远是一样的,但日期应该永远是昨天的日期: 到目前为止,我的设置: 我的主目录包含名为downloads\u fullname和downloads\u wildcard的子目录 它还包含一个名为foo.rb的简单ruby
#! /usr/bin/ruby
require 'net/ssh'
require 'net/scp'
yesterday = (Time.now - 86400).strftime('%Y%m%d')
Net::SCP.start('hostname', 'username') do |scp|
scp.download!('/outgoing/Extract/visits_' + yesterday + '-090003.dat.gz', 'downloads_fullname')
scp.download!('/outgoing/Extract/visits_' + yesterday + '-*.dat.gz', 'downloads_wildcard')
end
运行时,downloads\u fullname目录包含该文件,但downloads\u通配符目录不包含该文件
有没有办法在Net::SCP中使用通配符?或者有人有什么狡猾的变通办法吗?我尝试了\*但没有效果。我认为您无法使用scp到达目的地,因为它希望您确切地知道所需的文件,但sftp会让您获得目录列表 您可以使用以编程方式拾取文件并请求它。以下是示例代码: require 'net/sftp' Net::SFTP.start('host', 'username', :password => 'password') do |sftp| # upload a file or directory to the remote host sftp.upload!("/path/to/local", "/path/to/remote") # download a file or directory from the remote host sftp.download!("/path/to/remote", "/path/to/local") # grab data off the remote host directly to a buffer data = sftp.download!("/path/to/remote") # open and write to a pseudo-IO for a remote file sftp.file.open("/path/to/remote", "w") do |f| f.puts "Hello, world!\n" end # open and read from a pseudo-IO for a remote file sftp.file.open("/path/to/remote", "r") do |f| puts f.gets end # create a directory sftp.mkdir! "/path/to/directory" # list the entries in a directory sftp.dir.foreach("/path/to/directory") do |entry| puts entry.longname end end 基于此,您可以列出目录条目,然后使用find或select对返回的列表进行迭代,以查找具有当前日期的列表。将该文件名传递给sftp.download!下载到本地文件。谢谢Tin Man 对其他人来说,这是我在锡人的带领下完成的代码: 试图将其作为评论发布,但存在格式问题
#! /usr/bin/ruby
require 'net/sftp'
yesterday = (Time.now - 86400).strftime('%Y%m%d')
Net::SFTP.start('hostname', 'username') do |sftp|
sftp.dir.foreach("/outgoing/Extract") do |file|
if file.name.include? '_' + yesterday + '-'
sftp.download!('/outgoing/Extract/' + file.name, 'downloads/'+ file.name)
end
end
end