linux pptp client managment
需要從某網站頻繁獲取數據,但目標網站對IP限制嚴格,於是想到用vpn。市面上(淘寶)有種每次連接都會自動切換IP的PPTP VPN,很符合需求,下面就是怎麼用了,環境是CentOS6。
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 | #安裝pptp yum install pptp pptp-setup #配置pptp pptpsetup --create v --server vpn.server.address --username USERNAME --password PASSWORD #淘寶的這種pptp vpn一般是不用加密設置的,萬一需要,可以在配置命令最後加--encrypt參數 #為了方便後續使用,我們複製兩個命令 cp /usr/share/doc/ppp-2 .4.5 /scripts/pon /usr/sbin/ cp /usr/share/doc/ppp-2 .4.5 /scripts/poff /usr/sbin/ chmod +x /usr/sbin/pon chmod +x /usr/sbin/poff #啟動pptp pon v #如果啟動成功路由表中應該會出現一個ppp0的設備 route -n #如果沒出現可以查看那裡出了問題 tail -n 10 /var/log/messages | grep ppp #這是如果把網關設置到ppp0所有流量就都走vpn了\ #但是啊,你將失去ssh連接,很恐怖吧\ #這時最好有服務器的網頁端供你連到內網進行操作\ #當然,重啟服務器也能恢復連接 #下面設置路由表 /sbin/route del -net default /sbin/route add -net default dev ppp0 #此時所有流量就pptp了,通過下面命令查看 curl http: //myip .dnsdynamic.org/ #執行需要pptp的ip完成的任務 #關閉vpn poff #理論上此時應該恢復之前的路由表 #但我偷了個懶,重啟了一下網絡 /etc/init .d /network restart #從pon到重啟網絡,就完成了一個工作循環。 |
使用php腳本完成pptp的切換,其實就是php調用bash,兩點:運行用戶為root;命令最好寫相對路徑。
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | #!/usr/bin/php <?php class HttpClient{ private $ch ; function __construct( $cookie_jar ){ $this ->ch = curl_init(); curl_setopt( $this ->ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0' ); curl_setopt( $this ->ch, CURLOPT_TIMEOUT, 40); curl_setopt( $this ->ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt( $this ->ch, CURLOPT_AUTOREFERER, true); curl_setopt( $this ->ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt( $this ->ch, CURLOPT_COOKIEJAR, $cookie_jar ); curl_setopt( $this ->ch, CURLOPT_COOKIEFILE, $cookie_jar ); } function __destruct(){ curl_close( $this ->ch); } final public function setReferer( $ref = '' ){ if ( $ref != '' ){ curl_setopt( $this ->ch, CURLOPT_REFERER, $ref ); } } final public function Get( $url , $header =false, $nobody =false){ curl_setopt( $this ->ch, CURLOPT_POST, false); curl_setopt( $this ->ch, CURLOPT_URL, $url ); curl_setopt( $this ->ch, CURLOPT_HEADER, $header ); curl_setopt( $this ->ch, CURLOPT_NOBODY, $nobody ); return curl_exec( $this ->ch); } final public function Post( $url , $data = array (), $header =false, $nobody =false){ curl_setopt( $this ->ch, CURLOPT_URL, $url ); curl_setopt( $this ->ch, CURLOPT_HEADER, $header ); curl_setopt( $this ->ch, CURLOPT_NOBODY, $nobody ); curl_setopt( $this ->ch, CURLOPT_POST, true); curl_setopt( $this ->ch, CURLOPT_POSTFIELDS, http_build_query( $data )); return curl_exec( $this ->ch); } } function getNewIP(){ $logIPFile = "/home/logIP.txt" ; $res = array (); exec ( "/usr/sbin/pon v" , $res ); sleep(3); exec ( "/sbin/route del -net default" , $res ); sleep(2); exec ( "/sbin/route" , $res ); sleep(6); exec ( "/sbin/route add -net default dev ppp0" , $res ); // var_dump($res); sleep(1); // 需要一個cookie文件,創建一個空文件即可 $http = new HttpClient( '/home/dump.txt' ); sleep(1); file_put_contents ( $logIPFile , date ( "Y-m-d H:i:s" , time()). "_" . $ip .PHP_EOL , FILE_APPEND | LOCK_EX); $ips = array (); exec ( "tail -n 2 '$logIPFile'" , $ips ); $isNewIP =0; foreach ( $res as $oldIP ){ if ( strpos ( $oldIP , $ip ) != false){ $isNewIP = $isNewIP +1; } } return $isNewIP ; } function restoreIP(){ exec ( "/usr/sbin/poff" , $res ); // var_dump($res); sleep(2); exec ( "/etc/init.d/network restart" ); } //如果IP沒有重複兩次以上就執行任務 if ( getNewIP( $logIPFile ) <2 ){ // doYourOwnStaff(); } restoreIP(); exit (); ?> |
crontab的設置,這裡不用crontab -e,而是直接編輯/etc/crontab:
1 2 3 | nano /etc/crontab #指定運行命令的用戶為root 03 * * * * root /path/to/the/script |
這樣就完工了,感謝CentOS 6下配置PPTP VPN客户端和在 Linux 命令列進行 PPTP VPN 連線。在完全不了解linux路由表和iptables的情況下,我也只能做到如此了。其實理想狀態是只轉發http和https到pptp,有空再研究吧,新年快樂!
由於政策原因,動態pptp沒那麼好買了。於是年後換了一批靜態pptp,做法就是新建多個pptp配置,然後隨機取。
1 2 3 | $vpns = array ( "v47" , "v11" , "v18" , "v12" ); $rand_keys = array_rand ( $vpns , 2); $vpn = $vpns [ $rand_keys [0]] ; |
在設置過程中,遇到了unknown authentication type 26; Naking錯誤,解決辦去掉/etc/ppp/options.pptp文件中的require-mppe-128的注釋,並在修改配置文件為如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # written by pptpsetup pty "pptp SERVER --nolaunchpppd" lock noauth refuse-pap refuse-eap refuse-chap refuse-mschap nobsdcomp nodeflate require-mppe-128 name MYUSERNAME remotename CONFNAME ipparam CONFNAME |
另一種可能的錯誤是連接的時候出現LCP: timeout sending Config-Requests,我是通過chkconfig iptables off關閉iptables解決的,反正我在內網。
上文中的/etc/init.d/network restart有時並不能恢復本機網絡,此時我們可以替換腳本中的這一句為
1 2 | #網關改回原來的網關 subprocess.call( '/sbin/route add default gw 172.16.2.1 netmask 0.0.0.0 dev eth0' , shell = True ) |
linux l2tp client managment
時間來到2018年,pptp換成了l2tp,發現同樣的需求使用NetworkManager真是簡單。設置好l2tp連接後,一行命令即可打開關閉連接。
1 2 3 4 5 6 | #passwd-file只需配置一行PSK vpn.secrets.password:YOUR_PSK #打開l2tp連接 subprocess.call('nmcli con up l2tp76 passwd-file /home/42/vpnpass.txt', shell=True) #關閉l2tp連接 subprocess.call('nmcli con down l2tp76', shell=True) |
本文更新於 2018/11/09。