Skip to content

Instantly share code, notes, and snippets.

@Leask
Created February 21, 2017 06:57
Show Gist options
  • Select an option

  • Save Leask/4fcce4436bcf3778fa4faecf832073fc to your computer and use it in GitHub Desktop.

Select an option

Save Leask/4fcce4436bcf3778fa4faecf832073fc to your computer and use it in GitHub Desktop.
Check and reset the default route.
# -*- coding:utf-8 -*-
import commands
import types
import os
import time
import re
#commands.getstatusoutput('wpa_supplicant -D wext -i wlan0 -B -c /etc/wpa_supplicant/wpa_supplicant.conf')
#commands.getstatusoutput('dhclient wlan0')
pattern = re.compile(r'\d+.\d+.\d+.\d+')
os.system('wvdial &')
while 1:
(status,output)=commands.getstatusoutput("ifconfig ppp0 | grep 'inet addr' | awk '{ print $2}' | awk -F: '{print $2}'")
match = pattern.match(output)
if match:
ip =output
#print ip
(status,output)=commands.getstatusoutput('ping -c 3 www.baidu.com')
#print status
if status !=0:
os.system('route delete default')
#commands.getstatusoutput('route add default gw 192.168.12.1')
commands='route add default gw %s' % (ip)
#print commands
os.system(commands)
break
if status==0:
print 'ok'
break
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment