D
D
Dmitry Sergeev2012-09-06 10:49:28
Perl
Dmitry Sergeev, 2012-09-06 10:49:28

How to handle system result in perl script?

The perl script has a function call
system("/usr/sbin/ipset -A $table_name $ip");
sometimes it returns
ipset v4.5: 194.186.2.46 is already in set autoban_31.

in this case I need to execute

system("/usr/sbin/ipset -A $higher_table_name $ip");

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
PooFF, 2012-09-06
@PooFF

out = `/usr/sbin/ipset -A $table_name $ip`

P
PooFF, 2012-09-06
@PooFF

The parser failed

@out=`/usr/sbin/ipset -A $table_name $ip`

K
Konstantin Vlasov, 2012-09-06
@CaptainFlint

The return value of system is the error code shifted 8 bits to the left. If ipset in the described situation returns some specific code, you can pull it out and process it. If not, then just parse the output string, replacing system with backticks, as PooFF suggested above .

I
Ilya_Drey, 2012-09-06
@Ilya_Drey

In addition to what PooFF wrote, I can recommend that you check the return code, in case of an error while executing the script that you do not know (do not know what to parse in the output) ...
Example:

my $command = "/usr/sbin/ipset -A $table_name $ip";
my @result =`$command`

foreach my $line (@result)
{   
  if ($line =~ /already\sin\sset\sautoban_31/) {
    system("/usr/sbin/ipset -A $higher_table_name $ip");
  }

  die("Unknown error. Command  ".$command." return code: ".$?."") if $? -ne 0 ;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question