Skip to content

Commit

Permalink
Correctly detect UDP ports on linux
Browse files Browse the repository at this point in the history
netstat on UDP lines doesn't display a port state (e.g. LISTEN), so make this
field optional when parsing the netstat line.
  • Loading branch information
mivok authored and chris-rock committed Jan 15, 2016
1 parent c0ba2c6 commit f5780b6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/resources/port.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def parse_net_address(net_addr, protocol)
def parse_netstat_line(line)
# parse each line
# 1 - Proto, 2 - Recv-Q, 3 - Send-Q, 4 - Local Address, 5 - Foreign Address, 6 - State, 7 - Inode, 8 - PID/Program name
parsed = /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/.match(line)
parsed = /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)?\s+(\S+)\s+(\S+)\s+(\S+)/.match(line)

return {} if parsed.nil? || line.match(/^proto/i)

Expand Down
1 change: 1 addition & 0 deletions test/unit/mock/cmd/netstat-tulpen
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 0 21699 1/sshd
tcp6 0 48 2601:1:ad80:1445::54776 2620:0:861:52:208::6667 LISTEN 0 21702 2043/pidgin
tcp6 0 0 :::22 :::* LISTEN 0 21701 1/sshd
udp 0 0 0.0.0.0:111 0.0.0.0:* 0 8689 545/rpcbind
8 changes: 8 additions & 0 deletions test/unit/resources/port_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
_(resource.processes).must_equal ['sshd']
end

it 'verify UDP port on Ubuntu 14.04' do
resource = MockLoader.new(:ubuntu1404).load_resource('port', 111)
_(resource.listening?).must_equal true
_(resource.protocols).must_equal %w{ udp }
_(resource.pids).must_equal [545]
_(resource.processes).must_equal ['rpcbind']
end

it 'verify port on MacOs x' do
resource = MockLoader.new(:osx104).load_resource('port', 2022)
_(resource.listening?).must_equal true
Expand Down

0 comments on commit f5780b6

Please sign in to comment.