Page 1 of 1
Fetch IP
Posted: Mon Jan 12, 2004 6:50 am
by teksys
Hey all!
I just wanna know if someone possibly know how to retrive a list of IP adresses from a CGI script?
It looks like this:
- LAN k0nsl 127.0.0.1 9035 Monday 12, 07:32:47 Win2000
I just need some simple script to get the IP and Port. Like this:
127.0.0.1:9035
Is this possible? And if so, can someone maybe show me how to do it
Thx in advance, anyway! Great forums

Posted: Mon Jan 12, 2004 6:57 am
by patrikG
In PHP those details are stored in the
$_SERVER array (a superglobal). To show the IP, for example, simply type:
Posted: Mon Jan 12, 2004 7:41 am
by teksys
patrikG, yes i know how to get IP in PHP.
But i want to fetch this information from a CGI Script or maybe even a text file or anything.
cgi
Posted: Mon Jan 12, 2004 8:39 am
by ol4pr0
not sure about the hole cgi <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> tho since i am not really into the whole perl thingie, did some a few ago so forgot a lot more since a few ago.
however in cgi its being retrived with the
$ENV{'REMOTE_ADDR'}
you also might wanna take a look at a script called pcm.pl
you can download it from this location
http://www.cpan.org/scripts/CGI/index.html
and for more information have a look @ this page
http://search.cpan.org/~jhi/perl-5.8.0/lib/CGI.pm
hope that will help you.
Posted: Mon Jan 12, 2004 8:55 am
by DuFF
You will need to use a regular expression to extract the IP address and port from that line. I am not very good with them so either you will have to find out how to do it or someone else on the forum might be able to help you.
I do know that you need to use preg_match to use the regular expression. I'll post back if I find anything else helpful.
Edit:
Ok, after a little fooling around I found something that might help you:
Code: Select all
<?php
preg_match_all("/\d+\.\d+\.\d+\.\d+\s\d+/",
"LAN k0nsl 127.0.0.1 9035 Monday 12, 07:32:47 Win2000", $matches);
?>
<pre>
<?php print_r($matches) ?>
</pre>
Will output:
Code: Select all
Array
(
ї0] => Array
(
ї0] => 127.0.0.1 9035
)
)
Then just use str_replace to replace the whitespace with a colon.
So the final code would be something like this:
Code: Select all
<?php
preg_match_all("/\d+\.\d+\.\d+\.\d+\s\d+/",
"LAN k0nsl 127.0.0.1 9035 Monday 12, 07:32:47 Win2000", $matches);
$ip = str_replace(":", " ", $matches[0][0]);
echo $ip; //echos 127.0.0.1:9035
?>
Posted: Mon Jan 12, 2004 12:45 pm
by teksys
thx for all your help, really nice.
but on that page there's ALOT of IP Adresses which i want to have formatted nicely in this format:
someip:someport
by using a PHP script to get the IP and Port from the cgi script. the IP's are Socks4 Servers which i need to format like ip:port through the cgi script, just like a backend
hmm i guess i'll have to look around a little more because i know it has been done before i just cant seem to find the code for doing what i want....
Posted: Mon Jan 12, 2004 2:25 pm
by redmonkey
Code: Select all
<?php
$input = "LAN k0nsl 127.0.0.1 9035 Monday 12, 07:32:47 Win2000\n
LAN k0nsl 192.47.233.1 9080 Monday 12, 07:32:47 Win2000\n
LAN k0nsl 255.168.10.1 9010 Monday 12, 07:32:47 Win2000\n
LAN k0nsl 192.168.0.1 5035 Monday 12, 07:32:47 Win2000\n";
preg_match_all('/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\s+(\d+) /', $input, $matches, PREG_SET_ORDER);
$n_matches = count($matches);
for ($i = 0; $i < $n_matches; $i++) {
echo "{$matches[$i][1]}:{$matches[$i][2]}\n<br>\n";
}
?>
Result...
127.0.0.1:9035
192.47.233.1:9080
255.168.10.1:9010
192.168.0.1:5035
Posted: Thu Jan 15, 2004 6:34 am
by teksys
@redmonkey: thx but i already know how to do that, and it's not what i am looking for.
It's like 100+ IP's on the list and they change daily, and is updated automaticly hourly. What i want to do is to retrive and format the IP's directly from the CGI Script, without having to manually add the IP's to my PHP Script - Because then i could just visit the CGI Script and just cut and paste whatever IP i want.
I wanted it to be more automated, like a backend of some kind...
Posted: Thu Jan 15, 2004 7:13 am
by redmonkey
teksys wrote:
patrikG, yes i know how to get IP in PHP.
teksys wrote:
@redmonkey: thx but i already know how to do that, and it's not what i am looking for.
Which bit do you want to know?
My previous code example was to show you how to use regex to extract the info you want from a given $input. You could quite easily use
fopen(),
fread() and
fclose() to have $input be the contents of a text file or the output from some CGI script, but I'm guessing you allready know how to do that

Posted: Thu Jan 15, 2004 8:53 am
by twigletmac
If the line is always in this form:
Code: Select all
LAN k0nsl 127.0.0.1 9035 Monday 12, 07:32:47 Win2000
you wouldn't need to use regex to get the IP and port no (although it would be a lot more robust if the lines were pipe (|) delimited. Just use [php_man]file[/php_man] to read the data into an array, then use [php_man]explode[/php_man]() on each line. The IP will then be in the third element of the array and the port in the fourth.
Mac