Python:自动检测是否双跨并断开wifi

前言

双跨,也就是连接有线网的时候还连接着wifi。在双跨的情况下,如果黑客通过wifi进(外网)攻的话,有线网(内网)就有可能被侵入,存在安全问题。
所以使用Python开发了一个小工具,其中部分代码由公司一位大师提供,我在其基础上进行了优化。

注意点

1、因为最后是通过pyinstaller -F -w来打包的,而-w是取消(不是隐藏)DOS窗口,所以如果代码中有input、print等需要使用dos窗口的命令就会导致运行出错(failed to excute script)。在本脚本中,虽然没有input和print,但是有tkinter.messagebox.showinfo(),虽然加了“st.wShowWindow=subprocess.SW_HIDE”,但实际上还是要使用到DOS窗口。
为了解决这个问题,所以在tkinter.messagebox.showinfo()中增加了一些参数:

cmd1 = 'netsh interface show interface 以太网'
p = subprocess.check_output(cmd1, shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE)

这样就既能-w打包,又能正常运行了。

2、最后第二行“tkinter.messagebox.showinfo()”其实本来就是一个循环,所以当弹出窗口后会卡死,而且关闭后整个程序也关闭了。一开始我加了break,但这样一来整个程序也就结束了。再后来我就改成了root.destroy(),也就是关闭tkinter,这样循环继续,也解决了窗口卡死的问题。

3、使用pywifi达到了检测、关闭wifi的目的。

4、增加了“自动检测是否已经加入启动项,如果没有的话就自动加入”的代码。

5、如果使用exe程序的话,必须要用管理员模式,否则会失败。
在这里插入图片描述

代码

# -*- coding:utf-8 -*-
import subprocess
import time
from pywifi import PyWiFi, const
import tkinter
import tkinter.messagebox
import psutil
import sys
import win32api
import win32con

def GetAutoValues(name='double_kua'):
    '''判断是否有加入开机启动'''
    key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, 'Software\\Microsoft\\Windows\\CurrentVersion\\Run', 0, win32con.KEY_READ)
    info = win32api.RegQueryInfoKey(key)
    for i in range(0, info[1]):
        ValueName = win32api.RegEnumValue(key, i)
        if name == ValueName[0]:
            return 1
    win32api.RegCloseKey(key)
    return 0

def WriteAutoValues(name='double_kua'):
    '''自动加入开机启动'''
    path = os.path.abspath('.') + '\\'+ '自动检测双跨并断开wifi.exe'
    KeyName = 'Software\\Microsoft\\Windows\\CurrentVersion\\Run'
    key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, KeyName, 0, win32con.KEY_ALL_ACCESS)
    win32api.RegSetValueEx(key, name, 0, win32con.REG_SZ, path)
    win32api.RegCloseKey(key)

def check_pid_num(pid_name):
    '''检测进程数量'''
    n = 0
    pids = psutil.pids()
    for pid in pids:
        p = psutil.Process(pid)
        if p.name() == pid_name:
            n +=1
    return n

pid_name = '自动检测双跨并断开wifi.exe'
st=subprocess.STARTUPINFO
st.dwFlags=subprocess.STARTF_USESHOWWINDOW
st.wShowWindow=subprocess.SW_HIDE

# 设置开机自动启动
if not GetAutoValues():
    WriteAutoValues()

#如果已经打开的话就关闭程序
if check_pid_num(pid_name) > 2:
    tkinter.messagebox.showinfo('告警', '已经打开过了!')
    sys.exit()

#开始循环运行
while True:
    cmd1 = 'netsh interface show interface 以太网'
    p = subprocess.check_output(cmd1, shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
    ss=str(p,'GBK')
    time.sleep(1)
    if (ss.find(u"已连接"))>0:
        cmd2 = 'netsh interface show interface wlan'
        p = subprocess.check_output(cmd2, shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
        ss1=str(p,'GBK')
        if(ss1.find(u"已连接")) >0:
            wifi = PyWiFi()  # 创建一个无线对象
            ifaces = wifi.interfaces()[0]  # 获取第一个无线网卡
            ifaces.disconnect()
            root = tkinter.Tk()
            tkinter.messagebox.showinfo('告警', '发生双跨,即将断开wifi!')
            root.destroy()

猜你喜欢

转载自blog.csdn.net/weixin_42029733/article/details/106137894