惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 【当耐特】
博客园_首页
博客园 - Franky
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
Recorded Future
Recorded Future
H
Hackread – Cybersecurity News, Data Breaches, AI and More
F
Fortinet All Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
M
MIT News - Artificial intelligence
D
Docker
H
Help Net Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
S
SegmentFault 最新的问题
腾讯CDC
Latest news
Latest news
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
I
InfoQ
美团技术团队
C
Cybersecurity and Infrastructure Security Agency CISA
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
C
Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Schneier on Security
Spread Privacy
Spread Privacy
Recent Announcements
Recent Announcements
T
Threat Research - Cisco Blogs
F
Full Disclosure
T
Threatpost
T
Tenable Blog
AWS News Blog
AWS News Blog
Cloudbric
Cloudbric
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
W
WeLiveSecurity
I
Intezer
月光博客
月光博客
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
Lohrmann on Cybersecurity
Hacker News - Newest:
Hacker News - Newest: "LLM"

博客园 - SCPlatform

CPU封装技术介绍 openssl unicode编译以及VC++2015环境下的问题 重新开启此博 MIFARE Classic S50技术详解 Mifare简介 如何获取JavaCard栈信息 JavaCard应用开发环境 如何获取JavaCard剩余空间 如何统合分析一张JavaCard BER-TLV数据结构 智能卡中的数据结构 加密解密算法 SCPlatform博客文章总目录 C#实现简体中文和繁体中文的转换 QueryString 整站过滤 关于脚本注入的问题 开发Blog需注意的Blog基本特征和功能要素 java 学习步骤 CodeSmith 实体生成模板(C#版)
windown 使用python 自动切换网络
SCPlatform · 2019-12-03 · via 博客园 - SCPlatform

由于工作环境的特殊性,有时需要切换到内网工作,有时需要通过手机连接外网,想自动运行一个脚本就做完这事

首先在PC机上安装一个无线网卡,例如360wifi就可以。

除了python,还用到了DOS命令,事实我觉得可以直接通过python来实现,有时间再试试。

以下是实现代码:

import os
import time
import sys
import socket
import subprocess

NET_WLAN_NAME = "WLAN"
NET_LOCAL_NAME = "本地连接"

def operate_network_adapter(adaptername, toopen):
    processx = None
    try:
        cmd = "netsh interface set interface \"%s\" admin=%s" %(adaptername, "disabled" if toopen==False else "enabled")
        processx = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, creationflags=0x08000000)
        
        processx.wait()
        if processx.returncode == 0:  
            print('\n cmd \"%s\" success.\n' %cmd)  
        else:  
            print('\n cmd \"%s\" error.\n' %cmd)  
        processx.kill()
        
        pass
    except:
        print('\n%s\n' %(sys.exc_info()[1]))
        pass
    finally:
        if processx != None:
            processx.kill()  
        pass
    pass
    
if __name__ == "__main__":
    process = None

    try:
        myname = socket.getfqdn(socket.gethostname())
        myaddr = socket.gethostbyname(myname)
        
        if '127.0.0.1' not in myaddr:
            #open local network
            operate_network_adapter(NET_WLAN_NAME, False)
            time.sleep(2)
            operate_network_adapter(NET_LOCAL_NAME, True)
        else:
            #open wlan
            operate_network_adapter(NET_LOCAL_NAME, False)
            time.sleep(2)
            operate_network_adapter(NET_WLAN_NAME, True)
           
        pass
    except:
        print('\n%s\n' %(sys.exc_info()[1]))
        pass
    finally:
        pass