Time out set in perl script

Hi,

I am preparing a perl script, which will run some commads on remote Linux servers.

I have a file contains all the servers names one by one like below

vi servers.txt

srv1
srv2
srv3

Now, I need to prepare a perl script to ssh (or)rsh to each server in the above file and get the uptime.

but the issue is, if the server is pinging and not able to ssh or rsh, then the script is getting hung.

can anyone suggested me to fix this, without using any perl modules.

here is my script

 
 #!/usr/bin/perl 
 use strict;use warnings;
  
 open (DATA, "<servers.txt") or die "no file, $!";
@srv = <DATA>;
close (DATA);
  
 $cmd = 'uptime';

 foreach $i (@srv) {
        chomp $i;
         $up = qx/rsh $i '$cmd';
         print " $i $up";
          }
 exit(0);
 

here the problem is, if the server is pinging but not able to login, then the script is not moving to another server.

please suggest me.

The following is from a shell script that I have used some time ago.
It first does ping, then tries rsh connection, then runs rsh or ssh.
The main loop I have ported from your perl script.

#!/bin/sh
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin; export PATH

# prevent Solaris from running out of file descriptors
ulimit -n 1024

case `uname -s` in
SunOS)
  pingopt=""
  ;;
Linux)
  pingopt="-c 1 -w"
  ;;
HP-UX)
  pingopt="-n 1 -m"
  ;;
esac

if [ "$1" = "-bg" ]; then
  bg='&'
  shift
fi
command=$*
echo command $command
toggle=1

do_rsh(){
 # echo trying rsh first
 if perl -e "alarm 5;exec @ARGV" rsh -n "$1" ":"
 then
  : using rsh first
  rsh -n "$1" "$command"
  sleep $toggle </dev/null #otherwise Solaris may run out of sockets
 else
  : echo using ssh
  ssh -xn "$1" "$command"
 fi
}

# main loop
while read i
do
  if ping $i $pingopt 2  >/dev/null
  then
    : echo machine: $i
    eval do_rsh $i $bg
    toggle=`expr 1 - $toggle`
    sleep $toggle </dev/null #otherwise Solaris may run out of sockets
  else
    : echo $i is down
  fi
done < servers.txt

# in case rsh/ssh has corrupted the terminal:
[ -t 0 ] && stty sane