把 Windows 的 Hosts 文件转换成 DNSMASQ 格式
家中或者公司使用了 Tomato 或者 DD-WRT 固件路由器的同学可能想分享他们的“私密” Hosts 文件,但是又不方便拷贝到每台机器,那么把这个 Hosts 文件内容放到路由器上是最方便的,不过 DNSMasq 的 Hosts 文件与 Windows 的 Hosts 文件格式不同,需要做一个转换,于是有了下面这个 HTML 文件,其实就是一个简单的 Javascript 转换。复制所有内容,存为任意命名的 HTML 文件,用浏览器打开,然后点击转换按钮,把转换后的数据贴到路由器 DNS 设置界面就行了。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Hosts2DNSMasq</title>
<script type="text/javascript">
//类似 PHP 中的 trim
function trim(str) {
var str = str.replace(/^\s\s*/, ''),
ws = /\s/,
i = str.length;
while (ws.test(str.charAt(--i)));
return str.slice(0, i + 1);
}
//转换 Hosts 文件格式到 DNSMasq 的格式
function htol(el) {
var i;
var localz = new Array();
var hosts = document.getElementById(el).value;
var localzs='';
hostdomain = hosts.split("\n");
for (i=0;i<hostdomain.length;i++) {
if (trim(hostdomain[i]) != '') {
localz[i] = hostdomain[i].split("\t");
if (localz[i][1] != undefined && localz[i][0].indexOf("#") == -1) {
localzs += 'address=/'+localz[i][1]+'/'+localz[i][0]+'\n';
} else {
localzs += hostdomain[i]+'\n';
}
}
}
document.getElementById(el).value = localzs;
}
</script>
</head>
<body>
<div style="width:1024px; margin:0 auto;">
<h2>HOSTS</h2>
<div style="margin: 5px auto; display: block;">
<button type="button" id="hosttolocal" onclick="htol('hostsMap');">ConventerToDNSMASQ</button>
</div>
<textarea id="hostsMap" onclick="this.focus();this.select()" style="width:98%;height:550px;margin:0;padding:3px;display:block;"></textarea>
</div>
</body>
</html>
例如以下的 Hosts 数据:
203.208.45.200 base0.googlehosted.com
203.208.45.200 base1.googlehosted.com
203.208.45.200 base2.googlehosted.com
203.208.45.200 base3.googlehosted.com
203.208.45.200 base4.googlehosted.com
203.208.45.200 base5.googlehosted.com
203.208.45.200 bks0.books.google.com
203.208.45.200 bks1.books.google.com
会被转换成:
address=/base0.googlehosted.com/203.208.45.200
address=/base1.googlehosted.com/203.208.45.200
address=/base2.googlehosted.com/203.208.45.200
address=/base3.googlehosted.com/203.208.45.200
address=/base4.googlehosted.com/203.208.45.200
address=/base5.googlehosted.com/203.208.45.200
address=/bks0.books.google.com/203.208.45.200
address=/bks1.books.google.com/203.208.45.200
注意,只是做了简单的格式转换,没有利用 DNSMasq 的(泛域名)特性优化。
引用本文,复制粘贴...
7 个评论 “把 Windows 的 Hosts 文件转换成 DNSMASQ 格式”
太感谢了!转换后直接粘贴到DNSMASQ里就OK?
By 我懂的 on Dec 26, 2012
是,不过最好人工精简一下,要不就和 Hosts 文件一样,没什么意义了。例如文中的可以精简成这么一句:
address=/googlehosted.com/203.208.45.200
但是小心,别精简过度了。。。
By AvP on Dec 27, 2012
你好。为什么我打开之后点击转换,。没有任何反应呢。。
By 112 on Aug 26, 2013
function htol(el) {
var i, match, hosts, localzs = '',
ele = document.getElementById(el);
hosts = ele.value.split('\n');
for (i = 0; i
使用正则替换就好了。
博主的代码没考虑s使用空格分隔的情况吧。
其实一般的文本编辑器都可以搞定。
By aa65535 on Dec 29, 2013
function htol(el) {
var i, match, hosts, localzs = '',
ele = document.getElementById(el);
hosts = ele.value.split('\n');
for (i = 0; i
By aa65535 on Dec 29, 2013
function htol(el) {
var i, match, hosts, localzs = '',
ele = document.getElementById(el);
hosts = ele.value.split('\n');
for (i = 0; i < hosts.length; i ) {
match = hosts[i].match(/^\s*([\d\.] ?)\s (\S ?)\s*$/);
if (match) {
localzs = 'address=/' match[2] '/' match[1] '\n';
}
}
ele.value = localzs;
}
发个代码还要实体化
By aa65535 on Dec 29, 2013
function htol(el) {
var i, match, hosts, localzs = '',
ele = document.getElementById(el);
hosts = ele.value.split('\n');
for (i = 0; i < hosts.length; i ) {
match = hosts[i].match(/^\s*([\d\.] ?)\s (\S ?)\s*$/);
if (match) {
localzs = 'address=/' match[2] '/' match[1] '\n';
}
}
ele.value = localzs;
}
试试pre标签
By aa65535 on Dec 29, 2013