Anybody know Perl?

Mercutio

Fatwah on Western Digital
Joined
Jan 17, 2002
Messages
21,637
Location
I am omnipresent
I know perl.

I also know that debugging someone else's perl program is next to "sliding down a razor blade into a bucket of iodine" on my list of favorite things to do.

What's wrong?
 

Howell

Storage? I am Storage!
Joined
Feb 24, 2003
Messages
4,740
Location
Chattanooga, TN
Mercutio said:
I know perl.

I also know that debugging someone else's perl program is next to "sliding down a razor blade into a bucket of iodine" on my list of favorite things to do.

What's wrong?

I'm customising my smoothwall and contributing code. I know no perl. But, I've just figured out that there is a code difference between parrenthses and curly brackets despite how similar they look at high resolution.
 

Howell

Storage? I am Storage!
Joined
Feb 24, 2003
Messages
4,740
Location
Chattanooga, TN
If you'ld like to take a stab,

${swroot}/ddns/config contains

dyndns,cliptin,dyndns.org,off,on,userid,password,on
dyndns-custom,home,whstrain.us,off,on,userid,password,on

Code:
#!/usr/bin/perl
#
# SmoothWall CGIs
#
# This code is distributed under the terms of the GPL
#
# (c) The SmoothWall Team

close(STDIN);
close(STDOUT);
close(STDERR);

require '/var/smoothwall/header.pl';

my %settings;
my %noipsettings;
my $filename = "${swroot}/ddns/config";
my $cachefile = "${swroot}/ddns/ipcache";
my $ipcache = 0;

open(FILE, "$filename") or die 'Unable to open config file.';
my @current = <FILE>;
close(FILE);
my $lines = $#current + 1;
unless($lines) { exit 0; }

open(IP, "${swroot}/red/local-ipaddress") or die 'Unable to open local-ipaddress
 file.';
my $ip = <IP>;
close(IP);
chomp $ip;

if ($ARGV[0] ne '-f')
{
        open(IPCACHE, "$cachefile");
        $ipcache = <IPCACHE>;
        close(IPCACHE);
        chomp $ipcache;
}

if ($ip ne $ipcache)
{
        my $id = 0;
        my $success = 0;
        my $line;

        foreach $line (@current)
        {
                $id++;
                chomp($line);
                my @temp = split(/\,/,$line);
                unless ($temp[7] eq "off")
                {
                        $settings{'SERVICE'} = $temp[0];
                        $settings{'HOSTNAME'} = $temp[1];
                        $settings{'DOMAIN'} = $temp[2];
                        $settings{'PROXY'} = $temp[3];
                        $settings{'WILDCARDS'} = $temp[4];
                        $settings{'LOGIN'} = $temp[5];
                        $settings{'PASSWORD'} = $temp[6];
                        $settings{'ENABLED'} = $temp[7];
                        my @service = split(/\./, "$settings{'SERVICE'}");
                        $settings{'SERVICE'} = "$service[0]";
                        if ($settings{'SERVICE'} eq 'no-ip')
                        {
                                $noipsettings{'LOGIN'} = $settings{'LOGIN'};
                                $noipsettings{'PASSWORD'} = $settings{'PASSWORD'
};
                                $noipsettings{'HOSTNAME'} = $settings{'HOSTNAME'
};
                                $noipsettings{'DOMAIN'} = $settings{'DOMAIN'};
                                $noipsettings{'DAEMON'} = 'N';
                                $noipsettings{'DEVICE'} = '';
                                $noipsettings{'INTERVAL'} = '1';
                                $noipsettings{'NAT'} = 'N';
                                if ($settings{'PROXY'} eq 'on') { $noipsettings{
'PROXY'} = 'Y'; }
                                else { $noipsettings{'PROXY'} = 'N'; }

                                &writehash("${swroot}/ddns/noipsettings", \%noip
settings);
                                open(F, "${swroot}/ddns/noipsettings");
                                my @unsorted = <F>;
                                close(F);
                                my @sorted = sort { $b cmp $a } @unsorted;
                                open(F, ">${swroot}/ddns/noipsettings");
                                flock F, 2;
                                print F @sorted;
                                close(F);

                                my @ddnscommand = ('/usr/local/bin/noip_for_libc
6','-c',"${swroot}/ddns/noipsettings",'-i',"$ip");

                                my $result = system(@ddnscommand);
                                if ( $result != 0) { &log("Dynamic DNS ip-update
 for $settings{'HOSTNAME'}.$settings{'DOMAIN'}: failure"); }
                                else
                                {
                                        &log("Dynamic DNS ip-update for $setting
s{'HOSTNAME'}.$settings{'DOMAIN'}: success");
                                        $success++;
                                }
                        }
                        else
                        {
                                if ($settings{'WILDCARDS'} eq 'on') {$settings{'
WILDCARDS'} = '-w';}
                                else {$settings{'WILDCARDS'} = '';}
                                if ($settings{'SERVICE'} eq 'dyndns-custom')
                                {
                                        &log("Service=$settings{'SERVICE'}");
                                        my @ddnscommand = ('/usr/local/bin/ez-ip
update', '-a', "$ip", '-S', "$settings{'SERVICE'}", '-u', "$settings{'LOGIN'}:$s
ettings{'PASSWORD'}", '-h', "$settings{'DOMAIN'}", "$settings{'WILDCARDS'}", '-q
');
                                        &log("Modified command on $settings{'HOS
TNAME'}.$settings{'DOMAIN'}");
                                }
                                else
                                {
                                        &log("Service=$settings{'SERVICE'}");
                                        my @ddnscommand = ('/usr/local/bin/ez-ip
update', '-a', "$ip", '-S', "$settings{'SERVICE'}", '-u', "$settings{'LOGIN'}:$s
ettings{'PASSWORD'}", '-h', "$settings{'HOSTNAME'}.$settings{'DOMAIN'}", "$setti
ngs{'WILDCARDS'}", '-q');
                                        &log("Standard command on $settings{'HOS
TNAME'}.$settings{'DOMAIN'}");
                                }

                                my $result = system(@ddnscommand);
                                if ( $result != 0) { &log("Dynamic DNS ip-update
 for $settings{'HOSTNAME'}.$settings{'DOMAIN'}: failure"); }
                                else
                                {
                                        &log("Dynamic DNS ip-update for $setting
s{'HOSTNAME'}.$settings{'DOMAIN'}: success");
                                        $success++;
                                }
                        }
                }
        }

        if ($lines == $success)
        {
                open(IPCACHE, ">$cachefile");
                flock IPCACHE, 2;
                print IPCACHE $ip;
                close(IPCACHE);
        }
}

else { &log('Dyanmic DNS ip-update: your IP is already up-to-date'); }

Which results in,
Code:
22:40:07 smoothwall Service=dyndns
22:40:07 smoothwall Standard command on cliptin.dyndns.org
22:40:07 smoothwall Dynamic DNS ip-update for cliptin.dyndns.org: failure
22:40:07 smoothwall Service=dyndns-custom
22:40:07 smoothwall Modified command on home.whstrain.us
22:40:07 smoothwall Dynamic DNS ip-update for home.whstrain.us: failure

I can run the ez-ip command from command line and it works fine:
Code:
ez-ipupdate -a 67.34.156.150 -S dyndns-custom -u userid:password -h whstrain.us
 

Howell

Storage? I am Storage!
Joined
Feb 24, 2003
Messages
4,740
Location
Chattanooga, TN
I'm working on the other half of the problem now. Merc, can you tell me what this means:
Code:
if ($cgiparams{'ACTION'} eq $tr{'add'})
{
unless ($cgiparams{'HOSTNAME'} =~ /^[a-zA-Z_0-9-]+$/) { $errormessage = $tr{'invalid hostname'}; }
}
 

Newtun

Storage is nice, especially if it doesn't rotate
Joined
Nov 21, 2002
Messages
467
Location
Virginia
I don't know perl, but I'd bet that it means that, if the ACTION param is add, then set the errormessage string to "invalid hostname" if hostname is not a non-empty string consisting of alphanumeric, "_", or "-" characters.
 

Handruin

Administrator
Joined
Jan 13, 2002
Messages
13,741
Location
USA
Wouldn't $tr{'add'} be the associated value of add in the array?
 

Howell

Storage? I am Storage!
Joined
Feb 24, 2003
Messages
4,740
Location
Chattanooga, TN
Handruin said:
Wouldn't $tr{'add'} be the associated value of add in the array?

No, in this case there is no array. I know Newtun is close based on what the results are.

Newtun, You think =~ means "not".
 

Howell

Storage? I am Storage!
Joined
Feb 24, 2003
Messages
4,740
Location
Chattanooga, TN
OK, I'd like to change it from:
Code:
if ($cgiparams{'ACTION'} eq $tr{'add'})
{
unless ($cgiparams{'HOSTNAME'} =~ /^[a-zA-Z_0-9-]+$/) { $errormessage = $tr{'invalid hostname'}; }
}

To:
Code:
if ($cgiparams{'ACTION'} eq $tr{'add'})
{
       unless
       (
              ($cgiparams{'HOSTNAME'} =~ /^[a-zA-Z_0-9-]+$/)
              OR ($cgiparams{'SERVICE'} eq {'dyndns-custom'}
       )
       { $errormessage = $tr{'invalid hostname'}; }
}

But I'm not sure if I have the syntax right. Thanks for the help.
 
Top