centos系统宽带拨号pppoe自动更换IP脚本

自动拨号的脚本写起来就很容易了,主要还是用到了subprocess这个库,用来执行系统的命令,下面贴出来代码:

#!/usr/bin/env python
# coding=utf-8

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(50)
        subprocess.Popen('pppoe-start', shell=True, stdout=subprocess.PIPE)
        time.sleep(500)
        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环境,下面是使用截图(循环拨号测试):

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

super_ip_

你的打赏将是我的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值