吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6655|回复: 65
收起左侧

[Python 原创] 自动连接校园网-自动登录EasyConnect

  [复制链接]
xdxgkxq 发表于 2022-11-15 16:00

作为一个大学牲在校外访问教务处等校内网站时需要使用软件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

免费评分

参与人数 6吾爱币 +6 热心值 +6 收起 理由
52bbQ + 1 谢谢@Thanks!
MAOSKE + 1 谢谢@Thanks!
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
金色枫叶 + 1 + 1 谢谢@Thanks!
zhinians + 1 热心回复!
qaz2wsx + 1 不会用

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

zjcasdfghjkl 发表于 2022-11-25 00:11
wkdxz 发表于 2022-11-15 20:28
哪里  这个已经很好了  有个库pyautogui 能很快的实现键鼠点击,执行快捷键也很方便

pywin32和pyautogui都可以实现键鼠点击,python的库真是丰富,感觉对python更有信心了
 楼主| xdxgkxq 发表于 2022-11-15 21:28
wkdxz 发表于 2022-11-15 20:28
哪里  这个已经很好了  有个库pyautogui 能很快的实现键鼠点击,执行快捷键也很方便

谢谢大佬指点,我去看看
Aerberter 发表于 2022-11-15 16:12
chen297 发表于 2022-11-15 16:13
谢谢分享
lhp462 发表于 2022-11-15 16:14
楼主是否考虑过研究一下协议,如果可以脱离模拟点击就更好了,不过也很棒了
kurage7 发表于 2022-11-15 16:14
好耶!感谢楼主分享
lemon123654 发表于 2022-11-15 16:14
这个挺实用的啊
yifenmanyufan 发表于 2022-11-15 16:15
非常实用对于学校学生
JYY1 发表于 2022-11-15 16:15
谢谢分享,学习了学习了
TearApart 发表于 2022-11-15 16:17
感谢楼主分享!!!
gyz2005011 发表于 2022-11-15 16:17
学习,谢谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-6 17:31

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表