Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old December 7th, 2000, 04:15 AM
nebuchadnezzar nebuchadnezzar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 5 nebuchadnezzar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to nebuchadnezzar
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...!

Reply With Quote
  #2  
Old December 14th, 2000, 02:41 PM
Dingle Dingle is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Posts: 452 Dingle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 12 sec
Reputation Power: 8
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!

Reply With Quote
  #3  
Old December 15th, 2000, 02:46 AM
nebuchadnezzar nebuchadnezzar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 5 nebuchadnezzar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to nebuchadnezzar
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


Reply With Quote
  #4  
Old December 15th, 2000, 07:02 PM
Dingle Dingle is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Posts: 452 Dingle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 12 sec
Reputation Power: 8
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > help with EOFs and the seek function, PLEASE!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway