Pregunta

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.

Now, I tried with library , It didn't worked for me

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

as, This required username field. 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. Is there any command any gem or any way by which I could get the solution ?

¿Fue útil?

Solución 2

you can pass parameters into %x[] as follows:

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

Otros consejos

Any ssh connection requires a username. The default is either your system account name or whatever's specified in .ssh/config for that host you're connecting to.

Your current username should be set as ENV['USER'] if you need to access that.

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.

Backquotes works very similar to "system" function but with important difference. Shell command enclosed between the backquotes is executed with standard output as result.

So, following statement should execute ssh 192.168.1.209 "ls -l" and puts directory files listing into @output variable:

@output = `ssh 192.168.1.209 "ls -l"`
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top