PHP实现更改hosts文件的方法示例
作者:efbb 发布时间:2023-10-19 18:30:12
标签: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程序设计有所帮助。


猜你喜欢
- rhel5下默认安装mysql5.0后,中文显示为乱码原因:mysql默认字符集是latin,所以中文不能正常显示解决方法:修改配置文件,
- 网页给人最直观的感受就是它的页面框架与构造,就像一座大楼的主体框架与形态,你可能记不起东方明珠塔和艾菲尔铁塔是用什么颜色或什么材料涂的外墙,
- 需求:对方提供处理文件的接口,本地将待处理文件压缩后,通过http post multipart方式上传,等待处理完成后从相应连接下载结果代
- 它在Lynx里也会运行得很好:<%@ Language=VBScript %><HTML><
- pycharm的pytest功能在新建一个python文件时,比如名称是test_test_test.py,由于含有test,pycharm
- 此教程适合有pandas基础的童鞋来看,很多知识点会一笔带过,不做详细解释Pandas数据格式SeriesDataFrame:每个colum
- 一、前言本文就从数据爬取、数据清洗、数据可视化,这三个方面入手,但你简单完成一个小型的数据分析项目,让你对知识能够有一个综合的运用。整个思路
- 一、获取二叉树的深度就是二叉树最后的层次,如下图:实现代码:def getheight(self): &n
- 如何显示数据库中的图片和超级链接?代码见下:<% set conn=server.creatobject(&quo
- CSS命名规范一.文件命名规范全局样式:global.css;框架布局:layout.css;字体样式:font.css;链接样式:link
- 最近需要爬取某网站,无奈页面都是JS渲染后生成的,普通的爬虫框架搞不定,于是想到用Phantomjs搭一个代理。Python调用Phanto
- 网页版面布局的方法及技巧,版面指的是浏览器看到的完整的一个页面(可以包含框架和层)。因为每个人的显示器分辨率不同,所以同一个页面的大小可能出
- 本文实例讲述了Python加pyGame实现的简单拼图游戏。分享给大家供大家参考。具体实现方法如下:import pygame, sys,
- 自动弹出窗口是一个让人讨厌的事情,为什么我们用它来显示我们的调查表呢?用弹出窗口来显示调查表,被认为是达到我们收集访问用户信息的最方便快捷的
- 本文主要是基于Python Opencv 实现的图像分割,其中使用到的opencv的函数有:使用 OpenCV 函数 cv::filter2
- 本文涉及:Windows操作系统,Python,PyQt5,Qt Designer,PyCharm一、自适应原理 &
- 前两天由于一个小项目想为一元素添加一个阴影效果,但是记得看过某高人写的"用Div/CSS模拟阴影效果"文章,现在还有一点
- 企业管理器中的Tools,Database Maintenance Planner,可以设置数据库的定期自动备份计划。并
- 前言:作为一名从小就看篮球的球迷,会经常逛虎扑篮球及湿乎乎等论坛,在论坛里面会存在很多精美图片,包括NBA球队、CBA明星、花边新闻、球鞋美
- 前言这篇文章将为大家介绍:GoFrame 错误处理的常用方法&错误码的使用。如何自定义错误对象、如何忽略部分堆栈信息、如何自定义错误