LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   determining ppp ip address using perl (https://www.linuxquestions.org/questions/linux-software-2/determining-ppp-ip-address-using-perl-41763/)

Smerk 01-16-2003 11:18 PM

determining ppp ip address using perl
 
Hi!
I found this script on the net which is supposed to return my ppp ip if up, however it just prints almost the whole of ifconfig... where is it going wrong?

#!/usr/bin/perl

$ip = `/sbin/ifconfig`;
$ip =~ s/(ppp0)(.*)/$2/s;

if (!$1) { print "No connection\n"; }
else {$ip =~ s/inet addr:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/$1/s;
print "IP is $ip\n";
}

the output is:

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:1014 errors:0 dropped:0 overruns:0 frame:0
TX packets:1014 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:425188 (415.2 Kb) TX bytes:425188 (415.2 Kb)

Link encap:Point-to-Point Protocol
inet addr:203.221.120.251 P-t-P:203.220.247.178 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:3963 errors:0 dropped:0 overruns:0 frame:0
TX packets:4300 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:2001631 (1.9 Mb) TX bytes:451840 (441.2 Kb)

I am pretty much a perl newbie so any info will help :)

acid_kewpie 01-17-2003 03:41 AM

you are using the regex comparisons incorrectly. the =~ operator will not overwrite the previous data, just replace (with s///g) the text as requested. you should use a different variable, so:
Code:

# do a comparison
$ifconfig=~/(blah)(ip)/;
#use the variable
my $ip = $2;

hth (hopefully)

have a scan through perlop for more info.

Smerk 01-17-2003 06:49 AM

thanks for the pointer,
forgive my dumbness but what is perlop?
:study:

acid_kewpie 01-17-2003 07:00 AM

part of the core perl documentation, Perl Operators. i normally get to it by googling for "perldoc perlop" but it will actaully already be on your system using the perldoc command

Smerk 01-17-2003 07:12 AM

sorry, straight after sending off that reply I googled it..
I should google first, ask dumb questions later :)
-
ouch me head is hurting after readin the first few paragraphs of it!
shouldna had those beers!


All times are GMT -5. The time now is 06:05 AM.