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


Posted in PHP onAugust 08, 2017

本文实例讲述了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模板之Phpbean的目录结构
Jan 10 PHP
PHP 编写大型网站问题集
May 07 PHP
浅析memcache启动以及telnet命令详解
Jun 28 PHP
php中如何防止表单的重复提交
Aug 02 PHP
php使用PDO操作MySQL数据库实例
Dec 30 PHP
PHP中SSO Cookie登录分析和实现
Nov 06 PHP
ThinkPHP中where()使用方法详解
Apr 19 PHP
Centos 6.5下PHP 5.3安装ffmpeg扩展的步骤详解
Mar 02 PHP
PHP Post获取不到非表单数据的问题解决办法
Feb 27 PHP
PHP中十六进制颜色与RGB颜色值互转的方法
Mar 18 PHP
wordpress自定义标签云与随机获取标签的方法详解
Mar 22 PHP
PHP 出现 http500 错误的解决方法
Mar 09 PHP
PHP编程实现阳历转换为阴历的方法实例
Aug 08 #PHP
PHP数据分析引擎计算余弦相似度算法示例
Aug 08 #PHP
Eclipse PHPEclipse 配置的具体步骤
Aug 08 #PHP
PHP 文件锁与进程锁的使用示例
Aug 07 #PHP
PHP实现找出有序数组中绝对值最小的数算法分析
Aug 07 #PHP
php基于session锁防止阻塞请求的方法分析
Aug 07 #PHP
在Yii2特定页面如何禁用调试工具栏Debug Toolbar详解
Aug 07 #PHP
You might like
利用static实现表格的颜色隔行显示的代码
2007/09/02 PHP
php中3种方法统计字符串中每种字符的个数并排序
2012/08/27 PHP
Codeigniter框架实现获取分页数据和总条数的方法
2014/12/05 PHP
weiphp微信公众平台授权设置
2016/01/04 PHP
php 使用expat方式解析xml文件操作示例
2019/11/26 PHP
jQuery 行级解析读取XML文件(附源码)
2009/10/12 Javascript
基于jquery的高性能td和input切换并可修改内容实现代码
2011/01/09 Javascript
js控制容器隐藏出现防止样式变化的两种方法
2014/04/25 Javascript
教你用AngularJS框架一行JS代码实现控件验证效果
2014/06/23 Javascript
jQuery实现的AJAX简单弹出层效果代码
2015/11/26 Javascript
jQuery中cookie插件用法实例分析
2015/12/04 Javascript
jQuery弹出遮罩层效果完整示例
2016/09/13 Javascript
深入理解vue.js双向绑定的实现原理
2016/12/05 Javascript
JS实现的找零张数最小问题示例
2017/11/28 Javascript
一步一步的了解webpack4的splitChunk插件(小结)
2018/09/17 Javascript
JavaScript循环遍历你会用哪些之小结篇
2018/09/28 Javascript
Vue实现一个无限加载列表功能
2018/11/13 Javascript
vue实现登录页面的验证码以及验证过程解析(面向新手)
2019/08/02 Javascript
浅析微信小程序modal弹窗关闭默认会执行cancel问题
2019/10/14 Javascript
在Python中使用判断语句和循环的教程
2015/04/25 Python
Python中使用strip()方法删除字符串中空格的教程
2015/05/20 Python
利用Python获取操作系统信息实例
2016/09/02 Python
使用Python进行QQ批量登录的实例代码
2018/06/11 Python
python与caffe改变通道顺序的方法
2018/08/04 Python
python实现AES和RSA加解密的方法
2019/03/28 Python
python 利用浏览器 Cookie 模拟登录的用户访问知乎的方法
2019/07/11 Python
Python实现蒙特卡洛算法小实验过程详解
2019/07/12 Python
解决tensorflow添加ptb库的问题
2020/02/10 Python
Python实现自动打开电脑应用的示例代码
2020/04/17 Python
Python Tornado实现WEB服务器Socket服务器共存并实现交互的方法
2020/05/26 Python
完美解决ARIMA模型中plot_acf画不出图的问题
2020/06/04 Python
北美大型运动类产品商城:Champs Sports
2017/01/12 全球购物
来自世界各地的饮料:Flavourly
2019/05/06 全球购物
奥地利票务门户网站:oeticket.com
2019/12/31 全球购物
应届中专生自荐书范文
2014/02/13 职场文书
读书笔记格式
2015/07/02 职场文书