Whakamaamaa te API Takitaki me te Python SDK

Whakamaamaa te API Takitaki me te Python SDKKo te kaha katoa o te taunekeneke me nga API ka kitea i te wa e whakamahia ana me te waehere papatono, ka taea te whakaputa i nga tono API me nga taputapu mo te tarai i nga whakautu API. Heoi ano, kare tonu e kitea Kete Whakawhanake Pūmanawa Python (i muri nei ka kiia ko Python SDK) mo Tirohia API Whakahaere Tohu, engari mo te kore noa iho. Ka tino whakamaarama te oranga o nga kaiwhakawhanake me te hunga kaingākau aunoa. Kua tino rongonui a Python i enei ra ka whakatau ahau ki te whakakii i te waahi me te arotake i nga ahuatanga matua. Tirohia Point API Python Development Kit. Ko tenei tuhinga he taapiri pai ki tetahi atu tuhinga mo Habré Tohu Tirohanga R80.10 API. Whakahaerenga ma CLI, tuhinga me etahi atu. Ka titiro tatou me pehea te tuhi tuhinga ma te whakamahi i te Python SDK me te ata titiro ki te mahinga API Whakahaere hou i te putanga 1.6 (tautokohia mai i te R80.40). Kia mohio koe ki te tuhinga, me mohio koe ki te mahi me nga API me te Python.

Kei te kaha te whakawhanake a Check Point i te API a kua tukuna inaianei:

Kei te tautoko noa te Python SDK i te taunekeneke me te API Whakahaere me te Gaia API. Ka titiro tatou ki nga karaehe tino nui, nga tikanga me nga taurangi o tenei waahanga.

Whakamaamaa te API Takitaki me te Python SDK

Te tāuta i te kōwae

Takahauira cpapi he tere me te ngawari te whakauru mai putunga Tirotiro whaimana i runga i te github me te awhina pata. Kei te waatea nga tohutohu whakaurunga taipitopito i roto README.md. Kua urutauhia tenei waahanga ki te mahi me nga putanga Python 2.7 me 3.7. I roto i tenei tuhinga, ka hoatu nga tauira ma te whakamahi i te Python 3.7. Engari, ko te Python SDK ka taea te whakahaere tika mai i te Tiaki Whakahaere Tohu Tirohanga (Smart Management), engari kei te tautoko noa ratou i te Python 2.7, na te waahanga whakamutunga ka whakarato waehere mo te putanga 2.7. I muri tonu i te whakaurunga o te kōwae, ka tūtohu ahau ki te titiro ki nga tauira i roto i nga raarangi tauira_python2 и tauira_python3.

Getting Started

Kia taea ai e matou te mahi me nga waahanga o te waahanga cpapi, me kawemai mai i te waahanga cpapi kia rua nga akomanga e hiahiatia ana:

Kiritaki API и APIClientArgs

from cpapi import APIClient, APIClientArgs

Класс APIClientArgs Ko te kawenga mo nga tawhā hononga ki te tūmau API, me te akomanga Kiritaki API Ko te kawenga mo te taunekeneke me te API.

Te whakatau i nga tawhā hononga

Hei tautuhi i nga momo tawhā mo te hono ki te API, me hanga e koe he tauira o te akomanga APIClientArgs. Ko te tikanga, ko ona tawhā kua tautuhia, a, i te wa e whakahaere ana i te tuhinga i runga i te tūmau mana, kaore e hiahiatia kia tohua.

client_args = APIClientArgs()

Engari i te wa e rere ana i runga i te kaihautu-tuatoru, me tohu koe i te iti rawa o te wahitau IP me te ingoa manaaki o te tūmau API (e mohiotia ana ko te tūmau whakahaere). I roto i te tauira i raro nei, ka tautuhia e matou te tawhā hononga tūmau ka tautapahia te wāhitau IP o te tūmau whakahaere hei aho.

client_args = APIClientArgs(server='192.168.47.241')

Me titiro ki nga tawhā katoa me o raatau uara taunoa ka taea te whakamahi ina hono ana ki te tūmau API:

Nga tautohetohe mo te tikanga __init__ o te akomanga APIClientArgs

class APIClientArgs:
    """
    This class provides arguments for APIClient configuration.
    All the arguments are configured with their default values.
    """

    # port is set to None by default, but it gets replaced with 443 if not specified
    # context possible values - web_api (default) or gaia_api
    def __init__(self, port=None, fingerprint=None, sid=None, server="127.0.0.1", http_debug_level=0,
                 api_calls=None, debug_file="", proxy_host=None, proxy_port=8080,
                 api_version=None, unsafe=False, unsafe_auto_accept=False, context="web_api"):
        self.port = port
        # management server fingerprint
        self.fingerprint = fingerprint
        # session-id.
        self.sid = sid
        # management server name or IP-address
        self.server = server
        # debug level
        self.http_debug_level = http_debug_level
        # an array with all the api calls (for debug purposes)
        self.api_calls = api_calls if api_calls else []
        # name of debug file. If left empty, debug data will not be saved to disk.
        self.debug_file = debug_file
        # HTTP proxy server address (without "http://")
        self.proxy_host = proxy_host
        # HTTP proxy port
        self.proxy_port = proxy_port
        # Management server's API version
        self.api_version = api_version
        # Indicates that the client should not check the server's certificate
        self.unsafe = unsafe
        # Indicates that the client should automatically accept and save the server's certificate
        self.unsafe_auto_accept = unsafe_auto_accept
        # The context of using the client - defaults to web_api
        self.context = context

E whakapono ana ahau ko nga tohenga ka taea te whakamahi i nga wa o te akomanga APIClientArgs he maamaa mo nga kaiwhakahaere Tirotiro Tirohanga me te kore e rapu korero taapiri.

Te hono ma te APIClient me te kaiwhakahaere horopaki

Класс Kiritaki API Ko te huarahi tino watea ki te whakamahi ma te kaiwhakahaere horopaki. Ko nga mea katoa e tika ana kia tukuna ki tetahi tauira o te akomanga APIClient ko nga tawhā hononga i tautuhia i te taahiraa o mua.

with APIClient(client_args) as client:

Ko te kaiwhakahaere horopaki e kore e hanga aunoa i te waea takiuru ki te tūmau API, engari ka puta he waea takiputa ina puta ana. Mena he take kaore e hiahiatia kia puta mai i muri i te mutunga o te mahi me nga waea API, me timata koe ki te mahi me te kore e whakamahi i te kaiwhakahaere horopaki:

client = APIClient(clieng_args)

Tirohia te hononga

Ko te huarahi ngawari ki te tirotiro mena ka tutuki te hononga ki nga tawhā kua tohua ko te whakamahi i te tikanga taki_matimati. Mena ka rahua te manatokonga o te moni hash sha1 mo te maihao o te tiwhikete API tūmau (kua hoki mai te tikanga teka), katahi ka puta mai tenei na nga raru hononga ka taea e taatau te aukati i te mahi o te kaupapa (ka hoatu ranei ki te kaiwhakamahi te whai waahi ki te whakatika i nga raraunga hononga):

    if client.check_fingerprint() is False:
        print("Could not get the server's fingerprint - Check connectivity with the server.")
        exit(1)

Kia mahara kei te heke mai ko te karaehe Kiritaki API ka tirohia ia piiraa API (tikanga api_karanga и api_uiui, ka korerohia e matou mo ratou he iti ake) tiwhikete tohu matimati sha1 i runga i te tūmau API. Engari ki te tirotirohia te sha1 maihao o te tiwhikete tūmau API, ka kitea he hapa (kaore i te mohiotia te tiwhikete, kua hurihia ranei), te tikanga taki_matimati ka whai waahi ki te taapiri / whakarereke i nga korero mo runga i te miihini o te rohe. Ka taea te whakakore rawa i tenei haki (engari ka taea anake te tohutohu mena ka whakahaerehia nga tuhinga ki runga i te tūmau API ake, ina hono ana ki te 127.0.0.1), ma te whakamahi i te tohenga APIClientArgs - unafe_auto_accept (Tirohia etahi atu korero mo APIClientArgs i mua i te "Te tautuhi i nga tawhā hononga").

client_args = APIClientArgs(unsafe_auto_accept=True)

Takiuru ki te tūmau API

У Kiritaki API he maha nga tikanga e 3 mo te takiuru ki roto i te tūmau API, a ka mohio ia o ratou ki te tikanga Tuhinga o mua(watū-id), ka whakamahia aunoatia i ia piiraa API ka whai ake i te pane (ko te ingoa kei te pane o tenei tawhā ko X-chkp-sid), no reira kaore he take ki te tukatuka ano i tenei tawhā.

tikanga takiuru

Kōwhiringa mā te takiuru me te kupuhipa (i te tauira, ko te ingoa kaiwhakamahi kaiwhakahaere me te kupuhipa 1q2w3e ka tukuna hei tohenga tuunga):

     login = client.login('admin', '1q2w3e')  

Kei te waatea ano etahi taapiri taapiri i roto i te tikanga takiuru; koinei o raatau ingoa me nga uara taunoa:

continue_last_session=False, domain=None, read_only=False, payload=None

Takiuru_me_api_aratuka

Kōwhiringa mā te kī api (tautokohia mai i te putanga whakahaere R80.40/Management API v1.6, "3TsbPJ8ZKjaJGvFyoFqHFA=" koinei te uara matua API mo tetahi o nga kaiwhakamahi i runga i te tūmau whakahaere me te tikanga whakamana matua API):

     login = client.login_with_api_key('3TsbPJ8ZKjaJGvFyoFqHFA==') 

I roto i te tikanga takiuru_me_api_matua e wātea ana nga tawhā kōwhiringa rite i roto i te tikanga takiuru.

login_as_root tikanga

Kōwhiringa ki te takiuru ki tētahi mīhini ā-rohe me tētahi tūmau API:

     login = client.login_as_root()

E rua noa nga tawhā e waatea ana mo tenei tikanga:

domain=None, payload=None

Na te mutunga ka karanga te API ki a ratou ano

E rua nga whiringa ki te hanga waea API ma nga tikanga api_karanga и api_uiui. Kia mohio tatou he aha te rereketanga i waenga i a raatau.

api_karanga

E tika ana tenei tikanga mo nga waea katoa. Me tuku te waahanga whakamutunga mo te waea api me te utu i roto i te tinana tono mena e tika ana. Mena kei te putua te utu, karekau e taea te whakawhiti:

api_versions = client.api_call('show-api-versions') 

Putanga mo tenei tono i raro i te tapahi:

In [23]: api_versions                                                           
Out[23]: 
APIResponse({
    "data": {
        "current-version": "1.6",
        "supported-versions": [
            "1",
            "1.1",
            "1.2",
            "1.3",
            "1.4",
            "1.5",
            "1.6"
        ]
    },
    "res_obj": {
        "data": {
            "current-version": "1.6",
            "supported-versions": [
                "1",
                "1.1",
                "1.2",
                "1.3",
                "1.4",
                "1.5",
                "1.6"
            ]
        },
        "status_code": 200
    },
    "status_code": 200,
    "success": true
})
show_host = client.api_call('show-host', {'name' : 'h_8.8.8.8'})

Putanga mo tenei tono i raro i te tapahi:

In [25]: show_host                                                              
Out[25]: 
APIResponse({
    "data": {
        "color": "black",
        "comments": "",
        "domain": {
            "domain-type": "domain",
            "name": "SMC User",
            "uid": "41e821a0-3720-11e3-aa6e-0800200c9fde"
        },
        "groups": [],
        "icon": "Objects/host",
        "interfaces": [],
        "ipv4-address": "8.8.8.8",
        "meta-info": {
            "creation-time": {
                "iso-8601": "2020-05-01T21:49+0300",
                "posix": 1588358973517
            },
            "creator": "admin",
            "last-modifier": "admin",
            "last-modify-time": {
                "iso-8601": "2020-05-01T21:49+0300",
                "posix": 1588358973517
            },
            "lock": "unlocked",
            "validation-state": "ok"
        },
        "name": "h_8.8.8.8",
        "nat-settings": {
            "auto-rule": false
        },
        "read-only": false,
        "tags": [],
        "type": "host",
        "uid": "c210af07-1939-49d3-a351-953a9c471d9e"
    },
    "res_obj": {
        "data": {
            "color": "black",
            "comments": "",
            "domain": {
                "domain-type": "domain",
                "name": "SMC User",
                "uid": "41e821a0-3720-11e3-aa6e-0800200c9fde"
            },
            "groups": [],
            "icon": "Objects/host",
            "interfaces": [],
            "ipv4-address": "8.8.8.8",
            "meta-info": {
                "creation-time": {
                    "iso-8601": "2020-05-01T21:49+0300",
                    "posix": 1588358973517
                },
                "creator": "admin",
                "last-modifier": "admin",
                "last-modify-time": {
                    "iso-8601": "2020-05-01T21:49+0300",
                    "posix": 1588358973517
                },
                "lock": "unlocked",
                "validation-state": "ok"
            },
            "name": "h_8.8.8.8",
            "nat-settings": {
                "auto-rule": false
            },
            "read-only": false,
            "tags": [],
            "type": "host",
            "uid": "c210af07-1939-49d3-a351-953a9c471d9e"
        },
        "status_code": 200
    },
    "status_code": 200,
    "success": true
})

api_uiui

Tukua ahau kia rahui i tenei wa tonu ko tenei tikanga e tika ana mo nga waea e uru ana te whakaputanga. Ka puta taua whakatauki ina he nui nga korero kei roto, kei roto ranei. Hei tauira, he tono pea tenei mo te rarangi o nga mea manaaki katoa i hangaia i runga i te tūmau whakahaere. Mo enei tono, ka whakahoki te API i te rarangi o nga taonga 50 ma te taunoa (ka taea e koe te whakanui ake i te tepe ki te 500 nga taonga i roto i te whakautu). A kia kore ai e toia nga korero i nga wa maha ma te huri i te tawhā utu i roto i te tono API, he tikanga api_query e mahi aunoa i tenei mahi. Nga tauira o nga waea e hiahiatia ana tenei tikanga: whakaaturanga-waahanga, whakaatu-kaiwhakahaere, whakaatu-whatunga, whakaatu-kaari mohoao, whakaaturanga-rōpū, whakaatu-whitau-awhe, whakaatu-tikanga-kuwaha, whakaatu-ngawari-ahua, whakaatu-whakauru-turanga, whakaatu-whakawhirinaki-kiritaki, whakaaturanga-peke. Inaa, ka kite matou i nga kupu maha i roto i te ingoa o enei waea API, no reira ka ngawari ake te whakahaere i enei waea api_uiui

show_hosts = client.api_query('show-hosts') 

Putanga mo tenei tono i raro i te tapahi:

In [21]: show_hosts                                                             
Out[21]: 
APIResponse({
    "data": [
        {
            "domain": {
                "domain-type": "domain",
                "name": "SMC User",
                "uid": "41e821a0-3720-11e3-aa6e-0800200c9fde"
            },
            "ipv4-address": "192.168.47.1",
            "name": "h_192.168.47.1",
            "type": "host",
            "uid": "5d7d7086-d70b-4995-971a-0583b15a2bfc"
        },
        {
            "domain": {
                "domain-type": "domain",
                "name": "SMC User",
                "uid": "41e821a0-3720-11e3-aa6e-0800200c9fde"
            },
            "ipv4-address": "8.8.8.8",
            "name": "h_8.8.8.8",
            "type": "host",
            "uid": "c210af07-1939-49d3-a351-953a9c471d9e"
        }
    ],
    "res_obj": {
        "data": {
            "from": 1,
            "objects": [
                {
                    "domain": {
                        "domain-type": "domain",
                        "name": "SMC User",
                        "uid": "41e821a0-3720-11e3-aa6e-0800200c9fde"
                    },
                    "ipv4-address": "192.168.47.1",
                    "name": "h_192.168.47.1",
                    "type": "host",
                    "uid": "5d7d7086-d70b-4995-971a-0583b15a2bfc"
                },
                {
                    "domain": {
                        "domain-type": "domain",
                        "name": "SMC User",
                        "uid": "41e821a0-3720-11e3-aa6e-0800200c9fde"
                    },
                    "ipv4-address": "8.8.8.8",
                    "name": "h_8.8.8.8",
                    "type": "host",
                    "uid": "c210af07-1939-49d3-a351-953a9c471d9e"
                }
            ],
            "to": 2,
            "total": 2
        },
        "status_code": 200
    },
    "status_code": 200,
    "success": true
})

Te tukatuka i nga hua o nga waea API

I muri i tenei ka taea e koe te whakamahi i nga taurangi me nga tikanga o te akomanga APIWhakautu(i roto i te kaiwhakahaere horopaki me waho). I te akomanga APIWhakautu E 4 nga tikanga me nga taurangi e 5 kua tautuhia; ka korerohia e maatau nga mea tino nui.

Whakamaamaa te API Takitaki me te Python SDK

angitu

Hei timata, he pai ki te whakarite kua angitu te waea API me te whakahoki i tetahi hua. He tikanga mo tenei angitu:

In [49]: api_versions.success                                                   
Out[49]: True

Whakahoki Pono mena i angitu te waea API (waehere whakautu - 200) me te Heke mena kaore i angitu (tetahi atu waehere whakautu). He pai ki te whakamahi i muri tonu i te karanga API ki te whakaatu i nga korero rereke i runga i te waehere whakautu.

if api_ver.success: 
    print(api_versions.data) 
else: 
    print(api_versions.err_message) 

waehere tūnga

Ka whakahoki i te waehere whakautu i muri i te waea API.

In [62]: api_versions.status_code                                               
Out[62]: 400

Waehere whakautu pea: 200,400,401,403,404,409,500,501.

whakaturia_tu_tu_anga

I tenei keehi, me whakarereke pea te uara o te mana angitu. Ma te hangarau, ka taea e koe te whakauru i tetahi mea ki reira, ahakoa he aho noa. Engari ko te tino tauira ko te tautuhi ano i tenei tawhā ki te Heke i raro i etahi tikanga e whai ake nei. Kei raro nei, kia whai whakaaro ki te tauira ina he mahi kei te whakahaere i runga i te tūmau whakahaere, engari ka whakaaro matou kaore tenei tono i angitu (ka tautuhia te taurangi angitu ki teka, ahakoa te mea i angitu te karanga API me te whakahoki i te waehere 200).

for task in task_result.data["tasks"]:
    if task["status"] == "failed" or task["status"] == "partially succeeded":
        task_result.set_success_status(False)
        break

whakautu()

Ko te tikanga whakautu ka taea e koe te tiro i te papakupu me te waehere whakautu (status_code) me te tinana whakautu (tinana).

In [94]: api_versions.response()                                                
Out[94]: 
{'status_code': 200,
 'data': {'current-version': '1.6',
  'supported-versions': ['1', '1.1', '1.2', '1.3', '1.4', '1.5', '1.6']}}

raraunga

Ka taea e koe te kite i te tinana o te whakautu (tinana) anake kaore he korero kore.

In [93]: api_versions.data                                                      
Out[93]: 
{'current-version': '1.6',
 'supported-versions': ['1', '1.1', '1.2', '1.3', '1.4', '1.5', '1.6']}

hapa_karere

Kei te waatea noa enei korero ka puta he hapa i te wa e tukatuka ana i te tono API (waehere whakautu e kore 200). Putanga tauira

In [107]: api_versions.error_message                                            
Out[107]: 'code: generic_err_invalid_parameter_namenmessage: Unrecognized parameter [1]n'

He tauira whai hua

Ko nga tauira e whai ake nei e whakamahi ana i nga waea API i taapirihia ki te API Whakahaere 1.6.

Tuatahi, me titiro tatou ki te mahi waea tāpiri-kaiwhakahaere и tāpiri-wāhitau-awhe. Me kii tatou me hanga nga wahitau IP katoa o te kupengaroto 192.168.0.0/24, ko te octet whakamutunga ko te 5, hei taonga mo te momo kaihautu, me te tuhi i etahi atu wahitau IP katoa hei taonga mo te momo awhe wahitau. I tenei take, waiho te wahitau kupengaroto me te wahitau haapurororaa.

Na, kei raro nei he tuhinga hei whakaoti i tenei raru me te hanga i nga taonga 50 o te momo kaihautu me nga taonga 51 o te momo awhe wahitau. Hei whakaoti rapanga, me 101 nga waea API (kaore e tatau te waea whakaputa whakamutunga). Ano, ma te whakamahi i te waahanga timeit, ka tatauhia te wa e mahia ai te tuhinga tae noa ki te whakaputanga o nga huringa.

Hōtuhi mā te tāpiri-kaiwhakahaere me te awhe-awhe-wāhitau

import timeit
from cpapi import APIClient, APIClientArgs

start = timeit.default_timer()

first_ip = 1
last_ip = 4

client_args = APIClientArgs(server="192.168.47.240")

with APIClient(client_args) as client: 
     login = client.login_with_api_key('3TsbPJ8ZKjaJGvFyoFqHFA==')
     for ip in range(5,255,5):
         add_host = client.api_call("add-host", {"name" : f"h_192.168.0.{ip}", "ip-address": f'192.168.0.{ip}'})
     while last_ip < 255:
         add_range = client.api_call("add-address-range", {"name": f"r_192.168.0.{first_ip}-{last_ip}", "ip-address-first": f"192.168.0.{first_ip}", "ip-address-last": f"192.168.0.{last_ip}"})
         first_ip+=5
         last_ip+=5
     stop = timeit.default_timer() 
     publish = client.api_call("publish")
     
print(f'Time to execute batch request: {stop - start} seconds')

I roto i taku taiao taiwhanga, he 30 me te 50 hēkona te roa o tenei tuhinga ki te mahi, i runga i te kawenga o te tūmau whakahaere.

Inaianei kia kite tatou me pehea te whakaoti i taua raru ma te whakamahi i te waea API tāpiri-ahanoa-puranga, tautoko i taapirihia ki te putanga API 1.6. Ma tenei waea ka taea e koe te hanga i nga taonga maha i te wa kotahi i roto i te tono API kotahi. I tua atu, he momo momo rereke enei (hei tauira, nga kaihautu, nga kupenga iti me nga awhe wahitau). No reira, ka taea e taatau mahi te whakaoti i roto i te anga o te waea API kotahi.

Hōtuhi e whakamahi ana i nga taonga-apiri

import timeit
from cpapi import APIClient, APIClientArgs

start = timeit.default_timer()

client_args = APIClientArgs(server="192.168.47.240")

objects_list_ip = []
objects_list_range = []

for ip in range(5,255,5):
    data = {"name": f'h_192.168.0.{ip}', "ip-address": f'192.168.0.{ip}'}
    objects_list_ip.append(data)
    
first_ip = 1
last_ip = 4


while last_ip < 255:
    data = {"name": f"r_192.168.0.{first_ip}-{last_ip}", "ip-address-first": f"192.168.0.{first_ip}", "ip-address-last": f"192.168.0.{last_ip}"}
    objects_list_range.append(data)
    first_ip+=5
    last_ip+=5

data_for_batch = {
  "objects" : [ {
    "type" : "host",
    "list" : objects_list_ip
}, {
    "type" : "address-range",
    "list" : objects_list_range
  }]
}


with APIClient(client_args) as client: 
     login = client.login_with_api_key('3TsbPJ8ZKjaJGvFyoFqHFA==')
     add_objects_batch = client.api_call("add-objects-batch", data_for_batch)
     stop = timeit.default_timer() 
     publish = client.api_call("publish")
     
print(f'Time to execute batch request: {stop - start} seconds')

A ko te whakahaere i tenei tuhinga i roto i taku taiao taiwhanga ka roa mai i te 3 ki te 7 hēkona, i runga i te kawenga o te tūmau whakahaere. Arā, i te toharite, i runga i nga taonga API 101, he 10 nga wa tere ake te waea momo puranga. I runga i te maha o nga taonga ka tino miharo te rereketanga.

Inaianei kia kite tatou me pehea te mahi huinga-ahua-papa. Ma te whakamahi i tenei piiraa API, ka taea e taatau te whakarereke i tetahi tawhā. Me whakarite te haurua tuatahi o nga wahitau mai i te tauira o mua (tae atu ki te .124 kaihautu, me nga awhe hoki) ki te sienna tae, ka tautapa te tae khaki ki te haurua tuarua o nga wahitau.

Te huri i te tae o nga mea i hangaia i te tauira o mua

from cpapi import APIClient, APIClientArgs

client_args = APIClientArgs(server="192.168.47.240")

objects_list_ip_first = []
objects_list_range_first = []
objects_list_ip_second = []
objects_list_range_second = []

for ip in range(5,125,5):
    data = {"name": f'h_192.168.0.{ip}', "color": "sienna"}
    objects_list_ip_first.append(data)
    
for ip in range(125,255,5):
    data = {"name": f'h_192.168.0.{ip}', "color": "khaki"}
    objects_list_ip_second.append(data)
    
first_ip = 1
last_ip = 4
while last_ip < 125:
    data = {"name": f"r_192.168.0.{first_ip}-{last_ip}", "color": "sienna"}
    objects_list_range_first.append(data)
    first_ip+=5
    last_ip+=5
    
while last_ip < 255:
    data = {"name": f"r_192.168.0.{first_ip}-{last_ip}", "color": "khaki"}
    objects_list_range_second.append(data)
    first_ip+=5
    last_ip+=5

data_for_batch_first  = {
  "objects" : [ {
    "type" : "host",
    "list" : objects_list_ip_first
}, {
    "type" : "address-range",
    "list" : objects_list_range_first
  }]
}

data_for_batch_second  = {
  "objects" : [ {
    "type" : "host",
    "list" : objects_list_ip_second
}, {
    "type" : "address-range",
    "list" : objects_list_range_second
  }]
}

with APIClient(client_args) as client: 
     login = client.login_with_api_key('3TsbPJ8ZKjaJGvFyoFqHFA==') 
     set_objects_batch_first = client.api_call("set-objects-batch", data_for_batch_first)
     set_objects_batch_second = client.api_call("set-objects-batch", data_for_batch_second)
     publish = client.api_call("publish")

Ka taea e koe te whakakore i nga taonga maha i roto i te waea API kotahi ma te whakamahi delete-objects-batch. Inaianei kia titiro tatou ki tetahi tauira waehere ka whakakore i nga kaihautu katoa i hangaia i mua ma tāpiri-ahanoa-puranga.

Te whakakore i nga taonga ma te whakamahi i te delete-objects-batch

from cpapi import APIClient, APIClientArgs

client_args = APIClientArgs(server="192.168.47.240")

objects_list_ip = []
objects_list_range = []

for ip in range(5,255,5):
    data = {"name": f'h_192.168.0.{ip}'}
    objects_list_ip.append(data)

first_ip = 1
last_ip = 4
while last_ip < 255:
    data = {"name": f"r_192.168.0.{first_ip}-{last_ip}"}
    objects_list_range.append(data)
    first_ip+=5
    last_ip+=5

data_for_batch = {
  "objects" : [ {
    "type" : "host",
    "list" : objects_list_ip
}, {
    "type" : "address-range",
    "list" : objects_list_range
  }]
}

with APIClient(client_args) as client: 
     login = client.login_with_api_key('3TsbPJ8ZKjaJGvFyoFqHFA==')
     delete_objects_batch = client.api_call("delete-objects-batch", data_for_batch)
     publish = client.api_call("publish")

print(delete_objects_batch.data)

Ko nga mahi katoa ka puta mai i nga putanga hou o te rorohiko Check Point ka whiwhi tonu i nga waea API. No reira, i roto i te R80.40 ka puta nga "ahuatanga" penei i te Revert ki te whakahou me te Mahi Maamaa, a ka rite tonu nga waea API mo ratou. Hei taapiri, ko nga mahi katoa ka neke mai i nga papatohu tuku iho ki te aratau Kaupapa Whakakotahi ka whiwhi tautoko API. Hei tauira, ko te whakahoutanga kua roa e tatarihia ana i roto i te putanga rorohiko R80.40 ko te neke o te kaupapahere Tirotiro HTTPS mai i te aratau tuku iho ki te aratau Kaupapa Whakakotahi, a ka riro tonu mai i tenei mahi nga waea API. Anei tetahi tauira o te waehere e taapiri ana i te ture ki runga ake o te kaupapa here Tirotiro HTTPS e whakakore ana i nga waahanga e 3 mai i te tirotiro (Hauora, Putea, Ratonga Kawanatanga), ka aukatihia te tirotiro i runga i te ture i roto i te maha o nga whenua.

Tāpirihia he ture ki te kaupapa here Tirotiro HTTPS

from cpapi import APIClient, APIClientArgs

client_args = APIClientArgs(server="192.168.47.240")

data = {
  "layer" : "Default Layer",
  "position" : "top",
  "name" : "Legal Requirements",
  "action": "bypass",
  "site-category": ["Health", "Government / Military", "Financial Services"]
}

with APIClient(client_args) as client: 
     login = client.login_with_api_key('3TsbPJ8ZKjaJGvFyoFqHFA==')
     add_https_rule = client.api_call("add-https-rule", data)
     publish = client.api_call("publish")

Te whakahaere i nga tuhinga a Python i runga i te tūmau whakahaere Tirotiro

He rite tonu nga mea katoa README.md kei roto nga korero mo te whakahaere tika i nga tuhinga Python mai i te tūmau mana. Ka pai tenei ki te kore e taea e koe te hono atu ki te tūmau API mai i tetahi atu miihini. I hopukina e ahau he ataata e ono meneti e titiro ana ahau ki te whakauru i te waahanga cpapi me nga ahuatanga o te whakahaere i nga tuhinga Python i runga i te tūmau mana. Hei tauira, ka whakahaerehia he tuhinga e whakaaunoa ana i te whirihoranga o te kuaha hou mo tetahi mahi penei i te arotake whatunga Tirohanga Haumarutanga. I roto i nga ahuatanga i mahia e au: kaore ano kia puta te mahi ki Python 2.7 tāuru, na ki te tukatuka i nga korero ka whakauruhia e te kaiwhakamahi, ka whakamahia he mahi raw_whakauru. Ki te kore, he rite te waehere mo te whakarewatanga mai i etahi atu miihini, engari he pai ake te whakamahi i te mahi login_as_root, kia kore ai e kii ano to ingoa kaiwhakamahi, kupuhipa me te wahitau IP o te tūmau whakahaere.

Hōtuhi mo te tatūnga tere o te Tirohanga Haumarutanga

from __future__ import print_function
import getpass
import sys, os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from cpapi import APIClient, APIClientArgs

def main():
    with APIClient() as client:
       # if client.check_fingerprint() is False:
       #     print("Could not get the server's fingerprint - Check connectivity with the server.")
       #     exit(1)
        login_res = client.login_as_root()

        if login_res.success is False:
            print("Login failed:n{}".format(login_res.error_message))
            exit(1)

        gw_name = raw_input("Enter the gateway name:")
        gw_ip = raw_input("Enter the gateway IP address:")
        if sys.stdin.isatty():
            sic = getpass.getpass("Enter one-time password for the gateway(SIC): ")
        else:
            print("Attention! Your password will be shown on the screen!")
            sic = raw_input("Enter one-time password for the gateway(SIC): ")
        version = raw_input("Enter the gateway version(like RXX.YY):")
        add_gw = client.api_call("add-simple-gateway", {'name' : gw_name, 'ipv4-address' : gw_ip, 'one-time-password' : sic, 'version': version.capitalize(), 'application-control' : 'true', 'url-filtering' : 'true', 'ips' : 'true', 'anti-bot' : 'true', 'anti-virus' : 'true', 'threat-emulation' : 'true'})
        if add_gw.success and add_gw.data['sic-state'] != "communicating":
            print("Secure connection with the gateway hasn't established!")
            exit(1)
        elif add_gw.success:
            print("The gateway was added successfully.")
            gw_uid = add_gw.data['uid']
            gw_name = add_gw.data['name']
        else:
            print("Failed to add the gateway - {}".format(add_gw.error_message))
            exit(1)

        change_policy = client.api_call("set-access-layer", {"name" : "Network", "applications-and-url-filtering": "true", "content-awareness": "true"})
        if change_policy.success:
            print("The policy has been changed successfully")
        else:
            print("Failed to change the policy- {}".format(change_policy.error_message))
        change_rule = client.api_call("set-access-rule", {"name" : "Cleanup rule", "layer" : "Network", "action": "Accept", "track": {"type": "Detailed Log", "accounting": "true"}})
        if change_rule.success:
            print("The cleanup rule has been changed successfully")
        else:
            print("Failed to change the cleanup rule- {}".format(change_rule.error_message))

        # publish the result
        publish_res = client.api_call("publish", {})
        if publish_res.success:
            print("The changes were published successfully.")
        else:
                print("Failed to publish the changes - {}".format(install_tp_policy.error_message))

        install_access_policy = client.api_call("install-policy", {"policy-package" : "Standard", "access" : 'true',  "threat-prevention" : 'false', "targets" : gw_uid})
        if install_access_policy.success:
            print("The access policy has been installed")
        else:
                print("Failed to install access policy - {}".format(install_tp_policy.error_message))

        install_tp_policy = client.api_call("install-policy", {"policy-package" : "Standard", "access" : 'false',  "threat-prevention" : 'true', "targets" : gw_uid})
        if install_tp_policy.success:
            print("The threat prevention policy has been installed")
        else:
            print("Failed to install threat prevention policy - {}".format(install_tp_policy.error_message))
        
        # add passwords and passphrases to dictionary
        with open('additional_pass.conf') as f:
            line_num = 0
            for line in f:
                line_num += 1
                add_password_dictionary = client.api_call("run-script", {"script-name" : "Add passwords and passphrases", "script" : "printf "{}" >> $FWDIR/conf/additional_pass.conf".format(line), "targets" : gw_name})
                if add_password_dictionary.success:
                    print("The password dictionary line {} was added successfully".format(line_num))
                else:
                    print("Failed to add the dictionary - {}".format(add_password_dictionary.error_message))

main()

He tauira kōnae me te papakupu kupuhipa additional_pass.conf
{
"passwords" : ["malware","malicious","infected","Infected"],
"phrases" : ["password","Password","Pass","pass","codigo","key","pwd","пароль","Пароль","Ключ","ключ","шифр","Шифр"] }

mutunga

Ka tirotirohia e tenei tuhinga nga waahanga noa o te mahi Python SDK me te kōwae cpapi(kei te whakaaro pea koe, he kupu rite tonu enei), a ma te ako i te waehere i roto i tenei waahanga ka kitea e koe etahi atu huarahi ki te mahi. Ka hiahia pea koe ki te taapiri i a koe ake karaehe, mahi, tikanga me nga taurangi. Ka taea e koe te tiri i o mahi me te tiro i etahi atu tuhinga mo te Tirotiro i te waahanga CodeHub i roto i te hapori TakiMates, e whakakotahi ana i nga kaihanga hua me nga kaiwhakamahi.

Ka koa te whakawaehere me te mihi mo te panui ki te mutunga!

Source: will.com

Tāpiri i te kōrero