#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    5
    Rep Power
    0
    Hi,

    I'm trying to create a Perl script to convert a whole lot of modules used by RCF for use with iptables, instead of going by hand through each one and changing the ipchains commands to iptables commands.

    This is my first Perl experience, and have received some help along the way, but I am now stumped!

    Here is the script:

    #!/usr/bin/perl -w

    opendir(SCRIPTDIR, "/etc/firewall-modules/") or die "Can't open directory: $!";
    @scriptdir = grep !/^./, readdir SCRIPTDIR;
    close(SCRIPTDIR);

    foreach $value (@scriptdir) {
    print "$valuen";
    print "-" x 3, ">";
    print "n";
    opendir(SCRIPTDIR_1, "/etc/firewall-modules/$value") or die "Can't open directory: $!";
    @scriptdir1 = grep !/^./, readdir SCRIPTDIR_1;
    closedir SCRIPTDIR_1;
    foreach $value1 (@scriptdir1) {
    print "$value1n";
    print " ", "-" x 3, ">";
    print "n";
    opendir(SCRIPTDIR_2, "/etc/firewall-modules/$value/$value1") or die "Can't open directory: $!";
    @scriptdir2 = grep !/^./, readdir SCRIPTDIR_2;
    closedir SCRIPTDIR_2;
    foreach $value2 (@scriptdir2) {
    print "$value2n";
    my $script;
    $script = $value2 . ".tmp";
    open(INFILE,"/etc/firewall-modules/$value/$value1/$value2") or die "Can't open module: $!n";
    open(OUTFILE, ">/etc/firewall-modules/$value/$value1/$script") or die "Can't open temp file: $!n";
    if (<INFILE> =~ /^(d{3})-(w+)-(w+).tmp$/) {
    print "--> Already converted.n";
    }
    seek INFILE,0,0;
    elsif (<INFILE> =~ /ipchains/) {
    print "Converting $value2 (writing to $script)...";
    seek INFILE,0,0;
    while(<INFILE> ) {
    $line = $_;
    $line =~ s/ipchains/iptables/g;
    print OUTFILE "$line";
    print ".";
    }
    print "Done!n";
    }
    else {
    print "--> File did not contain any ipchains commands.n";
    }
    }
    print "n";
    }
    }

    ---

    When I run the script, I syntax errors at line 31 (where the elsif statement starts) and line 42 (where the else statement starts).

    I have no idea WHY this is occuring, but when I remove (comment out) the seek(INFILE,0,0); lines, it works (kind of... it executes without any errors, but there is nothing in the .tmp files created.)

    Can anyone help me with this, or provide me with some hints? It would be greatly appreciated!

    Thanks...!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Posts
    452
    Rep Power
    15
    the errors are occurring because youre trying to insert a statement inbetween an if and an elsif statement which is a no-no. unfortunately that seek statement is needed and is probably what is causing your problem so you'll need to find an alternative, perhaps 3 if statements rather than if/elsif/else

    also you're gonna end up with a bunch of empty files called whatever.tmp.tmp or whatever.tmp.tmp.tmp etc every time you run it because you're opening OUTFILE before you determine whether or not you need to. you should open OUTFILE in the following elsif block that way youre not creating garbage files.

    Lastly, a statement like if(<SOMEFILE> =~ /blah/) will only match the first line of the file, if what your looking for is in the first line it will work fine, if not you can undef $/ to search the whole file...


    hope that helps!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    5
    Rep Power
    0
    thanks!

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Dingle:
    Lastly, a statement like if(<SOMEFILE> =~ /blah/) will only match the first line of the file, if what your looking for is in the first line it will work fine, if not you can undef $/ to search the whole file...


    hope that helps!
    [/quote]

    how would I do the undef $/ ...?

    say I wanted to see if the file $module contains /ipchains/ in it.

    would I do if(undef $/ipchains/) ??? I didn't quite understand what you said.

    thanks a lot

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Posts
    452
    Rep Power
    15
    well, $/ is the file input seperator, which by default is a newline (n) character. When you read in a file this is what is used to split up the file, for instance a statement such as '@file = <FILEHANDLE>' is equivelant to '@file = split($/, <FILEHANDLE> )'. If you are trying to read the file as a scalar (like you are) it will only give you the first element, which would be the first line using the default $/ variable. To read the whole file as a scalar you can undefine $/ by adding 'undef $/;' before you read the file as a scalar, like at the beginning of the script.

    hope that helps

IMN logo majestic logo threadwatch logo seochat tools logo