PHP实现更改hosts文件的方法示例
时间:2019-03-31
本文章向大家介绍PHP实现更改hosts文件的方法示例,主要包括PHP实现更改hosts文件的方法示例使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
本文实例讲述了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文件操作总结》、《PHP网络编程技巧总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
- php性能监测模块XHProf
- BZOJ 1022: [SHOI2008]小约翰的游戏John (Anti-nim)
- 洛谷P2252 取石子游戏(威佐夫博弈)
- HDU 3032 Nim or not Nim?(Multi-Nim)
- POJ 2311 Cutting Game(二维SG+Multi-Nim)
- js去掉html标签和去掉字符串文本的所有的空格
- php操作memcache的使用测试总结
- linux awk命令详解
- php str_split 解决中文
- PHP汉字转拼音函数
- 51NOD 1185 威佐夫游戏 V2(威佐夫博弈)
- HDU 1527 取石子游戏(威佐夫博弈)
- PHP文件操作类
- Linux添加/删除用户和用户组
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- Codeforces Round #624 (Div. 3) A - Add Odd or Subtract Even
- XMLHttpRequest
- Codeforces Round #624 (Div. 3) B - WeirdSort
- 详解 Ajax
- 这个腾讯博客是被腾讯爬虫爬过来的,样式丑,请看我博客园地址,见下文
- Codeforces Round #624 (Div. 3) C - Perform the Combo
- 都是微服务的天下了,还有不知道 JSON 的程序员吗?
- Codeforces Round #624 (Div. 3) D.Three Integers
- select 高级查询之连接查询
- select 高级查询之子查询
- Java 最实用的文件读写
- MySQL基本操作
- MySQL 索引
- notepad++ 配置c++及运行
- Leetcode 687. 最长同值路径