|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +"""This script is used to create a new host and add it to an existing group. |
| 5 | + The new host will be using the red color, with a comment "This host was created thanks to the API" and |
| 6 | + will have two tags named "test1" and "test2" |
| 7 | +""" |
| 8 | + |
| 9 | +import logging |
| 10 | +import time |
| 11 | + |
| 12 | +import restfly |
| 13 | + |
| 14 | +from pycheckpoint_api.firewallManagement import FirewallManagement |
| 15 | +from pycheckpoint_api.models import Color |
| 16 | + |
| 17 | +# Setup the logger configuration. If you have any trouble, turn the logging mode to logging.DEBUG |
| 18 | +logging.basicConfig( |
| 19 | + filename="pycheckpoint_api-example-add-host-to-group.log", |
| 20 | + encoding="utf-8", |
| 21 | + level=logging.INFO, |
| 22 | +) |
| 23 | +logger = logging.getLogger(__name__) |
| 24 | + |
| 25 | +# GLOBAL VARIABLE |
| 26 | +HOSTNAME = "" |
| 27 | +PORT = 443 |
| 28 | +USER = "" |
| 29 | +PASSWORD = "" |
| 30 | +VERSION = "1.8" |
| 31 | +DOMAIN = "" |
| 32 | + |
| 33 | +# Initialize the API |
| 34 | +api = None |
| 35 | + |
| 36 | +try: |
| 37 | + # Please note that, as it's an example, we enabled the SSL verify to False to avoid having SSL certificate issues. |
| 38 | + # However, it's highly recommanded to use certificates with know certificate authorities. |
| 39 | + logger.info("Trying to login to the API...") |
| 40 | + |
| 41 | + with FirewallManagement( |
| 42 | + hostname=HOSTNAME, |
| 43 | + port=PORT, |
| 44 | + user=USER, |
| 45 | + password=PASSWORD, |
| 46 | + version=VERSION, |
| 47 | + domain=DOMAIN, |
| 48 | + ssl_verify=False, |
| 49 | + ) as firewall: |
| 50 | + |
| 51 | + # Start a timer |
| 52 | + start = time.time() |
| 53 | + |
| 54 | + # Ask for the host name |
| 55 | + INPUT_HOST_NAME = input("Enter the new host name: ") |
| 56 | + |
| 57 | + # Ask for the IPv4 information |
| 58 | + INPUT_IP = input("Enter the IPv4 address (x.x.x.x): ") |
| 59 | + |
| 60 | + # Ask for the group |
| 61 | + INPUT_GROUP = input( |
| 62 | + "Enter the name of the group in which you want to add this new host: " |
| 63 | + ) |
| 64 | + |
| 65 | + # Execute the action |
| 66 | + logger.info("Trying to create the new host...") |
| 67 | + |
| 68 | + try: |
| 69 | + new_host = firewall.network_objects.host.add( |
| 70 | + name=INPUT_HOST_NAME, |
| 71 | + ip_address=INPUT_IP, |
| 72 | + groups=INPUT_GROUP, |
| 73 | + color=Color.RED, |
| 74 | + comments="This host was created thanks to the API", |
| 75 | + tags=["test1", "test2"], |
| 76 | + ) |
| 77 | + |
| 78 | + logger.info("Host created successfully !") |
| 79 | + except Exception as e: |
| 80 | + logger.error("An error occured to create the new host.") |
| 81 | + |
| 82 | + # End timer |
| 83 | + timer = time.time() - start |
| 84 | + |
| 85 | + logger.info("-----------------------------------") |
| 86 | + logger.info( |
| 87 | + "Execution time: " |
| 88 | + + str(int(timer / 60)) |
| 89 | + + "min " |
| 90 | + + str(round(timer % 60)) |
| 91 | + + "s" |
| 92 | + ) |
| 93 | + |
| 94 | +except restfly.errors.BadRequestError as e: |
| 95 | + print(e) |
| 96 | + for p in dir(e.response.request): |
| 97 | + print(p, getattr(e.response.request, p)) |
0 commit comments