cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
866
Views
0
Helpful
0
Replies

FMC Bulk Add Hosts via REST API - Issues, Need Help!

Following the directions here:

 

https://www.cisco.com/c/en/us/support/docs/security/firepower-management-center/215972-push-objects-in-bulk-to-fmc-using-rest-a.html

 

I'm attempting to build a Python script that will allow me to bulk add hosts via the REST API.

 

Here's my script as it sits today:

 

import requests
import csv
import json
from requests.auth import HTTPBasicAuth
from getpass import getpass

address = input("FMC-IP-ADDRESS")
username = input ("USERNAME")
password = getpass("PASSWORD")

api_uri = "/api/fmc_platform/v1/auth/generatetoken"
url = "https://" + address + api_uri

response = requests.request("POST", url, verify=False, auth=HTTPBasicAuth(username, password))


accesstoken = response.headers["X-auth-access-token"]
refreshtoken = response.headers["X-auth-refresh-token"]
DOMAIN_UUID = response.headers["DOMAIN_UUID"]

csvFilePath = input("C:\vips.csv")


host = []

with open(csvFilePath, encoding='utf-8-sig') as csvf:
	csvReader = csv.DictReader(csvf)

for rows in csvReader:
	if rows['type'] == "Host":
		host.append(rows)

host_payload = json.dumps(host)

host_api_uri = "/api/fmc_config/v1/domain/" + DOMAIN_UUID + "/object/hosts?bulk =true"
host_url = "https://" + address + host_api_uri
headers = { 'Content-Type': 'application/json', 'x-auth-access-token': accesstoken }

if host != []:
	response = requests.request("POST", host_url, headers=headers, data = host_payload, verify = False)
else :
    print("Please Validate that the CSV file provided is correct or at correct location")
	
if response.status_code == 201 or response.status_code == 202:
	print("Host Objects successfully pushed")
else:
	print("Host Object creation failed")
	
logfile = "requestlog.txt"
log = open(logfile,"w+")
log.write(response.text)
log.close

So, when I run the script, it brings up the IP, I hit enter, then it brings up the username, enter, password, enter.

 

Then it generates the following errors:

 

Traceback (most recent call last):
  File "C:\bulk-add.py", line 14, in <module>
    response = requests.request("POST", url, verify=False, auth=HTTPBasicAuth(username, password))
  File "C:\Program Files\Python37\lib\site-packages\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Program Files\Python37\lib\site-packages\requests\sessions.py", line
498, in request
    prep = self.prepare_request(req)
  File "C:\Program Files\Python37\lib\site-packages\requests\sessions.py", line
441, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "C:\Program Files\Python37\lib\site-packages\requests\models.py", line 30
9, in prepare    self.prepare_url(url, params)
  File "C:\Program Files\Python37\lib\site-packages\requests\models.py", line 38
6, in prepare_url
    raise InvalidURL("Invalid URL %r: No host supplied" % url)
requests.exceptions.InvalidURL: Invalid URL 'https:///api/fmc_platform/v1/auth/generatetoken': No host supplied

Is anyone that understands Python and the REST API able to assist?

 

Thank you very much!!

0 Replies 0
Review Cisco Networking products for a $25 gift card