#!/usr/bin/perl -w use strict; $date = `date +"%l:%M:%S"`; # I'd use sprintf and localtime my %service= ( www => qr/:0050\s/, ssh => qr/:0016\s/, irc => qr/:1A0B\s/, ftp => qr/:0015\s/, ); my %count= qw( www 0 ssh 0 irc 0 ftp 0 ); foreach my $line ( grep /\b01\s/, `cat /proc/net/tcp` ) { foreach my $svc ( keys %service ) { $count{$svc}++ if $line =~ $service{$svc}; } } open (FILE, "+>/var/www/web/main.txt") or die "Can't write to /var/www/web/main.txt: $!\n"; print FILE "There are $count{www} web, $count{ftp} ftp, ", "$count{ssh} ssh, and $count{irc} irc connections ", "to this server as of $date\n"; close FILE;