#!/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 check { my ($ip, $port) = @_; print ( my $sock = IO::Socket::INET->new( PeerAddr => $ip, PeerPort => $port, Proto => 'tcp', timeout => 10, ) ? "$ip is listening on port $port\n" : "$ip is NOT listening on port $port.\n" ) }