2015-05-27
有这样一个需求,我有多个网址希望在不同的时候对应不同的 ip,如果一个个配 hosts,这工作显得有些繁琐。写了如下脚本来批量更改。
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=1c.example.com=1 # ip 记录[ip]local=127.0.0.1dev=192.168.1.100
php hosts.php local # 域名将指向本机 127.0.0.1php hosts.php dev # 域名将指向开发机 192.168.1.100php hosts.php # 删除域名的 hosts 配置
1
CI框架连接数据库配置操作以及多数据库操作
09-05
2
asp 简单读取数据表并列出来 ASP如何快速从数据库读取大量数据
05-17
3
C语言关键字及其解释介绍 C语言32个关键字详解
04-05
4
C语言中sizeof是什么意思 c语言里sizeof怎样用法详解
04-26
5
最简单的asp登陆界面代码 asp登陆界面源代码详细介绍
04-12
6
PHP中的魔术方法 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep,
09-05
7
PHP中的(++i)前缀自增 和 (i++)后缀自增
09-05
8
PHP中include和require区别之我见
09-05
常用dos命令及语法
2014-09-27
将视频设置为Android手机开机动画的教程
2014-12-11
php递归返回值的问题
2014-09-05
如何安装PHPstorm并配置方法教程 phpstorm安装后要进行哪些配置
2017-05-03
PHP 教程之如何使用BLOB存取图片信息实例
2014-09-05
java中的info是什么意思
2022-03-24
IcePHP框架中的快速后台中的通用CRUD功能框架
2014-09-05
单片机编程好学吗?单片机初学者怎样看懂代码
2022-03-21
PHP数组函数array
2014-09-05
学ug编程如何快速入门?
2022-03-17
逃跑吧少年官方正版手游下载v8.33.0 安卓2025手机版
动作闯关 1.33G
下载宝宝梦想小镇宝宝巴士最新版下载v9.87.00.00 安卓官方版
其它手游 95.9M
下载宝宝爱刷牙宝宝巴士教育版下载v9.87.00.00 安卓版
其它手游 148.0M
下载小玛丽捕鱼官方正版下载v5.13.0 安卓版
休闲益智 122.0M
下载小玛丽捕鱼旧版安装下载v5.10.10 安卓官方版
休闲益智 122.0M
下载原神bilibili服官方版下载v5.8.0 安卓哔哩哔哩服
其它手游 389.7M
下载天天捕鱼电玩版赢话费官方版下载v11.0 安卓版
休闲益智 89.8M
下载遇见龙2手游下载v1.1.157 安卓版
卡牌对战 1.29G
下载太空杀国际版(Super Sus)下载v1.67.25.031 安卓版
下载
变形金刚地球之战官方版下载v25.1.1.381 安卓最新版
下载
极限捕鱼安卓版下载v6.47.6.0.5.4 赢话费正版
下载
极限捕鱼高爆版游戏下载v6.47.6.0.5.4 安卓暴击版
下载
极限捕鱼万炮版下载v6.47.6.0.5.4 安卓加强版
下载
极限捕鱼达人版下载v6.47.6.0.5.4 安卓版
下载
2025pubgmobile国际服地铁逃生下载v3.9.0 安卓正版
下载
神灵部落官方下载v2.0 安卓版
下载