#!/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);
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.
#!/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);
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);
# 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);