The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 16, 2019, 09:10:12 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Enumerate local IPs and return one at random  (Read 4044 times)
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« on: May 17, 2007, 07:41:54 PM »

Code:
<?php
function _getip()
{
  
exec("ifconfig"$ips);
  
$regex "@addr\:((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))@";
  
$ipstr implode$ips );

  
$ipstr preg_replace"@127\.0\.0\.1@"""$ipstr ); // Don't want localhost
  
preg_match_all$regex$ipstr$matches );


  return 
$matches[1][array_rand$matches[1] )];

}

// Example usage
print _getip();
?>


AS usual, if anyone can suggest a better way, let us all know about it.

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: May 17, 2007, 09:16:54 PM »

Nice one TD... that'll come in handy actually.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #2 on: May 17, 2007, 09:31:13 PM »

Nice one TD... that'll come in handy actually.

Already has perk, already has  Wink

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #3 on: May 19, 2007, 01:34:51 AM »

Take my advice and put the full path to ifconfig in the call to exec e.g. "/sbin/ifconfig"

I just burned hours on this, it was buried so deep and for some reason it was working intermittently Huh?Huh???

There be weirdness,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #4 on: May 19, 2007, 05:32:08 PM »

Usage: (this one's PHP5.x only) because of the use of the stream context;

Code:
<?php
        
function _getfile($url)
        {

                
ini_set('error_reporting'E_ALL);
                
$false 0;
                
exec("/sbin/ifconfig"$ips);
                
$regex "@addr\:((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))@";
                
$ipstr implode$ips );

                
$ipstr preg_replace"@127\.0\.0\.1@"""$ipstr ); // Don't want localhost
                
preg_match_all$regex$ipstr$matches );

                
$opts = array( 'socket' =>
                            array( 
'bindto' => $matches[1][array_rand$matches[1] )].':0' ) );
                
$context stream_context_create($opts);
                
results file_get_contents$url$false$context );
                return 
results;
        }
?>



This one's PHP4.x+;

Code:
<?php
        
function _getfile($url)
        {
                
exec("/sbin/ifconfig"$ips);
                
$regex "@addr\:((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))@";
                
$ipstr implode$ips );
                
$ipstr preg_replace"@127\.0\.0\.1@"""$ipstr ); // Don't want localhost
                
preg_match_all$regex$ipstr$matches );
                
$URI_PARTS parse_url$url );

                
$sock socket_createAF_INETSOCK_STREAMSOL_TCP );
                
socket_bind $sock$matches[1][array_rand$matches[1] )] );
                
$ip gethostbyname$URI_PARTS['host'] );
                
socket_connect $sock$ip80 );
                
$request 'GET '.$URI_PARTS['path'].'?'.$URI_PARTS['query']." HTTP/1.1\n".
                           
'Host: '.$URI_PARTS['host']."\n\n";
                
socket_write$sock$request );
                
$results '';
                while( 
$buffer = @socket_read$sock512 ,PHP_NORMAL_READ ) )
                        
$results .= $buffer;
                
socket_close$sock );
                return 
$results;

        }
?>

Logged

"I want to be the guy my dog thinks I am."
 - Unknown
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: May 20, 2007, 11:41:19 AM »

The more I read this and think about some of my boxes, the more I like it  Mobster
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #6 on: May 20, 2007, 05:07:20 PM »

We're here to help  ROFLMAO  ROFLMAO  ROFLMAO

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!