PHP实现更改hosts文件的方法示例

作者:efbb 时间:2023-10-19 18:30:12 

本文实例讲述了PHP实现更改hosts文件的方法。分享给大家供大家参考,具体如下:

有这样一个需求,我有多个网址希望在不同的时候对应不同的 ip,如果一个个配 hosts,这工作显得有些繁琐。写了如下脚本来批量更改。


<?php
define('HOST_FILE', 'C:\Windows\System32\drivers\etc\hosts');
$hm = new HostManage(HOST_FILE);
$env = $argv[1];
if (empty($env)) {
   $hm->delAllGroup();
} else {
   $hm->addGroup($env);
}
class HostManage {
   // hosts 文件路径
   protected $file;
   // hosts 记录数组
   protected $hosts = array();
   // 配置文件路径,默认为 __FILE__ . '.ini';
   protected $configFile;
   // 从 ini 配置文件读取出来的配置数组
   protected $config = array();
   // 配置文件里面需要配置的域名
   protected $domain = array();
   // 配置文件获取的 ip 数据
   protected $ip = array();
   public function __construct($file, $config_file = null) {
       $this->file = $file;
       if ($config_file) {
         $this->configFile = $config_file;
       } else {
         $this->configFile = __FILE__ . '.ini';
       }
       $this->initHosts()
           ->initCfg();
   }
   public function __destruct() {
       $this->write();
   }
   public function initHosts() {
       $lines = file($this->file);
       foreach ($lines as $line) {
           $line = trim($line);
           if (empty($line) || $line[0] == '#') {
               continue;
           }
           $item = preg_split('/\s+/', $line);
           $this->hosts[$item[1]] = $item[0];
       }
       return $this;
   }
   public function initCfg() {
       if (! file_exists($this->configFile)) {
           $this->config = array();
       } else {
           $this->config = (parse_ini_file($this->configFile, true));
       }
       $this->domain = array_keys($this->config['domain']);
       $this->ip = $this->config['ip'];
       return $this;
   }
   /**
    * 删除配置文件里域的 hosts
    */
   public function delAllGroup() {
       foreach ($this->domain as $domain) {
           $this->delRecord($domain);
       }
   }
   /**
    * 将域配置为指定 ip
    * @param type $env
    * @return \HostManage
    */
   public function addGroup($env) {
       if (! isset($this->ip[$env])) {
           return $this;
       }
       foreach ($this->domain as $domain) {
           $this->addRecord($domain, $this->ip[$env]);
       }
       return $this;
   }
   /**
    * 添加一条 host 记录
    * @param type $ip
    * @param type $domain
    */
   function addRecord($domain, $ip) {
       $this->hosts[$domain] = $ip;
       return $this;
   }
   /**
    * 删除一条 host 记录
    * @param type $domain
    */
   function delRecord($domain) {
       unset($this->hosts[$domain]);
       return $this;
   }
   /**
    * 写入 host 文件
    */
   public function write() {
       $str = '';
       foreach ($this->hosts as $domain => $ip) {
           $str .= $ip . "\t" . $domain . PHP_EOL;
       }
       file_put_contents($this->file, $str);
       return $this;
   }
}

示例配置文件如下:


# 域名
[domain]
a.example.com=1 # 请无视这个 =1,因为使用了 parse_ini_file 这个函数来解析,如果后面不带值,就获取不到这条记录了
b.example.com=1
c.example.com=1
# ip 记录
[ip]
local=127.0.0.1
dev=192.168.1.100

使用方法:


php hosts.php local # 域名将指向本机 127.0.0.1
php hosts.php dev # 域名将指向开发机 192.168.1.100
php hosts.php # 删除域名的 hosts 配置

写完后,发现,这明明就是只需要一次查找替换就能完成的工作嘛

希望本文所述对大家PHP程序设计有所帮助。

标签:PHP,hosts文件
0
投稿

猜你喜欢

  • SQL按照日、周、月、年统计数据的方法分享

    2024-01-24 13:48:27
  • vue中子组件调用兄弟组件方法

    2024-04-30 10:24:44
  • SQL Server误区30日谈 第9天 数据库文件收缩不会影响性能

    2024-01-17 17:45:01
  • 随机提取N条记录方法

    2007-10-11 18:04:00
  • Python调整数组形状如何实现

    2021-01-06 09:55:34
  • Python作用域用法实例详解

    2022-12-10 15:44:42
  • Python之文字转图片方法

    2022-02-12 07:11:10
  • selenium鼠标操作实战案例详解

    2023-09-18 04:08:40
  • 手写一个python迭代器过程详解

    2021-06-29 07:45:23
  • python使用KNN算法手写体识别

    2022-06-28 05:30:56
  • Python中的xlrd模块使用原理解析

    2021-07-24 14:25:21
  • Python列表生成式与生成器操作示例

    2023-08-05 14:16:45
  • PHP简单实现冒泡排序的方法

    2024-06-07 15:45:49
  • python实现126邮箱发送邮件

    2022-07-29 23:37:56
  • python 多线程共享全局变量的优劣

    2023-11-20 20:55:16
  • 在Linux下搭建Git服务器的方法详解

    2022-02-05 16:35:10
  • Js-$.extend扩展方法使方法参数更灵活

    2024-05-02 16:16:21
  • Python开发中爬虫使用代理proxy抓取网页的方法示例

    2023-02-04 08:56:06
  • Python性能分析工具pyinstrument提高代码效率

    2021-01-24 02:37:44
  • Linux下用Python脚本监控目录变化代码分享

    2021-03-30 20:15:59
  • asp之家 网络编程 m.aspxhome.com