Create a Post
cancel
Showing results for 
Search instead for 
Did you mean: 
Peter_Odedina
Participant

Problem with Python Script and Post Request

Jump to solution

I wrote a python script to login to the management server read a csv file that contain a list of name, IP address and Color. But I keep getting that my payload is not Json. Please see my code below and the error not sure what else to do.

 

import requests, json, csv, base64
MGMT = '192.168.15.4'
url = 'https://192.168.15.4/web_api/v1.6.1/login'
payload= "{\r\n \"user\" : \"username\",\r\n \"password\" : \"*******\"\r\n}"
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload, verify=False)
response_dict = json.loads(response.text)
session_id = response_dict['sid']
print(response_dict['sid'])

url = 'https://192.168.15.4/web_api/v1.6.1/publish'
payload="{ }"
headers = {
'Content-Type': 'application/json',
'X-chkp-sid': session_id
}
response = requests.request("POST", url, headers=headers, data=payload, verify=False)
#data = open("C:/Users/OdedinaP/Python_Learning/Add-Multi-Host/add_mutli_host.csv", 'rb')
with open("C:/Users/OdedinaP/Python_Learning/Add-Multi-Host/add_mutli_host.csv", 'r') as data:
csv_reader = csv.DictReader(data)
csv_data = list(csv_reader)
for index, item in enumerate(csv_data):
csvJson_data = {json.dumps(csv_data)}
for index, item in enumerate(csvJson_data):
payload = (csvJson_data)
headers = {
'Content-Type': 'application/json',
'X-chkp-sid': session_id
}
print(payload)
data = base64.encodebytes('payload'.encode())
print(data)
url = 'https://192.168.15.4/web_api/v1.6.1/add-host'
response = requests.request("POST", url, headers=headers, data=data, verify=False)
print(response.text)

 

 

Below is my output with the error:

 

 

$ python add_multi_host.py
C:\Users\OdedinaP\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py:981: InsecureRequestWarning: Unverified HTTPS request is being made to host '192.168.15.4'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
warnings.warn(
C:\Users\OdedinaP\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py:981: InsecureRequestWarning: Unverified HTTPS request is being made to host '192.168.15.4'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
warnings.warn(
C:\Users\OdedinaP\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py:981: InsecureRequestWarning: Unverified HTTPS request is being made to host '192.168.15.4'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
warnings.warn(
atVmfEs8DGqoRaEREqHdiV9uVbI6O0KsJnisBDnDtOc
{'[{"name": "PC1", "ip-address": "192.168.10.1", "color": "Yellow"}, {"name": "PC2", "ip-address": "192.168.10.2", "color": "Yellow"}, {"name": "PC3", "ip-address": "192.168.10.3", "color": "Yellow"}, {"name": "PC4", "ip-address": "192.168.10.4", "color": "Yellow"}, {"name": "PC5", "ip-address": "192.168.10.5", "color": "Yellow"}, {"name": "PC6", "ip-address": "192.168.10.6", "color": "Yellow"}, {"name": "PC7", "ip-address": "192.168.10.7", "color": "Yellow"}, {"name": "PC8", "ip-address": "192.168.10.8", "color": "Yellow"}, {"name": "PC9", "ip-address": "192.168.10.9", "color": "Yellow"}, {"name": "PC10", "ip-address": "192.168.10.10", "color": "Yellow"}, {"name": "PC11", "ip-address": "192.168.10.11", "color": "Yellow"}, {"name": "PC12", "ip-address": "192.168.10.12", "color": "Yellow"}, {"name": "PC13", "ip-address": "192.168.10.13", "color": "Yellow"}, {"name": "PC14", "ip-address": "192.168.10.14", "color": "Yellow"}, {"name": "PC15", "ip-address": "192.168.10.15", "color": "Yellow"}, {"name": "PC16", "ip-address": "192.168.10.16", "color": "Yellow"}, {"name": "PC17", "ip-address": "192.168.10.17", "color": "Yellow"}, {"name": "PC18", "ip-address": "192.168.10.18", "color": "Yellow"}, {"name": "PC19", "ip-address": "192.168.10.19", "color": "Yellow"}, {"name": "PC20", "ip-address": "192.168.10.20", "color": "Yellow"}]'}
b'cGF5bG9hZA==\n'
{
"code" : "generic_err_invalid_syntax",
"message" : "Payload is not a valid JSON"
}

 

 

 

0 Kudos
1 Solution

Accepted Solutions
Peter_Odedina
Participant

Thanks for the mgmt_cli works for me for the first time. I just need to specify the correct path to the file and run it from the right directory. I guess you are right i don't need the python script after all. I share the command below for the benefit of all:

 

C:\Program Files (x86)\CheckPoint\SmartConsole\R80.40\PROGRAM>mgmt_cli -m 192.168.15.4 -u username -p ***** add host -b C:\Users\OdedinaP\Python_Learning\Add-Multi-Host\add_mutli_host.csv


---------------------------------------------
Time: [10:26:43] 26/10/2021
---------------------------------------------
"Publish operation" in progress (80%)


---------------------------------------------
Time: [10:26:53] 26/10/2021
---------------------------------------------
"Publish operation" succeeded (100%)

C:\Program Files (x86)\CheckPoint\SmartConsole\R80.40\PROGRAM>

 

View solution in original post

0 Kudos
4 Replies
PhoneBoy
Admin
Admin

First of all, you realize you don't even need to write a Python script for this, right?
You can import using mgmt_cli and a specially crafted CSV. 
There are several examples in the community, but here's one recent discussion: https://community.checkpoint.com/t5/API-CLI-Discussion/MGMT-CLI-Object-import/m-p/108135#M5494 

To your script: it's not clear to me if you're actually looping through the items and submitting each one individually to add-host or if you're trying to submit them all at once with a single call.
The output suggests you're trying to do the latter, which definitely work work.

0 Kudos
Peter_Odedina
Participant

Thanks for your response for some reason i have not been able to get the mgm_cli api call work with csv file call. It always return one error or the other that is what push me to try it in python but I hit another brick wall. 

Back to the script,  yes I was trying to submit all the items in the csv file all at once to create the objects. I was able to use the run iterative tool in Postman to achieve the same result but I need to make several API calls to login, publish, add object, publish again etc to make it work. So i was hoping to just put all these api calls together in one python script. 

0 Kudos
PhoneBoy
Admin
Admin

The order of operations should be something like:

  • Login
  • "Do Stuff"
  • Publish
  • Logout

In the "do stuff" step, you'd loop through and do multiple add-host commands.
And yes, that's how the API works.
That said, we added support for a batch mode in R80.40.
See: https://sc1.checkpoint.com/documents/latest/APIs/index.html#cli/add-objects-batch~v1.6%20 

As for the CSV file method, the issue everyone has is the first line of the CSV file.
Given the example you provided, the first line would be: name,ip-address,color

0 Kudos
Peter_Odedina
Participant

Thanks for the mgmt_cli works for me for the first time. I just need to specify the correct path to the file and run it from the right directory. I guess you are right i don't need the python script after all. I share the command below for the benefit of all:

 

C:\Program Files (x86)\CheckPoint\SmartConsole\R80.40\PROGRAM>mgmt_cli -m 192.168.15.4 -u username -p ***** add host -b C:\Users\OdedinaP\Python_Learning\Add-Multi-Host\add_mutli_host.csv


---------------------------------------------
Time: [10:26:43] 26/10/2021
---------------------------------------------
"Publish operation" in progress (80%)


---------------------------------------------
Time: [10:26:53] 26/10/2021
---------------------------------------------
"Publish operation" succeeded (100%)

C:\Program Files (x86)\CheckPoint\SmartConsole\R80.40\PROGRAM>

 

View solution in original post

0 Kudos