当前位置:首页 > PHP教程 > php应用 > 列表

PHP实现抓取Google IP并自动修改hosts文件

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-13 10:22:56 浏览: 评论:0 

这篇文章主要介绍了PHP实现抓取Google IP并自动修改hosts文件,本文方法可以实现免翻墙上google,小编亲测可用,需要的朋友可以参考下

无聊中居然又找到个php版本的抓取google hosts的文件,试了下还可以用,ping了下ip,延迟也不是很高,网页打开测试了下速度也很快,大家有兴趣的话可以试试.

自动更新hosts文件, 不覆盖已存在的记录,方便使用,不用每次都 复制->打开hosts文件->粘贴。

php文件:

  1. <?php 
  2. /** 
  3.  * 免翻墙上google 
  4.  * @author 自娱自乐自逍遥 <wapznw@gmail.com> 
  5.  * Date: 2015/2/6 
  6.  * Time: 11:42 
  7. */ 
  8.  
  9. define('START_TAG','#google-hosts-2015'); 
  10. define('END_TAG','#google-hosts-2015-end'); 
  11. if(!emptyempty($argv[1])){ 
  12.  $params = array(); 
  13.  parse_str($argv[1], $params); 
  14. if(isset($params['url'])){ 
  15.  define('GOOGLE_HOST_URL', $params['url']); 
  16. } 
  17. if(isset($params['del'])){ 
  18. define('DELETE_GOOGLE_HOST',true); 
  19. } 
  20. } 
  21. defined('GOOGLE_HOST_URL') || define('GOOGLE_HOST_URL', 'http://www.360kb.com/kb/2_150.html'); 
  22.  
  23. if(PHP_OS == 'WINNT'){ 
  24.  define('HOSTS_FILE_PATH', 'C:WindowsSystem32driversetchosts'); 
  25. }else if(in_array(PHP_OS, array('Linux','Darwin','FreeBSD','OpenBSD','WIN32','Windows','Unix'))){ 
  26.  define('HOSTS_FILE_PATH', '/etc/hosts'); 
  27. }else{ 
  28.  die('Unsupported system!'.PHP_EOL); 
  29. } 
  30.  
  31. if(!is_writable(HOSTS_FILE_PATH)){ 
  32.  die('Without permission, please use the root user to perform!'.PHP_EOL); 
  33. } 
  34.  
  35. $hosts = file_get_contents(HOSTS_FILE_PATH); 
  36.  
  37. $startPos = strpos($hosts, START_TAG); 
  38. if(!defined('DELETE_GOOGLE_HOST')){ 
  39.  $gs = get_google_hosts(); 
  40.  echo GOOGLE_HOST_URL.PHP_EOL; 
  41.  echo $gs.PHP_EOL; 
  42. }else{ 
  43.  $gs = ''; 
  44.  echo 'reset hosts'.PHP_EOL; 
  45. } 
  46.  
  47. if($startPos){ 
  48.  $_tmp = substr($hosts, $startPos, strpos($hosts, END_TAG) - $startPos + strlen(END_TAG)); 
  49.  $hosts = str_replace($_tmp,$gs,$hosts); 
  50. }else{ 
  51.  $hosts.= PHP_EOL.$gs; 
  52. } 
  53.  
  54. $old_file_size = filesize(HOSTS_FILE_PATH); 
  55.  
  56. if(file_put_contents(HOSTS_FILE_PATH, $hosts)){ 
  57.  die('success. '.PHP_EOL); 
  58. }else{ 
  59. die('fail'.PHP_EOL); 
  60. } 
  61.  
  62. function get_google_hosts(){ 
  63.  $html = file_get_contents(GOOGLE_HOST_URL); 
  64.  $html = strip_tags($html); 
  65.  $startPos = strpos($html, START_TAG); 
  66.  $html = substr($html, $startPos, strpos($html,END_TAG) - $startPos); 
  67.  $html = str_replace('&nbsp;',' ',$html); 
  68.  return $html.PHP_EOL.END_TAG; 
  69. }

Tags: PHP抓取Google IP

分享到: