Downloading files using wildcards with scp in Ruby.

huangapple go评论67阅读模式
英文:

Downloading files using wildcards with scp in Ruby

问题

以下是翻译好的部分:

我成功地使用下面的代码从服务器下载了“log_list”中的日志文件。我使用了net/ssh和net/scp:(https://github.com/net-ssh/net-scp)

log_list = ["ini.log", "syslog.log", "network.log"]
fetch(log_list)

def fetch(fetchlogs)
  root_cli do |ssh|
    fetchlogs.each do |testlog|
      source = "/var/log/logfiles/#{testlog}"
      destination = "/home/Documents/testdir/"
      ssh.scp.download!(source, destination)
    rescue Exception => e
      info "#{fetch} is not a valid file ==++==++"
    end
  end
end

现在“log_list”中的日志文件是旋转日志,并且可以有多个文件,如“ini.log,ini.log.001,ini.log.002,...”,我想下载所有文件,我尝试使用*:

log_list = ["ini.log*", "syslog.log*", "network.log*"]

通常我认为这应该起作用,但结果是没有文件,并且出现异常:

ini.log* is not a valid file ==++==++
syslog.log* is not a valid file ==++==++
network.log* is not a valid file ==++==++

我还尝试直接在源文件中添加*,而不是在log_list中:

source = "/var/log/logfiles/#{testlog}*"
pp " source #{source}"
ssh.scp.download!(source, "/home/Documents/testdir/")

输出:

source ini.log*
source syslog.log*
source network.log*

但文件没有被下载,出现相同的错误:

ini.log* is not a valid file ==++==++
syslog.log* is not a valid file ==++==++
network.log* is not a valid file ==++==++

尽管文件存在,有人知道吗,*字符是否被转义了?

英文:

I am successfully downloading the log files in "log_list" from the server to destination using the code below.
I am using net/ssh / net/scp : (https://github.com/net-ssh/net-scp)

log_list = ["ini.log", "syslog.log", "network.log"]
fetch(log_list)


    def fetch(fetchlogs)
        root_cli do |ssh|
    
          fetchlogs.each do |testlog|
              source= "/var/log/logfiles/#{testlog}"
              destination = "/home/Documents/testdir/"
              
              ssh.scp.download!(source,destination)
    
            rescue Exception => e
              info "#{fetch} is not a valid file ==++==++ "
            end
        end
      end

Now the log files in "log_list" are rotating logs and can have multiple files like "ini.log ,ini.log.001, ini.log.002, ..." and i want to download all what i tried is with *:

log_list = ["ini.log*", "syslog.log*", "network.log*"]

which normally i thought should work but this is resulting in no file with exception.

ini.log* is not a valid file ==++==++
syslog.log* is not a valid file ==++==++
network.log* is not a valid file ==++==++

I also tried with adding * directly to source instead in log_list

source = "/var/log/logfiles/#{testlog}*"
pp " source #{source}"

ssh.scp.download!(source,"/home/Documents/testdir/")

output:

source ini.log*
source syslog.log*
source network.log*

but files are not being downloaded with same error

ini.log* is not a valid file ==++==++
syslog.log* is not a valid file ==++==++
network.log* is not a valid file ==++==++

Although files exits, Does somebody have any idea, is * character is being escaped ? !!

答案1

得分: 1

你可以使用scp.session.exec!ls来根据通配符选择收集文件列表,然后逐个下载每个源文件。

大致如下:

log_list = ["ini.log*", "syslog.log*", "network.log*"]

...
fetchlogs.each do |testlog|
  source = "/var/log/logfiles/#{testlog}"
  destination = "/home/Documents/testdir/"

  # 使用ls获取通配符文件列表
  source_files = ssh.scp.session.exec!("ls #{source}").split

  source_files.each { |file|         
    ssh.scp.download!(file, destination)
  }

  ...
end

希望这有帮助!

英文:

You can use scp.session.exec! with ls to gather a list of files based on your wildcard choice, and then download each source file one by one.

Something along these lines:

log_list = ["ini.log*", "syslog.log*", "network.log*"]

...
fetchlogs.each do |testlog|
  source = "/var/log/logfiles/#{testlog}"
  destination = "/home/Documents/testdir/"
  
  # Fetch list of wildcard files using ls
  source_files = ssh.scp.session.exec!("ls #{source}").split
  
  source_files.each { |file|         
    ssh.scp.download!(file, destination)
  }

  ...
end

huangapple
  • 本文由 发表于 2023年4月1日 00:06:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900616.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定