简体   繁体   English

在Ruby中通过SSH从远程位置读取本地文件

[英]Read Local file from Remote Location via SSH in Ruby

I want to access a local file which is located on a device via a remote server location via SSH. 我想通过SSH通过远程服务器位置访问位于设备上的本地文件。

The local file is in this directory 本地文件在此目录中

/Desktop/applications.csv

and the IP address of the machine it is located in is 192.168.1.1 and user is user1 并且其所在计算机的IP地址为192.168.1.1 ,用户为user1

How do i read it from a remote location using SSH in Ruby on Rails? 如何在Ruby on Rails中使用SSH从远程位置读取它?

I tried doing this but it fails 我尝试这样做,但失败了

hash = Digest::MD5.hexdigest(File.read("ssh  user@192.168.1.1 /Desktop/applications.csv"))

Not sure how to go about it. 不知道如何去做。 Any help appreciated. 任何帮助表示赞赏。

Have you looked into SSHKit ? 您是否研究过SSHKit It lets you run commands on remote machines, via ssh. 它使您可以通过ssh在远程计算机上运行命令。

As an example: 举个例子:

require 'sshkit'
require 'sshkit/dsl'
include SSHKit::DSL

on '192.168.1.1'
  within "/Desktop/" do
    with rails_env: :production do
      rake "read_applications"            
    end
  end
end

And the 'read_applications' rake task would do the File.read and whatever else was required. 而“ read_applications”耙任务将执行File.read以及其他所需的操作。

EDIT: 编辑:

Does it know what user you're authenticating with? 它知道您要与哪个用户进行身份验证吗? Might need to add the user like this: 可能需要这样添加用户:

on '192.168.1.1'
  within "/Desktop/" do
    as :user do
      # do something
    end
  end
end

And change :user to be your username. 并将:user更改为您的用户名。 There's loads more examples on the Github page. Github页面上有更多示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM