How to handle ruby ​​url to retrieve components (scheme, username, password, host, etc.)?

I am trying to create a program using ruby ​​(and Net :: SSH) to connect to servers and perform some tasks. Server details should be provided as something like:

ssh://user:pass@host:port (for a host that does not yet have SSH keys)

or

user@host

Net :: SSH expects the following format:

Net::SSH.start('host', 'user', :password => "password")

Is there any gem / stdlib that can handle the url in this format? Or a simple regular expression that can match different parts?

Note. I know and use capistrano, but in this case I need a lower level of control.

+3
source share
2 answers

URI Addressable:: URI URL- .

URI Ruby, , Addressable:: URI , , , URL-.

require 'addressable/uri'

uri = Addressable::URI.parse('ssh://user:pass@www.example.com:81') 
uri.host # => "www.example.com"
uri.user # => "user"
uri.password # => "pass"
uri.scheme # => "ssh"
uri.port # => 81

require 'uri'
uri = URI.parse('ssh://user:pass@www.example.com:81')
uri.host # => "www.example.com"
uri.user # => "user"
uri.password # => "pass"
uri.scheme # => "ssh"
uri.port # => 81
+11

URI, . , ssh http , , URI ssh .

0

Source: https://habr.com/ru/post/1776828/


All Articles