简体   繁体   English

使用authorized_keys与没有用户名的Ruby进行SSH连接

[英]SSH connection with Ruby without username using `authorized_keys`

I have authenticated a server using authorized_keys push so I could run command ssh 192.168.1.101 from my system and could connect via server. 我已经使用authorized_keys push对服务器进行了身份验证,因此可以从系统中运行命令ssh 192.168.1.101并可以通过服务器进行连接。

Now, I tried with library , It didn't worked for me 现在,我尝试使用Library,但它对我没有用

  Net::SSH.start("192.168.1.209",username) do |ssh|   @output=ssh.exec!("ls -l")  end

as, This required username field. as,此必填的用户名字段。 I want without username. 我想要没有用户名。

So , I tried this 所以,我尝试了

    system('ssh 192.168.1.209 "ls -l"') 

It run the command for me. 它为我运行命令。 But I want the output in a variable like @output in first example. 但在第一个示例中,我希望输出使用@output类的变量。 Is there any command any gem or any way by which I could get the solution ? 是否有任何命令或任何方法可以获取解决方案?

Any ssh connection requires a username. 任何ssh连接都需要用户名。 The default is either your system account name or whatever's specified in .ssh/config for that host you're connecting to. 默认值是您的系统帐户名,或者是.ssh/config为您要连接的主机指定的名称。

Your current username should be set as ENV['USER'] if you need to access that. 如果您需要访问当前用户名,则应将其设置为ENV['USER']

If you're curious what username is being used for that connection, try finding out with ssh -v which is the verbose mode that explains what's going on. 如果您想知道该连接使用了什么用户名,请尝试使用ssh -v进行查找,这是详细信息,它说明了发生了什么。

Backquotes works very similar to "system" function but with important difference. 反引号的工作原理与“系统”功能非常相似,但有重要区别。 Shell command enclosed between the backquotes is executed with standard output as result. 反引号之间包含的Shell命令以标准输出作为结果执行。

So, following statement should execute ssh 192.168.1.209 "ls -l" and puts directory files listing into @output variable: 因此,以下语句应执行ssh 192.168.1.209“ ls -l”并将目录文件列出到@output变量中:

@output = `ssh 192.168.1.209 "ls -l"`

you can pass parameters into %x[] as follows: 您可以按如下方式将参数传递到%x []中:

1. dom = ‘www.ruby-rails.in‘
2. @whois = %x[whois #\{dom\}]

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

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