Method: Puppet::Util::Firewall#string_to_port

Defined in:
lib/puppet/util/firewall.rb

#string_to_port(value, proto) ⇒ Object

This method takes a string and a protocol and attempts to convert it to a port number if valid.

If the string already contains a port number or perhaps a range of ports in the format 22:1000 for example, it simply returns the string and does nothing.



78
79
80
81
82
83
84
85
86
87
# File 'lib/puppet/util/firewall.rb', line 78

def string_to_port(value, proto)
  proto = proto.to_s
  unless %r{^(tcp|udp)$}.match?(proto)
    proto = 'tcp'
  end

  m = value.to_s.match(%r{^(!\s+)?(\S+)})
  return "#{m[1]}#{m[2]}" if %r{^\d+(-\d+)?$}.match?(m[2])
  "#{m[1]}#{Socket.getservbyname(m[2], proto)}"
end