如何正確的使用動態VPS(Linux)自動更換IP

背景

如今愈來愈多的人開始玩網賺項目,螞蟻再小也是肉,薅羊毛的羊毛黨愈來愈多,一些網賺項目也愈來愈受歡迎,可是通常的網賺項目都是要求真實用戶的,因此要想得到大量的真實ip,一種動態VPS就誕生了,動態VPS採用PPTP或者ADSL撥號的方式獲取動態IP,不只能夠作到獲取區域性IP,還能夠混撥,也就是能夠獲取全國各地的IP地址,這樣對於網賺項目來講就很是好了,輕鬆模擬出來各類地區IP,達到賺錢的目的。php

博主最近接到一單,編寫自動化撥號腳本,自動切換動態VPS的外網IP實現自動化網賺,今天就記錄下動態VPS的配置以及使用的姿式。html

機器

先看下機器的配置,客戶給過來的機器,配置以下:python

系統:Linuxweb

內存:512Mshell

CPU:單核(PS:博主好想去採購一批ARM單片機來作動態VPS啊,高收益低成本)vim

硬盤:10Gcentos

帶寬:2M服務器

因爲客戶一次性給過來的機器比較多,100多臺VPS,配置大致上都差很少,可是各個機器之間的差距很大,好比有的機器只有一個網卡,有的機器有好幾張網卡,有的機器裝的是Centos 6有的是Centos7,有的已經配置好撥號程序了,有的啥也沒有等等各類狀況都存在。網絡

配置

既然是要實現自動化撥號程序,天然是要先判斷下撥號程序在不在,Linux的撥號程序使用的是 pppoe-start 命令,注意,在不一樣的Linux裏面撥號程序可能不同,有的版本使用的是adsl-start命令,我這裏首先查看下系統的發行版本,使用以下命令ssh

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.1.1503 (Core)

看到返回了發行版本是centos 7.1,很是好,系統版本比較新,對腳本的支持也會更友好。

撥號命令pppoe-start在系統中已經自帶了,因此這裏我們直接使用pppoe-setup設置咱們的adsl撥號帳號和密碼,執行pppoe-setup命令後終端返回以下提示

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

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便可,直接回車,以後提示輸入adsl的密碼

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後回車整個adsl撥號就設置完成了,系統會給出一個提示

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-stop、pppoe-status,pppoe-status命令是查詢adsl撥號的狀態,自動化腳本須要經過這個命令查詢是否撥號成功。

配置完成以後我們就來測試下撥號是否成功,直接執行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.115.19 peer 10.2.254.254/32 scope global ppp0
       valid_lft forever preferred_lft forever

能正常獲取到ip地址則爲撥號成功,可是我這裏的ip地址很明顯是個內網地址,嘗試ping baidu.com看下能不能連上外網,發現並不能鏈接外網

ping: unknown host www.baidu.com

問題

既然沒法鏈接外網,那確定是有問題了,先去看下撥號的配置文件

[root@localhost ~]# cat /etc/ppp/pap-secrets
# Secrets for authentication using PAP
# client	server	secret			IP addresses
"adsluser"	*	"123456"
[root@localhost ~]# cat /etc/ppp/chap-secrets
# Secrets for authentication using CHAP
# client	server	secret			IP addresses
"adsluser"	*	"123456"

確認了adsl帳號密碼沒有問題,執行ifconfig。系統返回以下

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.14.40  netmask 255.255.255.0  broadcast 192.168.14.255
        inet6 fe80::215:5dff:fe32:ce6b  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:32:ce:6b  txqueuelen 1000  (Ethernet)
        RX packets 30860  bytes 4216610 (4.0 MiB)
        RX errors 0  dropped 10  overruns 0  frame 0
        TX packets 321  bytes 24455 (23.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 18  bytes 1778 (1.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 18  bytes 1778 (1.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ppp0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST>  mtu 1480
        inet 10.2.134.26  netmask 255.255.255.255  destination 10.2.254.254
        ppp  txqueuelen 3  (Point-to-Point Protocol)
        RX packets 10  bytes 102 (102.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 59 (59.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

確認pppoe撥號是正常的,可是ip卻顯示內網ip,這個時候就要判斷數據包走的是哪一個接口了,eth0是直接鏈接到內網的網關,ppp0是鏈接的外網,若是數據包一直走內網,天然是不通的,怎麼判斷數據包走的哪一個接口呢?使用route命令查看默認的路由表,系統返回

[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.14.234  0.0.0.0         UG    100    0        0 eth0
10.2.254.254    0.0.0.0         255.255.255.255 UH    0      0        0 ppp0
192.168.14.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0

顯示默認網關是192.168.14.234,明顯走的是內網,這樣數據包確定是出不去的,因此須要修改一下接口設置,使用vi編輯下eth0接口的配置,系統返回

UUID=d1440e96-0020-4ceb-9407-a722fecb96de
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
TYPE=Ethernet
DEFROUTE=yes
IPV6INIT=yes
IPV4_FAILURE_FATAL=no
PEERDNS=yes
NAME=eth0

IPADDR=192.168.14.40
NETMASK=255.255.255.0
GATEWAY=192.168.14.234
DNS1=8.8.8.8
DNS2=4.4.4.4

能夠看到 DEFROUTE=yes 這裏設置成了默認路由,我將這裏修改爲no,保存而後修改ppp0接口的設置,系統返回

USERCTL=no
BOOTPROTO=dialup
NAME=DSLppp0
DEVICE=ppp0
TYPE=xDSL
ONBOOT=yes
PIDFILE=/var/run/pppoe-adsl.pid
FIREWALL=NONE
PING=.
PPPOE_TIMEOUT=80
LCP_FAILURE=3
LCP_INTERVAL=20
CLAMPMSS=1412
CONNECT_POLL=6
CONNECT_TIMEOUT=60
DEFROUTE=yes
SYNCHRONOUS=no
ETH=eth0
PROVIDER=DSLppp0
USER=adsluser
PEERDNS=no
DEMAND=no

這裏的DEFROUTE選項已經設置爲yes了,因此不須要修改,重啓系統或者重啓網卡後再次執行route命令,返回以下

[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         0.0.0.0         0.0.0.0         U     0      0        0 ppp0
10.2.254.254    0.0.0.0         255.255.255.255 UH    0      0        0 ppp0
192.168.14.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0

默認接口已經修改爲了ppp0,嘗試ping www.baidu.com已經能夠正常ping通

[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (103.235.46.39) 56(84) bytes of data.
64 bytes from 103.235.46.39: icmp_seq=1 ttl=48 time=207 ms
64 bytes from 103.235.46.39: icmp_seq=2 ttl=48 time=214 ms
64 bytes from 103.235.46.39: icmp_seq=3 ttl=48 time=205 ms
64 bytes from 103.235.46.39: icmp_seq=4 ttl=48 time=205 ms
64 bytes from 103.235.46.39: icmp_seq=5 ttl=48 time=204 ms
64 bytes from 103.235.46.39: icmp_seq=7 ttl=48 time=203 ms
64 bytes from 103.235.46.39: icmp_seq=9 ttl=48 time=209 ms
64 bytes from 103.235.46.39: icmp_seq=10 ttl=48 time=202 ms
64 bytes from 103.235.46.39: icmp_seq=11 ttl=48 time=202 ms
64 bytes from 103.235.46.39: icmp_seq=12 ttl=48 time=202 ms

(PS:這個延遲我要給跪了)

若是以爲丟包嚴重或者網絡不暢,能夠嘗試修改DNS爲電信的114.114.114.114

至此,服務器已經可以正常的撥號鏈接外網了,接下來就是編寫自動化換ip的腳本。

腳本

自動撥號的腳本寫起來就很容易了,主要仍是用到了subprocess這個庫,用來執行系統的命令,下面貼出來代碼

#!/usr/bin/env python
# coding=utf-8
# code by 92ez.com

import subprocess
import time
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:
        print '[*] 第%s次撥號' % str(count)
        change_ip_for_vps()
        count += 1

測試代碼裏面用到了一些sleep來作延遲,實際操做中適當調整sleep的值來達到最優的效果。

效果

使用以前固然是要具有Python環境,下面是使用截圖(循環撥號測試)

深度截圖20170720160300.png - 大小: 113.43 KB - 尺寸: 422 x 634 - 點擊打開新窗口瀏覽全圖

 

注意

這裏有幾點須要額外注意:

首先,動態VPS服務器的性能比較差,畢竟價格便宜,一臺機器3到20塊,單核512M內存2M帶寬用來掛機也足夠,你很難想象你正在使用的機器是哪一個年代從機房淘汰下來的,系統自帶的軟件老舊,不到萬不得已,不要使用yum update 來更新系統軟件,這個操做可能會耗費你大量的時間。

首次登陸系統之後你可能會發現別人使用過的痕跡,因此第一件事就是修改ssh的登陸密碼,這種機器誰說的準呢。

第一次登陸進去須要安裝的軟件還挺多,下面列出一下最基本的軟件包:

若是你對vi不夠熟的話最好安裝下vim 使用 yum install vim -y 命令

固然,wget也是必備的軟件,使用 yum install wget -y 命令

python升級到最新版本 yum install python-devel -y

本文連接:https://www.92ez.com/?action=show&id=23447 
!!! 轉載請先聯繫non3gov@gmail.com受權並在顯著位置註明做者和原文連接 !!! 小黑屋  提示:技術文章有必定的時效性,請先確認是否適用你當前的系統環境。

相關文章
相關標籤/搜索