快速切换IPv4/IPv6 host的脚本

2011年8月13日 | 分类: 翻墙相关 | 标签: , , ,

这篇文章讲了一些Google等国外网站IPv6 host列表。使用它们速度更快,更无束缚。但是如果在一个IPv6经常坏掉的校园网,或者需要经常移动到没有IPv6的网络中,一个切换IPv4/IPv6 host列表的脚本无疑很重要。

Windows平台

Window平台中host列表存放在 %windir%\System32\drivers\etc,我们规定IPv6的hosts存放在hosts.v6,IPv4的hosts存放在hosts.v4,处于其中一个状态时其内容存放在hosts文件中,使用如下batch则可以完成hosts状态的判断。

?
SwitchHosts.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
::  Description:
::    This batch is used to switch host files from ipv4 to ipv6 or the other.
@ECHO OFF
SET FileLocation=%WINDIR%\System32\drivers\etc
SET IPV4FileName=hosts.v4
SET IPV6FileName=hosts.v6
SET TargetFileName=hosts
:: If host.ipv6 exits, switch to IPV6, else switch to IPV4
IF EXIST %FileLocation%\%IPV6FileName% (
    :: Switch to IPV6
    CHOICE /M "Swithed to IPv6?" /T 0020 /D N
    IF ERRORLEVEL 2 GOTO END
    IF ERRORLEVEL 1 ECHO Successfully Switched to IPv6 Hosts!
    REN %FileLocation%\%TargetFileName% %IPV4FileName%
    REN %FileLocation%\%IPV6FileName% %TargetFileName%
) ELSE IF EXIST %FileLocation%\%IPV4FileName% (
    :: Switch to IPV4
    CHOICE /M "Swithed to IPv4?" /T 0020 /D N
    IF ERRORLEVEL 2 GOTO END
    IF ERRORLEVEL 1 ECHO Successfully Switched to IPv4 Hosts!
    REN %FileLocation%\%TargetFileName% %IPV6FileName%
    REN %FileLocation%\%IPV4FileName% %TargetFileName%
)
PAUSE
EXIT
:END
ECHO Nothing changed!

在Windows vista和 Windows 7系统中,有用户帐户控制(UAC),总是右键去点击使用管理员权限执行会麻烦,一个解决方案是使用Quick batch file compiler将其编译成exe,然后设定成总是需要管理员权限即可。
最终生成的是SwitchHosts.exe文件。

Linux平台

Linux平台中host列表存放在 \etc,我们做同样的文件名规定。

?
switch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
#Description:
#This scripts is used to switch host files from ipv4 to ipv6 or the other.
if [ -f /etc/hosts.v6 ] # Now using ipv4 mode
then
    sudo cp /etc/hosts /etc/hosts.v4
    sudo cp /etc/hosts.v6 /etc/hosts
    sudo rm -f /etc/hosts.v6
    echo "Successfully switched to ipv6 hosts!"
elif [ -f /etc/hosts.v4 ] # Now using ipv6 mode
then
    sudo cp /etc/hosts /etc/hosts.v6
        sudo cp /etc/hosts.v4 /etc/hosts
        sudo rm -f /etc/hosts.v4
    echo "Successfully switched to ipv4 hosts!"
else
    echo "ERROR! No Backup File is found!"
fi

最后

?
1
2
chmod switch 644
cp ./switch /usr/bin

之后直接在terminal中输入switch即可~
在Ubuntu 10.04测试通过。
EOF

原文http://logicmd.net/2011/06/a-script-quickly-switch-ipv4-ipv6-host/

目前还没有任何评论.