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

All, I am having problem with the following snip of code. The synthax looks ok to me and to "perl -c". But when I run it, I am getting the following error: iptables v1.2.3: host/network `-s 10.10.10.10 -j DROP' not found Try `iptables -h' or 'iptables --help' for more information. Does anyone have any idea why? Here's a snip of the code:
#!/usr/bin/perl my $CMD = "/sbin/iptables"; my $arg1 = "-A INPUT -p tcp -m multiport \--dport 80,8080"; my $arg2 = "-s 10.10.10.10 -j DROP"; system("$CMD", "$arg1", "$arg2") == 0 or die "can't fork: $!\n";

Replies are listed 'Best First'.
Re: problem with system function
by Kanji (Parson) on Dec 05, 2002 at 19:44 UTC

    The way that you're calling system() bypasses the shell, so you're giving $CMD just 2 arguments (neither of which are valid switches for iptables) instead of the 12 you would be passing if you'd run that from the command line.

    Instead, you want to force pre-parsing by the shell...

    system("$CMD $arg1 $arg2");

    ...or emulate that behaviour yourself by making $arg1 and $arg2 arrays instead...

    my $CMD = "/sbin/iptables"; my @arg1 = qw( -A INPUT -p tcp -m multiport --dport 80,8080 ); my @arg2 = qw( -s 10.10.10.10 -j DROP ); system($CMD, @arg1, @arg2) == 0 or die "can't fork: $!\n";

        --k.


Re: problem with system function
by pfaut (Priest) on Dec 05, 2002 at 19:45 UTC

    perldoc -f system

    You probably don't want to pass the arguments as two separate strings. The system() function will treat each argument as a separate argument to the executed program. If you are breaking it up to make your code more readable, try:

    system("$CMD $arg1 $arg2");
Re: problem with system function
by BrowserUk (Patriarch) on Dec 05, 2002 at 19:43 UTC

    Your error is in the syntax of parameters you are supplying to the iptables command rather than Perl. Have you tried that command from the command line?


    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.