<?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: FTD Bulk import objects, policies, NAT via api-explorer in Network Security</title>
    <link>https://community.cisco.com/t5/network-security/ftd-bulk-import-objects-policies-nat-via-api-explorer/m-p/4618150#M1090438</link>
    <description>&lt;P&gt;Here is the Python script I can use the CSV file if you have a bulk of hosts. The CSV file used should be stored in the same directory as the script, and it must be referenced when calling the script like so: &lt;STRONG&gt;python &amp;lt;script.py&amp;gt; &amp;lt;csvfile.csv&amp;gt;&lt;/STRONG&gt;. Here is the CSV file I used for the hosts:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;name,value,type,description
Host-1,10.0.1.10,host,Host 1
Host-2,10.0.2.10,host,Host 2&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Python Script&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;import csv
import json
import sys
import requests
import os

server = "https://192.168.45.45"
username = "api"
password = "cisco"
headers = {'Content-Type': 'application/json'}
api_auth_path = "/api/fmc_platform/v1/auth/generatetoken"
auth_url = server + api_auth_path
r = None

def yes_or_no(question):
    answer = raw_input(question + "(y/n): ").lower().strip()
    print("")
    while not(answer == "y" or answer == "yes" or \
    answer == "n" or answer == "no"):
        print("Input yes or no")
        answer = raw_input(question + "(y/n):").lower().strip()
        print("")
    if answer[0] == "y":
        return True
    else:
        return False

csvfile = open(sys.argv[1])
objects = csv.DictReader(csvfile)

print('\nThis script will attempt to create objects via an API call')
if yes_or_no('\nDo you want to continue?'):
    ()
else:
    quit()


try:
    print('\n\nPlease wait connection to FMC...')
    requests.packages.urllib3.disable_warnings()
    r = requests.post(auth_url, headers=headers,
    auth=requests.auth.HTTPBasicAuth(username,password), verify=False)
    auth_headers = r.headers
    auth_token = auth_headers.get('X-auth-access-token', default=None)
    if auth_token == None:
        print("auth_token not found. Exiting...")
        sys.exit()
except Exception as err:
    print ("Error in generating auth token --&amp;gt; "+str(err))
    sys.exit()

headers['X-auth-access-token'] = auth_token
print('...Connected! Auth token collected successfully (' + auth_token + (')\n'))
api_path = "/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/hosts"
url = server + api_path
if (url[-1] == '/'):
    url = url[:-1]

for object in objects:
    post_data = {
        "name": object["name"],
        "type": object["type"],
        "value": object["value"],
        "description": object["description"],
    }
    print('\n*************************************')
    print('Creating object: ' + object["name"])

    try:
        r = requests.post(url, data=json.dumps(post_data), headers=headers, verify=False)
        status_code = r.status_code
        resp = r.text
        log = open('api.log', 'a')
        print(" Status code: "+str(status_code))
        json_resp = json.loads(resp)
        log.write('\n---------------------------------------------------------------------\n')
        log.write(json.dumps(json_resp,sort_keys=True,indent=4, separators=(',', ': ')))

        if status_code == 201 or status_code == 202:
            print (" SUCCESS ")
        elif status_code == 400:
            print (" Message: ")  + resp + ('\n')
        else:
            r.raise_for_status()
            print (" Message: ")  + resp + ('\n')

    except requests.exceptions.HTTPError as err:
        print ("Error in connection --&amp;gt; "+str(err))
    finally:
        if r: r.close()

print('\nLog file "api.log" appended\n')&lt;/PRE&gt;
&lt;P&gt;api.log will logging what object it has created on the FMC.&lt;/P&gt;</description>
    <pubDate>Thu, 26 May 2022 08:47:13 GMT</pubDate>
    <dc:creator>Sheraz.Salim</dc:creator>
    <dc:date>2022-05-26T08:47:13Z</dc:date>
    <item>
      <title>FTD Bulk import objects, policies, NAT via api-explorer</title>
      <link>https://community.cisco.com/t5/network-security/ftd-bulk-import-objects-policies-nat-via-api-explorer/m-p/4617909#M1090425</link>
      <description>&lt;P&gt;Hi Experts,&lt;/P&gt;
&lt;P&gt;I am looking for some guidance to import network objects onto FTD via the api-explorer. I am able to create a single host using the GUI explorer. Any help on how the syntax would be if I want to import multiple objects via the api-explorer is appreciated.&lt;/P&gt;
&lt;P&gt;I tested and see that the following syntax for a single host, how do i modify this for multiple hosts?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;"name": "abc"&lt;/P&gt;
&lt;P&gt;"value": "x.x.x.x"&lt;/P&gt;
&lt;P&gt;"type": "networkobject"&lt;/P&gt;
&lt;P&gt;"subType": "HOST"&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2022 23:51:48 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/ftd-bulk-import-objects-policies-nat-via-api-explorer/m-p/4617909#M1090425</guid>
      <dc:creator>Arun2022</dc:creator>
      <dc:date>2022-05-25T23:51:48Z</dc:date>
    </item>
    <item>
      <title>Re: FTD Bulk import objects, policies, NAT via api-explorer</title>
      <link>https://community.cisco.com/t5/network-security/ftd-bulk-import-objects-policies-nat-via-api-explorer/m-p/4618150#M1090438</link>
      <description>&lt;P&gt;Here is the Python script I can use the CSV file if you have a bulk of hosts. The CSV file used should be stored in the same directory as the script, and it must be referenced when calling the script like so: &lt;STRONG&gt;python &amp;lt;script.py&amp;gt; &amp;lt;csvfile.csv&amp;gt;&lt;/STRONG&gt;. Here is the CSV file I used for the hosts:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;name,value,type,description
Host-1,10.0.1.10,host,Host 1
Host-2,10.0.2.10,host,Host 2&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Python Script&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;import csv
import json
import sys
import requests
import os

server = "https://192.168.45.45"
username = "api"
password = "cisco"
headers = {'Content-Type': 'application/json'}
api_auth_path = "/api/fmc_platform/v1/auth/generatetoken"
auth_url = server + api_auth_path
r = None

def yes_or_no(question):
    answer = raw_input(question + "(y/n): ").lower().strip()
    print("")
    while not(answer == "y" or answer == "yes" or \
    answer == "n" or answer == "no"):
        print("Input yes or no")
        answer = raw_input(question + "(y/n):").lower().strip()
        print("")
    if answer[0] == "y":
        return True
    else:
        return False

csvfile = open(sys.argv[1])
objects = csv.DictReader(csvfile)

print('\nThis script will attempt to create objects via an API call')
if yes_or_no('\nDo you want to continue?'):
    ()
else:
    quit()


try:
    print('\n\nPlease wait connection to FMC...')
    requests.packages.urllib3.disable_warnings()
    r = requests.post(auth_url, headers=headers,
    auth=requests.auth.HTTPBasicAuth(username,password), verify=False)
    auth_headers = r.headers
    auth_token = auth_headers.get('X-auth-access-token', default=None)
    if auth_token == None:
        print("auth_token not found. Exiting...")
        sys.exit()
except Exception as err:
    print ("Error in generating auth token --&amp;gt; "+str(err))
    sys.exit()

headers['X-auth-access-token'] = auth_token
print('...Connected! Auth token collected successfully (' + auth_token + (')\n'))
api_path = "/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/hosts"
url = server + api_path
if (url[-1] == '/'):
    url = url[:-1]

for object in objects:
    post_data = {
        "name": object["name"],
        "type": object["type"],
        "value": object["value"],
        "description": object["description"],
    }
    print('\n*************************************')
    print('Creating object: ' + object["name"])

    try:
        r = requests.post(url, data=json.dumps(post_data), headers=headers, verify=False)
        status_code = r.status_code
        resp = r.text
        log = open('api.log', 'a')
        print(" Status code: "+str(status_code))
        json_resp = json.loads(resp)
        log.write('\n---------------------------------------------------------------------\n')
        log.write(json.dumps(json_resp,sort_keys=True,indent=4, separators=(',', ': ')))

        if status_code == 201 or status_code == 202:
            print (" SUCCESS ")
        elif status_code == 400:
            print (" Message: ")  + resp + ('\n')
        else:
            r.raise_for_status()
            print (" Message: ")  + resp + ('\n')

    except requests.exceptions.HTTPError as err:
        print ("Error in connection --&amp;gt; "+str(err))
    finally:
        if r: r.close()

print('\nLog file "api.log" appended\n')&lt;/PRE&gt;
&lt;P&gt;api.log will logging what object it has created on the FMC.&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2022 08:47:13 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/ftd-bulk-import-objects-policies-nat-via-api-explorer/m-p/4618150#M1090438</guid>
      <dc:creator>Sheraz.Salim</dc:creator>
      <dc:date>2022-05-26T08:47:13Z</dc:date>
    </item>
    <item>
      <title>Re: FTD Bulk import objects, policies, NAT via api-explorer</title>
      <link>https://community.cisco.com/t5/network-security/ftd-bulk-import-objects-policies-nat-via-api-explorer/m-p/4651389#M1091873</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;How can we do it in FDM? Our firepower version is 6.4.0.15.&lt;/P&gt;
&lt;P&gt;Please help.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jul 2022 19:03:07 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/ftd-bulk-import-objects-policies-nat-via-api-explorer/m-p/4651389#M1091873</guid>
      <dc:creator>engineer467</dc:creator>
      <dc:date>2022-07-15T19:03:07Z</dc:date>
    </item>
  </channel>
</rss>

