Time out set in perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Time out set in perl script
# 1  
Old 09-11-2015
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

Code:
 
 #!/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.
# 2  
Old 09-11-2015
shell script

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.

Code:
#!/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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl:Script to append date and time stamp

Help with Perl script : I have a web.xml file with a line <display-name>some_text_here</display-name> Need to append the current date and time stamp to the string and save the XML file Something like <display-name>some_text_here._01_23_2014_03_56_33</display-name> -->Finally want... (5 Replies)
Discussion started by: gaurav99
5 Replies

2. Shell Programming and Scripting

Get the time difference between two consecutive line in UNIX perl script

Hi All :o, I have some log files which contains these informations: 2013-04-24 09:11:34.018 INFO XXXXXXXXXXXX 2013-04-24 09:11:34.029 INFO YYYYYYYYYYYY 2013-04-24 09:11:34.039 INFO ZZZZZZZZZZZZZZZ 2013-04-24 09:12:21.295 INFO TTTTTTTTTTTTTTT 2013-04-24 09:12:21.489 INFO... (3 Replies)
Discussion started by: shariquehabib
3 Replies

3. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

4. Shell Programming and Scripting

How do i access sybase using isql and how do i get result set in perl script?

Hi, How do i get result set in perl script using isql while connecting sybase server. I have a perl script which connected to sybase and get the result set. but i wanted to get the result set. How do i get the result set in perl script not unix shell script.. $server ="ServerName"; open... (1 Reply)
Discussion started by: solo123
1 Replies

5. Shell Programming and Scripting

need shell or Perl script to get the epoch time automatically

I need shell or Perl script to get the epoch time automatically Example I need to execute mysql command to delete content less then given epoch time If date is 01-07-2010 (dd-mm-yy) epoch should be lees 7 days mean 23-06-2010 for 23-06-2010 I need epoch time Delete BS_table where... (1 Reply)
Discussion started by: sreedhargouda
1 Replies

6. Shell Programming and Scripting

Perl script that checks against Future time

Ok, so this may be an unusual request. But I have a certificate that expires sometime in may of 2011. Now, i have to monitor this certificate and alert when the current time is within 30 days of may 20, 2011. #!/usr/bin/perl # use Time::Local; # $sec=59; $min=59; $hours=23; $day=31;... (3 Replies)
Discussion started by: SkySmart
3 Replies

7. Shell Programming and Scripting

Script to capture date/time in seconds in PERL... Cant understand errors

I'm Using this script to find the time of a file. I'm very much new to PERL and found this script posted by some one on this forum. It runs perfectly fine, just that it gives me following errors with the accurate output as well. I jus want the output to be stored in another file so that i can... (0 Replies)
Discussion started by: bankimmehta
0 Replies

8. Programming

Date time problem while executing perl script.

Hi All, This Monday 15th March 2010, i have faced a weired issue with my Perl script execution, this script is scheduled to run at 1 minute past midnight on daily basis ( 00:01 EST ) generally for fetching previous business date , say if it is Monday it should give last Friday date, for Tuesday... (0 Replies)
Discussion started by: ravimishra
0 Replies

9. Shell Programming and Scripting

time stamp perl script error out of range 1..31

Hi, while running the perl script i am getting this error message , Day '' out of range 1..31 at rsty.sh line 44 what do iam missing in the script, any suggestion #!/usr/bin/perl use Time::Local; my $wday = $ARGV; my $month = $ARGV; # convert the month shortname into 0-11 number if... (4 Replies)
Discussion started by: saha
4 Replies

10. Shell Programming and Scripting

I need to set a time limit for a script

Hello Folks, I have been asked to write a test script which can be run by students. the script should have a time limit. I have almost completed it except the bit of timing! I've seen something like this: on_timeout() { echo "$USER $score " >> theresult.txt echo "Time out!... (2 Replies)
Discussion started by: SultanKSA
2 Replies
Login or Register to Ask a Question