MulletRay has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,
I am calling on your wisdom in search for some advice. I have this code that gets names from a mysql database and uses the name to get a mac address via rsh. The server executing this script is a linux machine. the hosts i am connecting to are NT/2k machines. Some of the machines on my list are behind seperate firewalls. So when my script hits a machine like this, it hangs waiting for a response.
Is there a way to set a default timeout value, like say 20 secs, to wait for a response before it goes on to the next machine. Any advice would be greatly appreciated. Here is my code:
#!/usr/bin/perl -w ################################################################# # Ray Espinoza # # 12-31-02 # # GetMacsFromDB.pl - this script will get hostnames from # # our inventory database and query for # # the mac address and print it to a file. # ################################################################# use strict; use DBI; ###################################################### # create a database handle and defining my variables # ###################################################### my (@array_of_lines, $string, $mac); my $dbh = &connect; ########################################## # opening a pipe to my file to print out # ########################################## open (OUT,">>/home/respinoz/workbench/macs/MacResults.log") or die "Ca +n't open f ile: $!\n"; ########################################## # Calling the subroutine to do its work # ########################################## get_hostname_and_get_mac ($dbh); ############################################################## # All of the subs are named appropriately to their function # ############################################################## sub get_hostname_and_get_mac { my $dbh = shift; while (my $row = $sth->fetchrow_hashref()) { $hostname = $row->{name}; $mac = get_mac($hostname); print OUT "$hostname\t$mac\n"; } $sth->finish(); } ########### sub get_mac { my $hostname = shift; my ($string, $mac); $string = `/usr/bin/rsh $hostname nbtstat -a $hostname`; if ($string =~ /s*(([0-9A-Fa-f]{2}[-:]){5}[0-9A-Fa-f]{2})/) { $mac = $1; return $mac; } } ########### sub connect { my ($dbh, $sth, $count); $dbh= DBI->connect("DBI:mysql:host=localhost;database=testsites","xxxx +x","xxxxx" ,{PrintError => 0, RaiseError => 1}); return $dbh; }
thanks, Ray

edited: Thu Jan 2 05:06:23 2003 by jeffa - added <readmore>

Replies are listed 'Best First'.
Re: can i set a timeout
by pg (Canon) on Jan 01, 2003 at 20:17 UTC
    Some versions of rsh support -t option, which allows you to set timeout. Try it and see whether your version support it.

    In case your rsh does not support -t:

    I am not sure whether alarm is an option for you, as it is not supported on all platforms, even in perl 5.8.0. An alternative way is, before you rsh, use some other command with timeout to determine whether the remote host is reachable, such command include nmap, ping etc.
Re: can i set a timeout
by Juerd (Abbot) on Jan 01, 2003 at 19:55 UTC

    Is there a way to set a default timeout value, like say 20 secs, to wait for a response before it goes on to the next machine.

    Found in perlfaq8:

    How do I timeout a slow event?
    Use the alarm() function, probably in conjunction with a signal handler, as documented in "Signals" in perlipc and the section on "Signals" in the Camel. You may instead use the more flexible Sys::AlarmCall module available from CPAN.

    MulletRay--.
    Read FAQs *before* asking a question. Try Google, search.cpan.org and Super Search if the FAQs don't answer your question. Only if you are unable to find the answer yourself, ask a question.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.