手动调节,内网外网同时使用

当需要在内网和外网间切换时,可以使用Python脚本来控制网卡的启用和禁用状态。脚本首先通过ping命令检测网络连接,然后使用netsh命令禁用或启用指定的网络接口,如以太网或无线网卡,以实现网络的切换。这个方法避免了手动修改路由的复杂性。
摘要由CSDN通过智能技术生成

内网外网同时使用的情况需要修改路由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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值