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

Hi All,

I have this script:
#!/usr/bin/perl use IO::Socket::INET; $ns1dns = IO::Socket::INET->new( PeerAddr => 'xxx.xxx.xxx.xxx', PeerPort => 53, Proto => 'tcp'); $ns2dns = IO::Socket::INET->new( PeerAddr => 'xxx.xxx.xxx.xxx', PeerPort => 53, Proto => 'tcp'); $ns3dns = IO::Socket::INET->new( PeerAddr => 'xxx.xxx.xxx.xxx', PeerPort => 53, Proto => 'tcp'); $ns4dns = IO::Socket::INET->new( PeerAddr => 'xxx.xxx.xxx.xxx', PeerPort => 53, Proto => 'tcp'); $mx1pop3 = IO::Socket::INET->new( PeerAddr => 'xxx.xxx.xxx.xxx', PeerPort => 995, Proto => 'tcp'); $mx1imap = IO::Socket::INET->new( PeerAddr => 'xxx.xxx.xxx.xxx', PeerPort => 993, Proto => 'tcp'); $mx1smtp = IO::Socket::INET->new( PeerAddr => 'xxx.xxx.xxx.xxx', PeerPort => 25, Proto => 'tcp'); $mx1web = IO::Socket::INET->new( PeerAddr => 'xxx.xxx.xxx.xxx', PeerPort => 80, Proto => 'tcp'); print "Content-type: text/html\n\n"; # Web Services if ($mx1web) { print "<IMG SRC='http://www.xxx.com/images/up.gif' WIDTH=38 HEIGHT=2 +1>"; } else { print "<IMG SRC='http://www.xxx.com/images/down.gif' WIDTH=38 HEIGHT +=21>"; } print "<br>"; # DNS Services if ($ns1dns|$ns2dns|ns3dns|ns4dns) { print "<IMG SRC='http://www.xxx.com/images/up.gif' WIDTH=38 HEIGHT=2 +1>"; } else { print "<IMG SRC='http://www.xxx.com/images/down.gif' WIDTH=38 HEIGHT +=21>"; } print "<br>"; #Mail Services if ($mx1pop3|$mx1imap|mx1smtp) { print "<IMG SRC='http://www.xxx.com/images/up.gif' WIDTH=38 HEIGHT=2 +1>"; } else { print "<IMG SRC='http://www.xxx.com/images/down.gif' WIDTH=38 HEIGHT +=21>"; } print "<br>";
It is supposed to display the down gif when any one of the variables in brackets i.e ($mx1pop|$mx1imap) is not responding, but if i take one of the servers down, it waits for ages before still displaying the up gif.

Why does it show the up.gif file even when a server is down, and also is there anyway to set a maximum timeout for the servers, as if i take one down it can take up to 30 seconds before it returns the results.

Thanks

Replies are listed 'Best First'.
Re: Whats wrong with this? & Timeouts
by Ovid (Cardinal) on Sep 09, 2002 at 22:04 UTC

    While there are a variety of responses that could be made, I'll stick with the immediately obvious one: you didn't use strict. If you had, you would have caught an obvious error of forgetting the sigil ($):

    # wrong #if ($ns1dns|$ns2dns|ns3dns|ns4dns) { if ($ns1dns || $ns2dns || $ns3dns || $ns4dns) {

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      ahh cheers, how about the timeout when using IO::Socket::INET?
Re: Whats wrong with this? & Timeouts
by sauoq (Abbot) on Sep 09, 2002 at 22:26 UTC
    if ($mx1pop3|$mx1imap|mx1smtp) { print "<IMG SRC='http://www.xxx.com/images/up.gif' WIDTH=38 HEIGHT=2 +1>"; } else { print "<IMG SRC='http://www.xxx.com/images/down.gif' WIDTH=38 HEIGHT +=21>"; }
    It is supposed to display the down gif when any one of the variables in brackets i.e ($mx1pop|$mx1imap) is not responding, but if i take one of the servers down, it waits for ages before still displaying the up gif.

    In addition to the error that Ovid pointed out, you have a logic error as well. Since you want down.gif to be displayed if any of the servers is down, you want the up.gif to be displayed only when all of them are up. In other words, you should be using logical and, rather than logical or, in your if statements.

    if ($mxlpop3 && $mxlimap && $mxlsmtp) { # Display up.gif } else { # Display down.gif }
    -sauoq
    "My two cents aren't worth a dime.";
    
      Thanks. i noticed the logic error just after posting that! D'oh. I'm still stuck on the timeouts tho....

        Specify appropriate Timeout arguments in your calls to the IO::Socket::INET constructor.

        -sauoq
        "My two cents aren't worth a dime.";