Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, I have written below mentioned script in perl for monitoring a service on the server. I need to monitor multiple services likewise. But the values assined to variable status ...
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    23

    Exclamation perl script misbehaving

    Hi,

    I have written below mentioned script in perl for monitoring a service on the server. I need to monitor multiple services likewise. But the values assined to variable status is 0 instead of the output of the command. Therefore the if condition after that also goes wrong

    ---------------------------------------------------------------------------------------------------------------

    #!/usr/bin/perl

    my $status=system "nmap 192.168.32.240 -p 80 | grep 80 | cut -d \' \' -f2";
    print "\n$status\n";
    if ( $status eq "open" )
    {
    print "http on R240 is open\n";
    }
    else
    {
    print "http on R240 is closed\n";
    }

    --------------------------------------------------------------------------------------------------------------------------

    Kindly let me know what cud the issue. Please help
    Last edited by me_spearhead; 06-26-2007 at 11:36 AM. Reason: different script

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Yup. system executes a command and returns the command's exit value.

    What you want to do is use backquotes instead. This means "Execute this external command, and return its STDOUT":
    Code:
    #!/usr/bin/perl
    
    my $status=`nmap 192.168.32.240 -p 80 | grep 80 | cut -d \' \' -f2`;
    print "\n$status\n";
    if ( $status eq "open" )
    {
        print "http on R240 is open\n";
    }
    else
    {
        print "http on R240 is closed\n";
    }
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...