PHP 获取IP地址、连接数,并获取IP所在地址示例
内容摘要
这篇文章主要为大家详细介绍了PHP 获取IP地址、连接数,并获取IP所在地址示例,具有一定的参考价值,可以用来参考一下。
php-cli下执行windows下的运维工具,改成linux也很方便用P
php-cli下执行windows下的运维工具,改成linux也很方便用P
文章正文
这篇文章主要为大家详细介绍了PHP 获取IP地址、连接数,并获取IP所在地址示例,具有一定的参考价值,可以用来参考一下。
php-cli下执行windows下的运维工具,改成linux也很方便用PHP获取IP与连接数,并获取IP所在地址,主要针对80端口,其他端口也能看到开启反解ip可判断是否是蜘蛛,PHP获取IP与连接数,并获取IP所在地址php-cli下执行 windows下的运维工具,改成linux也很方便用PHP获取IP与连接数,并获取IP所在地址,主要针对80端口,其他端口也能看到开启反解ip可判断是否是蜘蛛,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | <code class = "php" > /** * 获取IP地址、连接数,并获取IP所在地址 * * @param * @author php教程 www.idcnote.com **/ //获取连接数最高的ip的详细信息 $top = 10; //获取ip的详细信息 $get_location = true; //反解ip,用于获取蜘蛛,开启后速度较慢 $reverse = FALSE; //$reverse = TRUE; ini_set ( 'extension_dir' ,dirname( __FILE__ )); ini_set ( 'enable_dl' ,TRUE); if (!dl( "php_curl.dll" )){ exit ( 'Can\'t load curl.' ); } $content = `netstat -an -p TCP`; $regex = "/\s+TCP\s+\d+\.\d+\.\d+\.\d+:(\d+)\s+(\d+\.\d+\.\d+\.\d+):\d+\s+/sm" ; $table = array (); //1、连接数/2、本机ip/3、对方ip /** * 端口/ip是唯一的 */ if (preg_match_all( $regex , $content , $result )){ foreach ( $result [1] as $i => $port ){ if (isset( $table [ $port . ':' . $result [2][ $i ]])) $table [ $port . ':' . $result [2][ $i ]]++; else $table [ $port . ':' . $result [2][ $i ]] = 1; } $curl = curl_init(); curl_setopt( $curl ,CURLOPT_TIMEOUT,5); curl_setopt( $curl , CURLOPT_HEADER, 0); curl_setopt( $curl , CURLOPT_RETURNTRANSFER, TRUE); $i = 0; $count = 0; if (asort( $table )) foreach ( $table as $key => $times ){ $ip = substr ( strstr ( $key , ':' ),1); $port = substr ( $key ,0, strpos ( $key , ':' )); $i ++; echo "\r\n连接数:" , $times , ' - ' , $key ; if (! $get_location || $i < count ( $table )- $top ){ continue ; } if ( $port == '80' ) $count += $times ; if ( $ip !== '127.0.0.1' || $ip !== '0.0.0.0' ){ $host = $reverse ? gethostbyaddr ( $ip ) : $ip ; if ( $host == $ip ){ curl_setopt( $curl ,CURLOPT_URL, "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=txt&ip=" . $ip ); $location = curl_exec( $curl ); echo "(" .preg_replace( '/(\s|\d|\.)/' , '' , $location ). ")" ; } else { echo "(" . $host . ")" ; } } } echo "\r\n" , 'All(80):' , $count ; }</code> |
注:关于PHP 获取IP地址、连接数,并获取IP所在地址示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释