File: services.pl

package info (click to toggle)
libparse-dmidecode-perl 0.03-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 336 kB
  • sloc: perl: 1,150; makefile: 4
file content (59 lines) | stat: -rw-r--r-- 1,364 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/perl -wT

use strict;
use DBI qw();

my %service;
open(SER,'<','/etc/services') || die "Unable to open file '/etc/services': $!";
while (local $_ = <SER>) {
	next if /^\s*#/ || /^\s*$/;
	my $desc;
	if (/#\s*(.+)\s*$/) {
		$desc = $1;
		s/#.*$//;
	}
	my ($service,$port,@aliases) = split(/\s+/,$_);
	$service{$port} = {
			name => $service,
			aliases => \@aliases,
			desc => $desc,
		};
}
close(SER) || die "Unable to close file '/etc/services': $!";

my $dbh = DBI->connect('DBI:mysql:machinedb:localhost','machinedb_update','a8fc0ebf41ed43eb2686fcec2291c071');
my $sth = $dbh->prepare(qq{
	SELECT hostname,data
		FROM probe
		NATURAL JOIN machine
		NATURAL JOIN host
		WHERE probe = ? ORDER BY hostname
	});

$sth->execute('netstat');
my @cols = qw(proto recvq sendq local foreign state process);

while (my ($hostname,$data) = $sth->fetchrow_array) {
	print "$hostname\n";
	for (split(/\n/,$data)) {
		next unless /[:\*\d]/;
		my %data;
		@data{@cols} = split(/\s+/,$_);
		if ($data{proto} =~ /^tcp/ && $data{state} eq 'LISTEN') {
			($data{port}) = $data{local} =~ /:([\d\*]+)$/;
			my $desc = '';
			if (exists $service{"$data{port}/tcp"}) {
				$desc = $service{"$data{port}/tcp"}->{desc} ||
					$service{"$data{port}/tcp"}->{name} || '';
			}
			printf("\t%s\t%s\n",$data{port},$desc);
		}
	}
	print "\n";
}

$sth->finish;
$dbh->disconnect;