Help needed with Perl Script

i have the following script:

#!/usr/local/bin/perl
use STUN::Client;
use Data::Dumper;
use strict;

my $stun_client = STUN::Client->new;

$stun_client->stun_server('10.59.29.14');
my $r = $stun_client->get;

my $ip = $r->{ma_address};

print "IP: $ip\n\nResult (hash): \n".Dumper($r);

when run from the command line, it produces this:

[#####01] ./stun_check.pl

IP: 216.111.11.123

Result (hash): 
$VAR1 = {
          'ma_port' => 38822,
          'message_type' => 257,
          'message_length' => 48,
          'attr_type' => 1,
          'attr_length' => 8,
          'ma_family' => '1',
          'transaction_id' => 'C1Eblah13384',
          'ma_dummy' => '0',
          'ma_address' => '216.111.11.123'
        };

[#####01]

Can someone please help me modify the script to make it verify that the IP address returned is a public IP and that something was returned at all? when i say public IP, i mean ensure that the IP that is returned is valid (the octets are between 0-255 and not some bizarre IP).

i'm not good with perl at all so, i would appreciate it tremendously if any one of you gurus on this site can help me modify the above short script.

thanksss

Try:

#!/usr/local/bin/perl
use STUN::Client;
use Data::Dumper;
use strict;

my $stun_client = STUN::Client->new;

$stun_client->stun_server('10.59.29.14');
my $r = $stun_client->get;

my $ip = $r->{ma_address};
my @ip_a=split /\./,$ip;

print "IP: $ip\n\nResult (hash): \n".Dumper($r) if ($ip_a[0]>=0&&$ip_a[0]<=255&&$ip_a[1]>=0&&$ip_a[1]<=255&&$ip_a[2]>=0&&$ip_a[2]<=255&&$ip_a[3]>=0&&$ip_a[3]<=255);

Didn't test it though.

thank you so much for your post. it appears to be on the right track.

however, i guess i should have been clearer.

how can i make it so that the script aborts with a 0, if and only if the IP that is returned is valid (the octets are between 0-255 and not some bizarre IP)?

like, for instance, instead of all that other output, if i run the script, i want the script to only spit out (if everything is ok), something like this: 0:$(theipthatwasreturned).

#!/usr/local/bin/perl
use STUN::Client;
use Data::Dumper;
use strict;

my $stun_client = STUN::Client->new;

$stun_client->stun_server('10.59.29.14');
my $r = $stun_client->get;

my $ip = $r->{ma_address};
my @ip_a=split /\./,$ip;

print "0:$ip\n" if ($ip_a[0]>=0&&$ip_a[0]<=255&&$ip_a[1]>=0&&$ip_a[1]<=255&&$ip_a[2]>=0&&$ip_a[2]<=255&&$ip_a[3]>=0&&$ip_a[3]<=255);

Another one -

# Split the IP address into an array of octets
@x = split /\./, $ip;
 
# Print it unless it is invalid
print "0:$ip", "\n" unless ($#x != 3 or grep {$_ =~ /\D/ or $_ < 0 or $_ > 255} @x);

tyler_durden

i tried this and it hangs and finally spits this out:

Can't use string ("") as a HASH ref while "strict refs" in use at ./stun_check.pl line 11.

---------- Post updated 12-21-11 at 08:48 AM ---------- Previous update was 12-20-11 at 07:37 PM ----------

how do i incorporate this into the above script?