thedarkness


<?

php

 
function _getip()
{
  exec("ifconfig", $ips);
  $

regex

  = "@addrApplause(?Applause?: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

perkiset

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

thedarkness

quote author=perkiset link=topic=243.msg1525#msg1525 date=1179461814

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


Already has perk, already has  Applause

Cheers,
td

thedarkness

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 ApplauseApplause??

There be weirdness,
td

thedarkness

Usage: (this one's

PHP

 5.x only) because of the use of the stream context;


<?

php

 
        function _getfile($url)
        {

                ini_set('error_reporting', E_ALL);
                $false = 0;
                exec("/sbin/ifconfig", $ips);
                $

regex

  = "@addrApplause(?Applause?: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

PHP

 4.x+;


<?

php

 
        function _getfile($url)
        {
                exec("/sbin/ifconfig", $ips);
                $

regex

  = "@addrApplause(?Applause?: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_create( AF_

INET

 , SOCK_STREAM, SOL_TCP );
                socket_bind ( $sock, $matches[1][array_rand( $matches[1] )] );
                $ip = gethostbyname( $URI_PARTS['host'] );
                socket_connect ( $sock, $ip, 80 );
                $request = 'GET '.$URI_PARTS['path'].'?'.$URI_PARTS['query']." HTTP/1.1 ".
                          'Host: '.$URI_PARTS['host']." ";
                socket_write( $sock, $request );
                $results = '';
                while( $buffer = @socket_read( $sock, 512 ,

PHP

 _NORMAL_READ ) )
                        $results .= $buffer;
                socket_close( $sock );
                return $results;

        }
?>

perkiset

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

thedarkness

We're here to help  Applause  Applause  Applause

Cheers,
td


Perkiset's Place Home   Politics @ Perkiset's