#!/usr/bin/perl use strict; use warnings; use IO::Socket::INET; my @host = qw[ 192.168.1.3 192.168.46.2 ]; my $port = 22; foreach (@host) { check($_, $port); } sub can_connect { my ($ip, $port) = @_; return ! !IO::Socket::INET->new( PeerAddr => $ip, PeerPort => $port, Proto => 'tcp', timeout => 10, ); } sub check { my ($ip, $port) = @_; my $can_connect = can_connect($ip,$port); print "$ip is "; print "NOT" unless ($can_connect); print " listening on port $port\n"; return $can_connect; } sub check_variation { my $can_connect = can_connect(@_); print "$ip is "; print "NOT" unless ($can_connect); print " listening on port $port\n"; return $can_connect; }