diff --git a/lib/sshkit/host.rb b/lib/sshkit/host.rb index fc33be20..d52a5f47 100644 --- a/lib/sshkit/host.rb +++ b/lib/sshkit/host.rb @@ -1,4 +1,5 @@ require 'ostruct' +require 'resolv' module SSHKit @@ -122,6 +123,22 @@ def attributes end + # @private + # :nodoc: + class IPv6HostParser < SimpleHostParser + def self.suitable?(host_string) + host_string.match(Resolv::IPv6::Regex) + end + + def port + + end + + def hostname + @host_string.match(Resolv::IPv6::Regex)[0] + end + end + class HostWithPortParser < SimpleHostParser def self.suitable?(host_string) @@ -192,6 +209,7 @@ def hostname PARSERS = [ SimpleHostParser, + IPv6HostParser, HostWithPortParser, HostWithUsernameAndPortParser, IPv6HostWithPortParser, diff --git a/test/unit/test_host.rb b/test/unit/test_host.rb index 8ce489fa..17d5044f 100644 --- a/test/unit/test_host.rb +++ b/test/unit/test_host.rb @@ -28,6 +28,12 @@ def test_host_with_port assert_equal 'example.com', h.hostname end + def test_custom_host_with_port + h = Host.new 'db:22' + assert_equal 22, h.port + assert_equal 'db', h.hostname + end + def test_host_with_username h = Host.new 'root@example.com' assert_equal 'root', h.username @@ -50,6 +56,12 @@ def test_host_local assert_equal 'localhost', h.hostname end + def test_ipv6_without_brackets + h = Host.new '1fff:0:a88:85a3::ac1f' + assert_nil h.port + assert_equal '1fff:0:a88:85a3::ac1f', h.hostname + end + def test_does_not_confuse_ipv6_hosts_with_port_specification h = Host.new '[1fff:0:a88:85a3::ac1f]:8001' assert_equal 8001, h.port