一个更改 hosts 的 PHP 脚本
2015-04-28 21:15
260 查看
有这样一个需求,我有多个网址希望在不同的时候对应不同的 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
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使用Yii框架写一个不间断执行的脚本
- MySQL利用命令和PHP脚本创建删除一个数据库
- 一个批量更改文件名的Python脚本
- 1.2 - 第一章:PHP基础 - 解剖一个PHP脚本
- 小技巧找出一个php的cron脚本出问题的代码行
- 一个批量更改文件名的Python脚本
- 又一个加密PHP脚本的解码方法
- 随手做的一个用PHP压缩javascript,css的小脚本
- 写一个php小脚本辅助渗透测试
- PHP一个防止注入的脚本
- 一个很简单的PHP的后端脚本,能够从(某个网中)得到最新新闻。
- 一个批量更改文件名的Python脚本
- PHP 一个可以过滤非法脚本的函数
- 一个很easy的脚本--php获取服务器端的相关信息
- 一个简单自动监控nginx 504错误的php脚本
- 一个很简单的修改hosts的脚本
- 一个简单的php LDAP认证脚本
- 用PHP实现一个MP3下载脚本
- 一个批量更改文件名的Python脚本