Advertisement
allan

VPN Users Created After Date

Dec 25th, 2023 (edited)
1,425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | Source Code | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # config.py with api_server, user_name, and password variables
  4. import config
  5.  
  6. # https://github.com/CheckPointSW/cp_mgmt_api_python_sdk
  7. import cpapi
  8. import time
  9.  
  10. # search for VPN users created after this date
  11. date_cutoff = "20200301"
  12.  
  13. # save account names to file
  14. output_file = "output.csv"
  15.  
  16.  
  17. query_parameters = {"command": "show-generic-objects",
  18.                     "details_level": "full",
  19.                     "payload": {
  20.                         "class-name": "com.checkpoint.objects.classes.dummy.CpmiUser",
  21.                         "limit": 50,
  22.                         "offset": 0
  23.                         }
  24.                     }
  25.  
  26.  
  27. cutoff = time.mktime(time.strptime(date_cutoff, "%Y%m%d"))
  28.  
  29. client_args = cpapi.APIClientArgs(server=config.api_server)
  30.  
  31. with open(output_file, "w") as f:
  32.     f.write("\"login name\",\"create date\"\n")
  33.  
  34.     with cpapi.APIClient(client_args) as client:
  35.         if client.check_fingerprint() is False:
  36.             print("Server fingerprint does not match.")
  37.             exit(1)
  38.  
  39.         login_response = client.login(config.user_name, config.password, read_only=True)
  40.         if login_response.success is False:
  41.             print("Server connection failed.")
  42.             exit(2)
  43.  
  44.         keep_looping = True
  45.         user_count = 0
  46.  
  47.         while keep_looping:
  48.             query_result = client.gen_api_query(**query_parameters)
  49.  
  50.             page = query_result.__next__()
  51.             if page.success and page.res_obj["data"]["total"] > 0:
  52.  
  53.                 # start from the last user on this page
  54.                 query_parameters["payload"]["offset"] += len(page.data["objects"])
  55.  
  56.                 for item in page.data["objects"]:
  57.                     sec, msec = divmod(item["meta-info"]["creationTime"], 1000)
  58.  
  59.                     if sec > cutoff:
  60.                         user_count += 1
  61.  
  62.                         f.write("\"{0}\",{1}\n".format(
  63.                             item["name"], time.strftime("%m-%d-%Y", time.localtime(sec))
  64.                         ))
  65.             else:
  66.                 # api error or
  67.                 # exhausted all users in database
  68.                 keep_looping = False
  69.         print("Total users: ", user_count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement