内网外网同时使用的情况需要修改路由route ,但太麻烦,有时也不一定成功。其实只需要将其中一个网卡关闭,就可以使用另外一个网络。上程序,是不是很简单。
import os
import time
ret = os.system("ping baidu.com -n 1")
if ret == 0:
print("OK")
show_cmd = 'netsh interface show interface'
disable_cmd = 'netsh interface set interface 以太网 disabled' #关闭网卡
os.system(disable_cmd)
print("已成功关闭以太网·!可以使用内网")
time.sleep(5)
else:
print("NO")
disable_cmd = 'netsh interface set interface 以太网 enabled' # 恢复网卡
os.system(disable_cmd)
print("已成功打开以太网·!可以使用互联网")
time.sleep(5)
具体DOS命令如下:
禁用无线网卡:netsh interface set interface wlan0 disabled
启用无线网卡:netsh interface set interface wlan0 enabled
禁用有线网卡:netsh interface set interface eth0 disabled
启用有线网卡:netsh interface set interface eth0 enabled