diff options
author | Kevin Menard <kevin@nirvdrum.com> | 2014-05-01 21:05:37 -0400 |
---|---|---|
committer | Kevin Menard <kevin@nirvdrum.com> | 2014-05-01 21:05:37 -0400 |
commit | a2d7bce1bbf4984dcea41714f65fbf12f8ea5c9c (patch) | |
tree | 15b65f591e8fd1b93c5d04e828c772429b325c69 | |
parent | dc6704d050550c3fe63f8410e194781c47fabea2 (diff) | |
download | net-ssh-a2d7bce1bbf4984dcea41714f65fbf12f8ea5c9c.tar.gz |
Support negative patterns in host lookup from the SSH config file.
Fixes #150: ssh_config parsing doesn't support negated host patterns
-rw-r--r-- | lib/net/ssh/config.rb | 14 | ||||
-rw-r--r-- | test/configs/negative_match | 6 | ||||
-rw-r--r-- | test/test_config.rb | 6 |
3 files changed, 24 insertions, 2 deletions
diff --git a/lib/net/ssh/config.rb b/lib/net/ssh/config.rb index fb605b2..137b8e3 100644 --- a/lib/net/ssh/config.rb +++ b/lib/net/ssh/config.rb @@ -103,8 +103,18 @@ module Net; module SSH if key == 'host' # Support "Host host1 host2 hostN". # See http://github.com/net-ssh/net-ssh/issues#issue/6 - multi_host = value.to_s.split(/\s+/) - matched_host = multi_host.select { |h| host =~ pattern2regex(h) }.first + negative_hosts, positive_hosts = value.to_s.split(/\s+/).partition { |h| h.start_with?('!') } + + # Check for negative patterns first. If the host matches, that overrules any other positive match. + # The host substring code is used to strip out the starting "!" so the regexp will be correct. + negative_match = negative_hosts.select { |h| host =~ pattern2regex(h[1..-1]) }.first + + if negative_match + matched_host = nil + else + matched_host = positive_hosts.select { |h| host =~ pattern2regex(h) }.first + end + seen_host = true settings[key] = host elsif !seen_host diff --git a/test/configs/negative_match b/test/configs/negative_match new file mode 100644 index 0000000..7a2e5ec --- /dev/null +++ b/test/configs/negative_match @@ -0,0 +1,6 @@ +Host test.* !test.host + Port 1234 + Compression no + +Host test.host + Port 9876
\ No newline at end of file diff --git a/test/test_config.rb b/test/test_config.rb index 761299d..623281e 100644 --- a/test/test_config.rb +++ b/test/test_config.rb @@ -35,6 +35,12 @@ class TestConfig < Test::Unit::TestCase assert !config.key?('rekeylimit') end + def test_load_with_wild_card_and_negative_pattern_does_not_match + config = Net::SSH::Config.load(config(:negative_match), "test.host") + assert_equal 9876, config['port'] + assert !config.key?('compression') + end + def test_for_should_load_all_files_and_translate_to_net_ssh_options config = Net::SSH::Config.for("test.host", [config(:exact_match), config(:wild_cards)]) assert_equal 1234, config[:port] |