支线小小项目(pyautogui实现自动连接GP VPN)

用了pyautogui做了一个懒人小脚本,主要是在家上班,每天要连公司vpn, 然后还要等好久,公司用的GP(global protect) VPN, 长这个样子

主要问题每次点击connect后需要等他先出来windows NT的login认证,然后在等一会才出来RSA的login, 而RSA的秘钥要从这个里面获取,然后再填进去。

最后终于连上了,还要调整gateway,整个过程比较费时,所以做了一个小脚本,放在github上了,需要的可以去拿(麻烦star一下),然后改一改就可以实现你自己的小功能了。

源码如下:

# 这是一个分支项目
# pyautogui实现自动连接GP(global protect)import pyautogui as pg
import time
import os# Main steps:
# 1. find GP icon->left click-> if already connected -> check if no-split -> if yes -> exit, if not -> func(choose no-split).
# 2. if not connected/connect failed status --> go to func(connect)
# 3. wait for prompt of NT login window
# 4. func(sign in pwd) - click pic center - typewrite(pwd) - moveRel - click sign on
# 5. get RSA credential from func(rsa) - rsa copy(mvoeRel(0,75)) - ctrl+V
# 6. func(sign in rsa window) - check if cert_confirm - click ok
# 7. wait for GP connected status --> check if gp no-splitNT_PASSWD = 'Ballball15'
RSA_PASSWD = '10538185'def click_center(png, x_offset=0, y_offset=0, showed=True):"""Used to click the center of a picture and also provide following function:1. Use x_offset,y_offset to click somewhere around center position,2. Use "showed" flag to mark if need to wait for the ICON/PROMPT showup before click it.3. Return the center position(x,y)."""try:# first check if need to wait ICON/PROMPT showup# if ICON/PROMPT is there then get its positionif showed:x1, y1 = pg.locateCenterOnScreen(png)else:# if ICON/PROMPT is not there then waiting it showupnumber = 0while (not showed):showed = pg.locateCenterOnScreen(png)number += 1print(f'Waiting {png} for {number} times')x1, y1 = showed[0], showed[1]# click the center position or somewhere around itpg.click(x1+x_offset, y1+y_offset, button='left')# return back center positionprint(f'{png} position({x1},{y1})')return (x1, y1)except Exception as e:print(f'Cannot find {png} in your Screen.')def signin(temp=None):# mvoe mouse to input login passwd infox1, y1 = click_center('.\GP_login_signon.png', 0, -50, showed=False)# check if it is NT login or RSA loginif temp:pg.typewrite(temp)else:pg.hotkey('ctrl', 'v')# click "sign on" buttonpg.click(x1, y1, button='left', duration=0.5)time.sleep(2)# click the certification confirm dialogclick_center('.\cert_confirm.png', 0, 0, showed=False)def get_rsa(token):click_center('.\RSA_icon.png', 0, 0, showed=True)time.sleep(1)click_center('.\RSA_main.png', 0, 0, showed=True)pg.typewrite(token)pg.press('enter')# move to copy buttonclick_center('.\RSA_copy.png', 0, 0, showed=True)def choose_GP(x, y):# check if no-split is connectedif (pg.locateOnScreen('.\GP_nosplit.png')):print('No Split is connected! Enjoy it!')# if not then choose no-splitelse:print('changing gateway')# click the "Change Gateway"pg.click(x+100, y-95, button='left', duration=1)# go to the search boxpg.click(x+100, y-320, button='left', duration=1)pg.typewrite('no')# click the "no split"pg.click(x+100, y-292, button='left', duration=1)print('Done, enjoy it.')def connect_GP(x, y):# find and click "connect"pg.click(x+85, y-50, button='left')# time.sleep(5)# GP login with NTsignin(NT_PASSWD)get_rsa(RSA_PASSWD)# GP login with RSAsignin()if __name__ == '__main__':# check if GP icon is gray(notconnected or connectfailed)gray_gp = click_center('.\gray_GP1.png', 0, 0, showed=True) or click_center('.\gray_GP2.png', 0, 0, showed=True)blue_gp = click_center('.\GP_blue.png', 0, 0, showed=True)# if not connected then do connection flowif (gray_gp):gp_x, gp_y = gray_gp[0], gray_gp[1]connect_GP(gp_x, gp_y)time.sleep(10)click_center('.\GP_blue.png', 0, 0, showed=False)choose_GP(gp_x, gp_y)# if connected then check if no-split is choosenelif (blue_gp):# get GP icon position using blue icongp_x, gp_y = blue_gp[0], blue_gp[1]choose_GP(gp_x, gp_y)else:os.system('taskkill /IM PanGPA.exe /F')# time.sleep(6)pg.moveTo(1300, 1056)pg.moveRel(500, 0, duration=0.5)print('Cleared unknown status, please retry')

图片内容在GITHUB:

https://github.com/peanutfisher/GPAutoConnect.git
最终效果: (gif太大了,放张截图吧)



pyautogui的中文说明书参照下面

https://github.com/asweigart/pyautogui/blob/master/docs/simplified-chinese.ipynb
英文的是这个:
https://pyautogui.readthedocs.io/en/latest/quickstart.html#general-functions

pyautogui实现自动连接GP VPN相关推荐

  1. 禁用火狐浏览器的自动连接

    如何阻止 Firefox 在未经我许可的情况下自动连接 有些用户特别担心 Firefox 连接互联网的情况,尤其是当 Firefox 无缘无故地连接网络的时候(更多信息,请参看 Mozilla Fir ...

  2. linux 多路径重启,(linux多路径连接iScsi存储重启自动连接.docx

    (linux多路径连接iScsi存储重启自动连接 RHEL系统带有iSCSI initiator软件,下面描述如何配置iSCSI Initiator实现对ISCSI卷的访问Linux iscsi 介绍 ...

  3. 转-Android中自动连接到指定SSID的Wi-Fi

    最近在做一个项目,其中涉及到一块"自动连接已存在的wifi热点"的功能,在网上查阅了大量资料,五花八门,但其中一些说的很简单,即不能实现傻瓜式的拿来就用,有些说的很详细,但其中不乏 ...

  4. 1.1.1.1校园网_还担心断网?快试试清华校园网自动连接程序

    作为一名热爱科ban研zhuan的博士生,当我放假回家的时候,最挂念的就是实验室电脑上的文件和程序.在校外无法通过mstsc使用IP直连,只能通过第三方远程连接软件诸如Teamviewer.向日葵等来 ...

  5. Py:利用pyautogui实现自动将pdf文件(需手动设定pdf总页数)自动翻页并截取另存为图片形式,或自动隔0.1秒自动截笔记本全屏保存到指定文件夹

    Py:利用pyautogui实现自动将pdf文件(需手动设定pdf总页数)自动翻页并截取另存为图片形式,或自动隔0.1秒自动截笔记本全屏保存到指定文件夹 目录 实现步骤和结果 核心代码 实现步骤和结果 ...

  6. archlinux 开机自动连接wifi

    archlinux 开机自动连接wifi archlinux 开机后连接wifi很简单 利用wifi-menu命令就可以选择热点和输入密码 但是重启后又要再来一次. 如果安装了gui, 当然可以利用N ...

  7. iphone无法连接电脑_电脑开机无线网卡无法自动连接网络怎么办?

    当电脑时常出现无法连接网络或者开机的时候无线网卡无法自动连接网络的情况,一般排除网络的情况之外就是电脑系统本身设定没有设定好. 这种情况怎么解决呢?小编教您在win7和win10的系统下如何进行设定好 ...

  8. STM32开发 -- 自动连接一个未保存过的WIFI

    如需转载请注明出处:https://blog.csdn.net/qq_29350001/article/details/90695656 上一篇文章 STM32开发 – WIFI那些事儿(一) 简单介 ...

  9. boost::signals2模块实现使用自动连接管理的测试程序

    boost::signals2模块实现使用自动连接管理的测试程序 实现功能 C++实现代码 实现功能 boost::signals2模块实现使用自动连接管理的测试程序 C++实现代码 #include ...

最新文章

  1. Asp.Net 之 通过调用 WScript.Shell 启动本地 exe 程序时产生“ automation服务器不能创建对象 ”的错误...
  2. Kubernetes之路 1 - Java应用资源限制的迷思
  3. sympy科学计算器
  4. ThreadPoolExecutor 中的workerDone(this); 为什么会时不时的就会在这个地方停下来???
  5. java 上调下移_java – 在调整框架大小时,JLabel的位置会更...
  6. 图像处理之基础---大话小波和卷积
  7. 解决linux下访问https站点问题
  8. mysql日期为00_MySQL 8.0.13设置日期为0000-00-00 00:00:00时出现的问题解决
  9. 【Allwinner】---全志GPIO号 计算 最新最详细
  10. Web前端程序员简历
  11. C#/VB.NET 如何旋转PDF页面
  12. Ruby语言的特别之处
  13. 面向对象编程训练:模拟购物车模块(:3[▓▓]快醒醒开学了
  14. 关于丢番图方程x^2+y^2=n
  15. 杰里混响调MIC增益和深度【篇】
  16. Playwright 中文文档
  17. 艾永亮:回力鞋生死的92年,风靡全球后没落,如今成功逆袭
  18. short 在JAVA_short在java中是什么类型的
  19. 在 PostgreSQL 中简单实现 Insert ignore 操作
  20. 检测浏览器是否开启硬件加速

热门文章

  1. IE浏览器提示是否停止运行此脚本的解决方法
  2. android局域网通信方案,Android基于TCP的局域网聊天通信
  3. 信用评级模型_不寻常的方式来提高您的信用评分
  4. 巡风 win7 python3.7安装使用记录
  5. Vue简单入门学习-----day1-基础用法
  6. 腾讯arttemplate模板官方介绍
  7. CFT Show 信息收集篇
  8. Matrix Hub 矩阵运算库 C语言
  9. 信息系统监理与审计 我国实践与美国的经验
  10. 关于三星手机刷机,以GTN8000为例