Monday 23 September 2024

GlobalProtect VPN login automation in an enterprise client -- stupid but simple python code

'''
This code is provided "as-is" without any express or implied warranties or guarantees. 
The author of this code assumes no responsibility or liability for any errors, omissions, 
or damages resulting from its use. 
It is the user’s responsibility to determine the suitability of this code 
for their specific needs, 
including but not limited to ensuring compliance with applicable laws and regulations.
The author is not liable for any abuse, misuse, or misapplication of this code. 
Users are solely responsible for maintaining and updating the code as required. 
By using this code, you agree to indemnify, defend, 
and hold harmless the author from any claims, damages, or liabilities arising from its use.
''' 
'''
The piece of code is generally able to automate the login vpn process. The capability makes
me confused for a while that sometimes, certain level corporate security configuration 
disables the usage of keyboard by library like pyautogui etc. Any way, it works in my
employer. May be you want to further check the window visibility to make the script 
works for stable. 
''' 
import pyautogui
import time
import sys
from pywinauto.application import Application
from pywinauto import Desktop
def call_method_if_exists(obj, method_name):
# Step 1: Check if the object exists
if obj is None:
return 'None object ' # Return empty string if the object is None
# Step 2: Check if the object has the method with the given name
if hasattr(obj, method_name):
method = getattr(obj, method_name)
# Step 3: Check if it's a callable method and takes no parameters
if callable(method) and method.__code__.co_argcount == 1:
# Call the method and return its value
return method()
else:
return 'not callable ' + method_name # Return empty string if the method has parameters
else:
return 'No attr ' + method_name # Return empty string if the method doesn't exist

time.sleep(0.5)
pyautogui.hotkey('win')
time.sleep(0.5)
pyautogui.write('globalprotect', interval=0.1)
pyautogui.press('enter')
time.sleep(0.5)

pyautogui.keyDown('enter')
pyautogui.keyUp('enter')
time.sleep(1)

# backend possibilities ula, wind32... May be do not use any parameter?
wins = Desktop(backend='').windows()

user_input = None
pass_input = None
sign_in_button = None
input_hold1 = None
input_hold2 = None
i = 0
for w in wins:
i = i + 1
s = w.window_text()
if s.find('GlobalProtect Login') >= 0:
print("---", i)
print(w.window_text())
controls = w.descendants()
j = 0
  for control in controls:
j = j + 1
control_type = call_method_if_exists(control, 'control_type')
win_text = call_method_if_exists(control, 'window_text').lower()
contrl_name = call_method_if_exists(control, 'name')
auto_id = call_method_if_exists(control, 'automation_id')
help_text = call_method_if_exists(control, 'help_text')
# print("i, j: ", i, '--', j, ", control_type: ", control_type, ', win_text', win_text, ',contrl_name: ', contrl_name, ', auto_id:', auto_id, ', help_text: ', help_text)
if j == 2:
input_hold1 = control
# if j == 2:
# input_hold2 = control
if win_text == 'sign in' or win_text == 'sign in ':
sign_in_button = control
user_input = input_hold1
pass_input = input_hold2
print("====Input user name and password now===i, j: ", i, ",", j)
user_input.set_focus()
time.sleep(1)
user_input.type_keys('user_name')
time.sleep(1)
pyautogui.keyDown('tab')
pyautogui.keyUp('tab')
time.sleep(1)
pyautogui.write('password!', 0.1)
time.sleep(1)
pyautogui.keyDown('tab')
pyautogui.keyUp('tab')
time.sleep(1)
print("===Username and password input done ===")
# sign_in_button.click() # the button is not visible. click_input() does not work as well.
pyautogui.keyDown('enter')
pyautogui.keyUp('enter')
# time.sleep(1)
# pyautogui.press('escape')
print("sign_in_button called click_input. shall login to VPN")
sys.exit()

No comments: