自动登录EasyConnect

文章正文
发布时间:2025-08-19 20:57

作为一个大学牲在校外访问教务处等校内网站时需要使用软件EasyConnect连接校园vpn,但是没有记住密码的功能,每次都需要手动输入账号密码,所以就用python写了点,通过模拟鼠标键盘自动点击来实现自动登录。(不是CS专业,学点pthon只是点兴趣,只会点皮毛,有错误还烦请大佬指正)
导入库:

from cmath import inf from ctypes import * from telnetlib import PRAGMA_HEARTBEAT import win32gui import win32api import win32con import os from time import sleep

自己的登录账号与密码保存在代码所在目录的login.txt文件夹中
读取账号与密码:

def get_info():     with open("login.txt") as lg:         info = lg.readlines()         info[0] = info[0].strip()         return info def Easyconnect():     '''打开应用'''     dir = "C:\\Program Files (x86)\\Sangfor\\SSL\\SangforCSClient\\SangforCSClient.exe"     os.startfile(dir)     sleep(5) def setpos(h):     '''根据句柄设置鼠标坐标至窗体中央'''     left, top, right, bottom = win32gui.GetWindowRect(h)     pos = [int((left + right)/2), int((top + bottom)/2)]     win32api.SetCursorPos(pos) def get_son_windows(parent):     hWnd_child_list = []     win32gui.EnumChildWindows(         parent, lambda hWnd, param: param.append(hWnd), hWnd_child_list)     return hWnd_child_list

分别模拟鼠标与键盘点击:

def mouse():     '''模拟鼠标点击'''     win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,                          0, 0, 0, 0)     win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,                          0, 0, 0, 0)     sleep(0.08) def keyboard(val):     '''模拟键盘点击'''     win32api.keybd_event(val, 0, 0, 0)     win32api.keybd_event(val, 0, win32con.KEYEVENTF_KEYUP, 0)     sleep(0.02)

将从txt读入的字符转换为对应键位代码方便模拟点击:
因为我们学校的密码固定只有@字符,所以只对@做了特殊处理,其他像!#¥%这种没有考虑。所以只能正常处理小写英文字母、数字和@,其他会出错。

def inputchar(blank):     for c in blank:         print(ord(c))         if (c == '@'):             win32api.keybd_event(16, 0, 0, 0)             win32api.keybd_event(50, 0, 0, 0)             win32api.keybd_event(50, 0, win32con.KEYEVENTF_KEYUP, 0)             win32api.keybd_event(16, 0, win32con.KEYEVENTF_KEYUP, 0)         c = ord(c)         if ('a' <= chr(c) <= 'z'):             c -= 32         keyboard(c)

查找软件窗口:

hwnd_title = dict() def get_all_hwnd(hwnd, a):     if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):         hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)}) def get_handle():     win32gui.EnumWindows(get_all_hwnd, 0)     for h, t in hwnd_title.items():         if (t == "EasyConnect"):             return h

运行部分:

if __name__ == "__main__":     info = get_info()     Easyconnect()     ec_handle = get_handle()     sonlist = get_son_windows(ec_handle)     for i, h in enumerate(sonlist):         if (i == 22):             setpos(h)             mouse()             inputchar(tuple(info[0]))         elif (i == 24):             setpos(h)             mouse()             inputchar(tuple(info[1]))             keyboard(13)             break