#!/usr/bin/perl use Socket; sub addr_is_private ( $ ) { my $addr = shift; ($addr & "\xff\0\0\0") eq pack "CCCC", 10, 0, 0, 0 or ($addr & "\xff\xf0\0\0") eq pack "CCCC", 172, 16, 0, 0 or ($addr & "\xff\xff\0\0") eq pack "CCCC", 192, 168, 0, 0; } sub host_exists_and_is_not_private ( $ ) { my $host = shift; my $addr = inet_aton($host); defined $addr and not addr_is_private($addr); } while (<>) { my @hosts = (); my $nonpriv = 0; while (s/^([^,\s]+),?//) { my $host = $1; push @hosts, $host if $host =~ /^[-\w]+$/ or host_exists_and_is_not_private($host) and $nonpriv++, 1; } print join(",", @hosts) . $_ if $nonpriv > 0; }