I've got a short script that downloads a bogons list for iptables blocking. The script is relatively simple (bottom below). At execution time, the following error occurs:
shield:/etc/iptables# perl -Tw testbog.pl
Global symbol "$blist" requires explicit package name at testbog.pl line 59.
Execution of testbog.pl aborted due to compilation errors.
I believe this is a variable scoping problem. I have tried several variations on the offending print statement without success. I have no doubt the solution is simple and staring me in the face, but I can't see it.
use strict;
use LWP::UserAgent;
# Define the shell file name
my $filenam = '/tmp/bogonlist.sh';
# Location of bogon list in CIDR format
my $boglist = '
http://www.completewhois.com/bogons/data/bogons-cidr-all.txt';
# Number of retrieval attempts
my $rvtries = 2;
print("Update iptables bogon blocking\n");
while ($rvtries > 0) {
print("Retrieve the bogon list\n");
# Create an LWP user agent object
my $ua = LWP::UserAgent->new;
$ua->agent("Bogon block list retriever");
# Create and submit an HTTP request for the document
my $req =
HTTP::Request->new(GET => $boglist);
my $res = $ua->request($req);
print("Status: ".$res->message."\n");
# Store the list and exit the while loop if successful
if ($res->is_success) {
my $blist = $res->content;
last;
}
# Count down retries and exit if exhausted
print("Unable to retrieve the bogon list\n");
$rvtries = $rvtries - 1;
if ($rvtries == 0) {
print("Retries exhausted - update failed\n");
exit(1);
}
}
print ("Retrieved the bogon list\n");
print("Create and open the block script file\n");
# Open the file for write
my $f1flag = open(F,"> $filenam");
# Exit if an error occurred
if (!($f1flag)) {
print("Unable to create the block script file\n");
print("Status: $f1flag\n");
exit(1);
}
print("Created and opened the block script file\n");
print("Write the block script file\n");
# Write the file
$f1flag = print F $blist; <--- problem occurs here
# Exit if an error occurred
if (!($f1flag)) {
print("Unable to write the block script file\n");
print("Status: $f1flag\n");
exit(1);
}
print("Wrote the block script file\n");
print ("Close the block script file\n");
# Close the file
$f1flag = close F;
if (!($f1flag)) {
print("Unable to close the block script file\n");
print("Status: $f1flag\n");
exit(1);
}
print("Closed the block list file\n");
Start Free Trial