<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python API Lib set in API / CLI Discussion</title>
    <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Python-API-Lib-set/m-p/66850#M4082</link>
    <description>&lt;P&gt;Not bad &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; But are you familiar with the official Python SDK for the management (and nowadays also the GAiA) API?&lt;/P&gt;&lt;P&gt;You can find it via the official Check Point Github Account, or by clicking &lt;A href="https://github.com/CheckPointSW/cp_mgmt_api_python_sdk" target="_self"&gt;here&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;Could save you some time as it nearly has all the features which somebody would need for automation tasks via the API.&lt;/P&gt;&lt;P&gt;It handles the requests and return values + also have own functions for login etc.&amp;nbsp;In addition to that it includes samples, that show you how to get started.&lt;/P&gt;</description>
    <pubDate>Fri, 08 Nov 2019 08:11:26 GMT</pubDate>
    <dc:creator>Maik</dc:creator>
    <dc:date>2019-11-08T08:11:26Z</dc:date>
    <item>
      <title>Python API Lib set</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Python-API-Lib-set/m-p/66810#M4080</link>
      <description>&lt;P&gt;a few months ago i found a code snip on here of python calling the web api and i wanted to expand on it to make a more extensible library set.&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/celticcow/r80api-functions" target="_blank"&gt;https://github.com/celticcow/r80api-functions&lt;/A&gt;&lt;/P&gt;&lt;P&gt;has the library set with a test script ... a LOT of debug info is returned.&amp;nbsp; I've been able to take this and make web front ends via simple cgi calls to automate a lot of firewall object builds etc ..&lt;/P&gt;&lt;P&gt;the idea is to call a function like add_a_host that will take arguments and do the json work for you ... and check things like does a host already exist with this IP.&amp;nbsp; or add_a_host_with_group which will add a host ... if a host object does NOT exist with this IP already.&amp;nbsp; if a host with that IP exist it will add that host object to the group name that is passed as an argument.&amp;nbsp; &amp;nbsp;does similar things with networks and IP ranges.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;example:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;"""
add a host object and add it to a group
"""
def add_a_host_with_group(ip_addr, name, ip, group, sid):
    print("temp -- in add_a_host&amp;lt;br&amp;gt;")
    check_host_obj = {"type" : "host", "filter" : ip, "ip-only" : "true"}
    chkhst = api_call(ip_addr, "show-objects", check_host_obj, sid)

    if(chkhst['total'] == 0):
        #need new host
        if(name_exist(ip_addr, name, sid) == False):
            host_to_add = {"name" : name, "ip-address" : ip, "groups" : group, "color" : "light green"}
            out1 = api_call(ip_addr, "add-host", host_to_add, sid)
            print(json.dumps(out1))
        else:
            print("object with that name already exist")
    else:
        # host exist ... 
        print("host already exist")
        existing_host_name = chkhst['objects'][0]['name'] # name of existing host
        add_host_to_group_json = {
            "name" : group,
            "members" : {
                "add" : existing_host_name
            }
        }
        out1 = api_call(ip_addr, "set-group", add_host_to_group_json, sid)
        print(json.dumps(out1))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so in the main code you can just do something like:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;apifunctions.add_a_host_with_group(ip_addr, "test176", "192.168.176.200", "group1", sid)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;this will attempt to create a host object named "test176" with ip 192.168.176.200 into a group named "group1" unless something already exist with that IP and then it will add that.&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;ip_addr  = raw_input("Enter IP of MDS : ")
ip_cma   = raw_input("Enter IP of CMA : ")
user     = raw_input("Enter P1 User : ")
password = getpass.getpass('Enter P1 Password :')

sid = apifunctions.login(user, password, ip_addr, ip_cma)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;will get you sid for the login.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;hope this is helpful.&amp;nbsp; it's been a huge time savor for me when we're building out policies and i can create a web form for engineers to dump data into and it will create / search for them.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2019 21:11:41 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/Python-API-Lib-set/m-p/66810#M4080</guid>
      <dc:creator>Greg_Dunlap</dc:creator>
      <dc:date>2019-11-07T21:11:41Z</dc:date>
    </item>
    <item>
      <title>Re: Python API Lib set</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Python-API-Lib-set/m-p/66850#M4082</link>
      <description>&lt;P&gt;Not bad &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; But are you familiar with the official Python SDK for the management (and nowadays also the GAiA) API?&lt;/P&gt;&lt;P&gt;You can find it via the official Check Point Github Account, or by clicking &lt;A href="https://github.com/CheckPointSW/cp_mgmt_api_python_sdk" target="_self"&gt;here&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;Could save you some time as it nearly has all the features which somebody would need for automation tasks via the API.&lt;/P&gt;&lt;P&gt;It handles the requests and return values + also have own functions for login etc.&amp;nbsp;In addition to that it includes samples, that show you how to get started.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2019 08:11:26 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/Python-API-Lib-set/m-p/66850#M4082</guid>
      <dc:creator>Maik</dc:creator>
      <dc:date>2019-11-08T08:11:26Z</dc:date>
    </item>
  </channel>
</rss>

