美文网首页
2022-08-03网卡切换

2022-08-03网卡切换

作者: XH_hector | 来源:发表于2022-08-03 17:28 被阅读0次

    思路

    公司内网、外网不允许同时使用,经常的切换。
    内网是有线连接,外网是通过无线网卡连接。
    连接内网时,无线网卡需要禁用。
    连接外网时,内网网卡需要禁用。
    自动切换,内网环境运行后变成外网,外网环境运行连接内网。

    代码

    import os
    import pywifi
    from pywifi import const,Profile
    wifi = pywifi.PyWiFi()
    ifaces = wifi.interfaces()[0]
    wifistatus = ifaces.status()
    if wifistatus == const.IFACE_DISCONNECTED:
        os.popen('netsh interface set interface "oa" admin=DISABLE')#禁用内网网卡,"oa"为网卡名称
        profile = pywifi.Profile()
        profile.ssid = '****' # 无线网名称
        profile.auth = const.AUTH_ALG_OPEN
        profile.akm.append(const.AKM_TYPE_WPA2PSK)
        profile.key = '****'# 无线网密码
        tep_profile = ifaces.add_network_profile(profile)
        ifaces.connect(tep_profile)
        print("连接无线网")
    else:
        ifaces.disconnect()#断开无线网
        os.popen('netsh interface set interface "oa" admin=ENABLE')#启用内网网卡
        print("连接内网")
    

    相关文章

      网友评论

          本文标题:2022-08-03网卡切换

          本文链接:https://www.haomeiwen.com/subject/olcewrtx.html