欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

VPN拨号

程序员文章站 2022-07-14 08:38:41
...

windows下vpn拨号:

import os
from time import sleep

class VPNHelper(object):
    def __init__(self, _vpnIP, _userName, _passWord, WinDir=r"C:\Windows\System32", RasDialFileName=r'\rasdial.exe'):
        self.IPToPing = _vpnIP
        self._VPNName = _vpnIP;
        self._UserName = _userName;
        self._PassWord = _passWord;
        self._WinDir = WinDir
        self._RasDialFileName = RasDialFileName
        self._VPNPROCESS = self._WinDir + self._RasDialFileName

    def TryConnectVPN(self):
        try:
            command = self._VPNName + " " + self._UserName + " " + self._PassWord
            os.system(self._VPNPROCESS + " " + command)
            return 1
        except:
            print("----VPN连接失败!---")
            return 0

    def TryDisConnectVPN(self):
        try:
            command = self._VPNName + " /d"
            os.system(self._VPNPROCESS + " " + command)
        except:
            print("---VPN断开失败!---")

    def Restart(self, waitingTime=0):
        import time
        self.TryDisConnectVPN()
        time.sleep(waitingTime)
        status=self.TryConnectVPN()
        while(status==0):
            status = self.TryConnectVPN()
        time.sleep(1.5)
def changIP():
    vpn = VPNHelper("地址", "账号", "密码")
    vpn.Restart(5)
    sleep(6)

 

Linux下pptp拨号(先安装pptp):

 

def changIP():
    o = 'F'
    while o =='F':
        try:
            print("关闭拨号器!")
            #subprocess.getoutput('sudo pptpsetup --delete laoying')
            #sleep(0.5)
            pkill=subprocess.getoutput('sudo pkill -9 pptp')
            sleep(1)
            print('---1---',pkill)
            setuped=subprocess.getoutput('sudo pptpsetup --create laoying --server 地址 --username 账号--password 密码 --start')
            sleep(3)
            print('----2----',setuped)
            pon=subprocess.getoutput('sudo pon laoying')
            print('---3---',pon)
            sleep(2)
            newvpn = subprocess.getoutput('sudo route add default dev ppp0')
            print('----4--',newvpn)
            sleep(6)
            if pon != '' or newvpn!='':
                o = 'F'
                continue
            print("拨号成功!")
            return
        except:
            o = 'F'
            print('拨号出错!')
            sleep(0.5)

#if __name__=='__main__':
 #    changIP()

 

相关标签: 爬虫