VPS动态切换IP · zijian’s blog

VPS动态切换IP · zijian’s blog

VPS介绍

VPS(Virtual Private Server 虚拟专用服务器)技术,将一台服务器分割成多个虚拟专享服务器的优质服务。实现VPS的技术分为容器技术,和虚拟化技术。在容器或虚拟机中,每个VPS都可分配独立公网IP地址、独立操作系统、实现不同VPS间磁盘空间、内存、CPU资源、进程和系统配置的隔离,为用户和应用程序模拟出“独占”使用计算资源的体验。VPS可以像独立服务器一样,重装操作系统,安装程序,单独重启服务器。VPS为使用者提供了管理配置的自由,可用于企业虚拟化,也可以用于IDC资源租用。

动态VPS可以采用PPTP或者ADSL拨号的方式获取动态IP,不仅可以区域性IP,还可以混拨,从而获得全国各地的IP地址。

接下来将介绍不同操作系统如何实现动态IP的自动切换。

windows下通过bat脚本自动切换IP

新建一个bat文件,内容如下:

:t
rasdial 宽带连接 /disconnect
ping -n 1 127.1>nul
rasdial 宽带连接 x3ppp12345 1234
ping -n 180 127.1>nul
goto :t

这个一个无线循环命令,首先断开宽带连接,等待一秒钟后重新拨号,x3ppp12345和1234分别是拨号的用户名和密码。等待180秒后,再次循环。

ping -n 180 127.1>null
这条命令的意思是运行这条命令需要180秒,>null是不显示命令执行结果。该命令通常用作计时器。

以上脚本可以实现无限循环拨号,但是如果宽带连接出现错误,必须要等待180秒后才能重新拨号。
要解决这个问题,可加一条判断语句,只有拨号没有错误,才执行等待语句,否则一直循环直到拨号成功。

rasdial 宽带连接 /disconnect
ping -n 1 127.1>nul
rasdial 宽带连接 x3ppp12345 1234
if %ERRORLEVEL% neq 0 (
ping -n 180 127.1>nul&goto :t)

linux系统配置PPPOE拨号程序

首先判断拨号程序是否存在,linux的拨号程序使用pppoe-start命令。
不同的linux系统的拨号程序有可能不一样,有的使用adsl-start命令。所以首先得看下系统的发行版本。


cat /etc/redhat-release

如果系统已经自带pppoe-start拨号命令,接下来直接使用pppoe-setup设置adsl拨号用户名和密码。

Welcome to the PPPoE client setup.  First, I will run some checks on
your system to make sure the PPPoE client is installed properly...


LOGIN NAME

Enter your Login Name (default root):

如果是第一次设置拨号会提示以上信息,如果之前设置过,会提示是否覆盖以前的记录。

以上提示输入adsl账号,输入完成后,终端提示

INTERFACE

Enter the Ethernet interface connected to the PPPoE modem
For Solaris, this is likely to be something like /dev/hme0.
For Linux, it will be ethX, where 'X' is a number.
(default eth0):

提示pppoe拨号使用的网卡,如果机器只有一个网卡,所以选择默认就好。

输入回车自动选择默认网卡后返回提示

Do you want the link to come up on demand, or stay up continuously?
If you want it to come up on demand, enter the idle time in seconds
after which the link should be dropped. If you want the link to
stay up permanently, enter 'no' (two letters, lower-case.)
NOTE: Demand-activated links do not interact well with dynamic IP
addresses. You may have some problems with demand-activated links.
Enter the demand value (default no):

以上提示是询问是按需连接还是始终保持连接,选择默认的始终连接后回车

DNS

Please enter the IP address of your ISP's primary DNS server.
If your ISP claims that 'the server will provide dynamic DNS addresses',
enter 'server' (all lower-case) here.
If you just press enter, I will assume you know what you are
doing and not modify your DNS setup.
Enter the DNS information here:

一般不用设置DNS,使用自动获取DNS即可,直接回车。

PASSWORD

Please enter your Password:

Please re-enter your Password:

按提示输入密码并再次输入密码。

USERCTRL

Please enter 'yes' (three letters, lower-case.) if you want to allow
normal user to start or stop DSL connection (default yes):

提示是否允许其他用户连接,输入no后回车。

FIREWALLING

Please choose the firewall rules to use. Note that these rules are
very basic. You are strongly encouraged to use a more sophisticated
firewall setup; however, these will provide basic security. If you
are running any servers on your machine, you must choose 'NONE' and
set up firewalling yourself. Otherwise, the firewall rules will deny
access to all standard servers like Web, e-mail, ftp, etc. If you
are using SSH, the rules will block outgoing SSH connections which
allocate a privileged source port.

The firewall choices are:
0 - NONE: This script will not set any firewall rules. You are responsible
for ensuring the security of your machine. You are STRONGLY
recommended to use some kind of firewall rules.
1 - STANDALONE: Appropriate for a basic stand-alone web-surfing workstation
2 - MASQUERADE: Appropriate for a machine acting as an Internet gateway
for a LAN
Choose a type of firewall (0-2):

防火墙设置有三个选项,我们不需要设置,输入0回车。

Start this connection at boot time

Do you want to start this connection at boot time?
Please enter no or yes (default no):
···

输入yes设置开机自动连接。


** Summary of what you entered **

Ethernet Interface: eth0
User name: adsluser
Activate-on-demand: No
DNS: Do not adjust
Firewalling: NONE
User Control: no
Accept these settings and adjust configuration files (y/n)?

提示是否将刚刚的设置生成配置文件,输入y后回车。

Adjusting /etc/sysconfig/network-scripts/ifcfg-ppp0
Adjusting /etc/ppp/chap-secrets and /etc/ppp/pap-secrets
(But first backing it up to /etc/ppp/chap-secrets.bak)
(But first backing it up to /etc/ppp/pap-secrets.bak)



Congratulations, it should be all set up!

Type '/sbin/ifup ppp0' to bring up your xDSL link and '/sbin/ifdown ppp0'
to bring it down.
Type '/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-ppp0'
to see the link status.

上面的提示说明了配置文件的位置和基本使用方法。

配置完成后测试拨号是否成功,直接执行pppoe-start命令,稍等几秒钟。如果没有报错继续执行pppoe-status命令查询是否拨号成功。

pppoe-status: Link is up and running on interface ppp0
3: ppp0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1480 qdisc pfifo_fast state UNKNOWN qlen 3
link/ppp
inet 10.2.xxx.xx peer 10.2.254.254/32 scope global ppp0
valid_lft forever preferred_lft forever

若能正常获取到ip地址则表示拨号成功。

然后使用ping baidu.com查看是否能连接外网。

linux下通过python脚本自动切换IP

python脚本内容如下




import subprocess
import time
from datetime import datetime
import sys
import re

reload(sys)
sys.setdefaultencoding('utf8')

def change_ip_for_vps():
try:
subprocess.Popen('pppoe-stop', shell=True, stdout=subprocess.PIPE)
time.sleep(5)
subprocess.Popen('pppoe-start', shell=True, stdout=subprocess.PIPE)
time.sleep(5)
pppoe_restart = subprocess.Popen('pppoe-status', shell=True, stdout=subprocess.PIPE)
pppoe_restart.wait()
pppoe_log = pppoe_restart.communicate()[0]
adsl_ip = re.findall(r'inet (.+?) peer ', pppoe_log)[0]
print '[*] New ip address : ' + adsl_ip
return True
except Exception, e:
print e
change_ip_for_vps()

if __name__ == '__main__':
count = 1
while True:
i = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print '[*] The %sth dialing. now is %s ' % (str(count),i)
change_ip_for_vps()
count += 1
time.sleep(180)

编写shell启动脚本,并将结果输出到日志中。

nohup python -u ipswitch.py > nohup.log 2>&1 &