<?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: mgmt_cli validation check in API / CLI Discussion</title>
    <link>https://community.checkpoint.com/t5/API-CLI-Discussion/mgmt-cli-validation-check/m-p/243851#M8982</link>
    <description>&lt;P&gt;I know this is a little late for this party, but I would highly suggest you do this with Ansible and the Check Point modules. &amp;nbsp;It handles all of this backend dirty work for you, plus you gain consistency along the way. &amp;nbsp;You can also structure your playbooks to collect before/after states and completely discard the entire operation, giving you the chance to fully test the entire sequence so you can run it at a later date (such as "maintenance weekend", when you're already short on time and short on patience).&lt;/P&gt;
&lt;P&gt;Shameless plug: I have a series (link in my signature line below) on setting up an Ansible host, dependencies, and introductions to playbooks, etc. &amp;nbsp;First 5 episodes are available now, with more coming.&lt;/P&gt;</description>
    <pubDate>Sat, 15 Mar 2025 04:29:25 GMT</pubDate>
    <dc:creator>Duane_Toler</dc:creator>
    <dc:date>2025-03-15T04:29:25Z</dc:date>
    <item>
      <title>mgmt_cli validation check</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/mgmt-cli-validation-check/m-p/239535#M8918</link>
      <description>&lt;P&gt;Hi Check Mates,&lt;/P&gt;
&lt;P&gt;Been working on a simple standard change which I wanted to create a simple automation task.&lt;/P&gt;
&lt;P&gt;I have been working on this in three steps.&lt;/P&gt;
&lt;P&gt;1) Using CMD via SmartConsole adding a Network Object and then add this to a Network Group object.&amp;nbsp; That simply worked.&lt;BR /&gt;&lt;BR /&gt;I am running these the commands&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;add network name "TestObj" subnet xxx.xxx.xxx.xxx subnet-mask xxx.xxx.xxx.xxx
set group name "Proxy_Access" members.add "TestObj"&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;publish&lt;BR /&gt;Install policy&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) Working on a shell script to be executed on SMS server doing exactly the same steps above. This works too.&lt;BR /&gt;&lt;BR /&gt;I have created a script name "add_object_to_group.sh" and given right to execute with "chmod 777".&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Script looks like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;#/bin/bash
clear

# ask for credentials from user
echo "Please enter your username and password"
read -p "Enter username and press [ENTER]: " USER
read -s -p "Enter password and press [ENTER]: " PASS
echo

mgmt_cli login user ${USER} password ${PASS} &amp;gt; id.txt

# in case of an error: print to screen the error message and abort
if [ $? -ne 0 ]; then
echo "Login command failed."
cat id.txt
exit 1
fi

# Ask for user to enter a Name of Network Object
echo
echo "Please enter a Network Object Name e.g TestObj "
read -p "Enter Network Object Name  eg. TestObj [ENTER] : " NetworkObjectName

# Ask for user to enter a Source Subnet for Network Object Name
echo
echo "Please define the Network Object Network subnet"
read -p "Enter subnet eg. 10.80.212.0 [ENTER] : " NetworkObjectSubNet

while [[ ! "$NetworkObjectSubNet" =~ '^((25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9]{1,2})[.]){3}(25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9]{1,2})$' ]]; do
    read -p "Not a valid IP Subnet. Re-enter: " NetworkObjectSubNet
done

read -p "Enter subnet-mask eg. 255.255.255.0 [ENTER] : " NetworkObjectSubNetMask

while [[ ! "$NetworkObjectSubNetMask" =~ '^((255)\.(0|128|192|224|240|248|252|254|255)\.(0|128|192|224|240|248|252|254|255)\.(0|128|192|224|240|248|252|254|255))$' ]]; do
    read -p "Not an Subnet Mask. Re-enter: " NetworkObjectSubNetMask
done

# Run the mgmt_cli against CP SMS Server
mgmt_cli -s id.txt add network name "${NetworkObjectName}" subnet "${NetworkObjectSubNet}" subnet-mask "${NetworkObjectSubNetMask}"
mgmt_cli -s id.txt set group name "Proxy_Access" members.add "${NetworkObjectName}"

# Publish the creation of Network Object and add it to Network Group
mgmt_cli publish -s id.txt
mgmt_cli logout -s id.txt
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3) Moving script our from SMS server to be run in Linux jumphost. I haven't yet worked on this because I want to complete my above steps first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am lacking some kind of error handling in my script and I haven't been able to find any examples of being able to do so.&lt;BR /&gt;I know I have a validation check for login if anything goes wrong. Or can I use the same method from login validation in each of the steps?&lt;/P&gt;
&lt;P&gt;Example of error handling would be.&lt;/P&gt;
&lt;P&gt;1) In case Network Object Name exist.&lt;/P&gt;
&lt;P&gt;2) in case of two objects have then same subnet in use. In SmartConsole you can have different object names with the same subnet.&lt;/P&gt;
&lt;P&gt;3) I want to be sure that I am not trying to add a network object to a network group if it already exist etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyone can help me or give me a direction?&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jan 2025 10:35:29 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/mgmt-cli-validation-check/m-p/239535#M8918</guid>
      <dc:creator>Kim_Moberg</dc:creator>
      <dc:date>2025-01-24T10:35:29Z</dc:date>
    </item>
    <item>
      <title>Re: mgmt_cli validation check</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/mgmt-cli-validation-check/m-p/239542#M8919</link>
      <description>&lt;P&gt;I want to be able to do validation check when running mgmt_cli commands&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;mgmt_cli -s id.txt add network name "${NetworkObjectName}" subnet "${NetworkObjectSubNet}" subnet-mask "${NetworkObjectSubNetMask}"
mgmt_cli -s id.txt set group name "Proxy_Access" members.add "${NetworkObjectName}"&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jan 2025 10:08:43 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/mgmt-cli-validation-check/m-p/239542#M8919</guid>
      <dc:creator>Kim_Moberg</dc:creator>
      <dc:date>2025-01-24T10:08:43Z</dc:date>
    </item>
    <item>
      <title>Re: mgmt_cli validation check</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/mgmt-cli-validation-check/m-p/239580#M8921</link>
      <description>&lt;P&gt;Rather than a static "id.txt" for the session cookie, I would use a variable populated by mktemp. That way, multiple people could use the tool at the same time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;sessionCookie=$(mktemp)
mgmt_cli login user "${USER}" password "${PASS}" &amp;gt;"${sessionCookie}"
...
...
mgmt_cli -s "${sessionCookie}" add network name ...&lt;/LI-CODE&gt;
&lt;P&gt;As for error handling, you would have to actually read the messages. Something like this:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;apiOut="$(mgmt_cli -f json -s "${sessionCookie}" add network name ...)"
if [ "1" = "$0" ];then
errorText="$(echo "${apiOut}" | jq '.some.path.here')"
case "${errorText}" in
	"Some error string")
		# Handle this error.
		;;
	"A different error string")
		# Handle a different error.
		;;
	*)
		# This is the default case, where errors you don't specifically detect end up.
		echo "Got an error I don't know how to handle: ${errorText}"
		exit 1
		;;
esac
fi&lt;/LI-CODE&gt;
&lt;P&gt;You would replace '.some.path.here' with the path in an API call error to the description of the error. I forget what it is off the top of my head. You would then have an item in the case statement for each error you want to handle.&lt;/P&gt;
&lt;P&gt;Note that adding a network to a group which already contains it isn't harmful. I wouldn't bother trying to detect that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jan 2025 21:09:02 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/mgmt-cli-validation-check/m-p/239580#M8921</guid>
      <dc:creator>Bob_Zimmerman</dc:creator>
      <dc:date>2025-01-24T21:09:02Z</dc:date>
    </item>
    <item>
      <title>Re: mgmt_cli validation check</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/mgmt-cli-validation-check/m-p/239644#M8922</link>
      <description>&lt;P&gt;Try to use:&lt;/P&gt;
&lt;P&gt;nohup mgmt_cli -s id.txt add network name dummy subnet 1.1.1.0 subnet-mask 255.255.255.0 2&amp;gt;/dev/null &amp;amp;nohup mgmt_cli -s id.txt add network name "${NetworkObjectName}" subnet "${NetworkObjectSubNet}" subnet-mask "${NetworkObjectSubNetMask}" 2&amp;gt;/dev/null &amp;amp;&lt;BR /&gt;nohup mgmt_cli -s id.txt set group name "Proxy_Access" members.add "${NetworkObjectName}" 2&amp;gt;/dev/null &amp;amp;&lt;/P&gt;
&lt;P&gt;This will write all output to "nohup.out".&lt;/P&gt;
&lt;P&gt;If all is ok it wouldn't write anything to it. If you have an issue it will write it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So for example, in the script after running the command while having a another object of the same name:&lt;/P&gt;
&lt;P&gt;Please enter your username and password&lt;BR /&gt;Enter username and press [ENTER]: aa&lt;BR /&gt;Enter password and press [ENTER]:&lt;/P&gt;
&lt;P&gt;Please enter a Network Object Name e.g TestObj&lt;BR /&gt;Enter Network Object Name eg. TestObj [ENTER] : dummy&lt;/P&gt;
&lt;P&gt;Please define the Network Object Network subnet&lt;BR /&gt;Enter subnet eg. 10.80.212.0 [ENTER] : 1.2.6.0&lt;BR /&gt;Enter subnet-mask eg. 255.255.255.0 [ENTER] : 255.255.255.0&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;---------------------------------------------&lt;BR /&gt;Time: [18:34:09] 26/1/2025&lt;BR /&gt;---------------------------------------------&lt;BR /&gt;"Publish operation" succeeded (100%)&lt;BR /&gt;tasks:&lt;BR /&gt;- task-id: "01234567-89ab-cdef-91b4-d9616ebd3c7b"&lt;BR /&gt;task-name: "Publish operation"&lt;BR /&gt;status: "succeeded"&lt;BR /&gt;progress-percentage: 100&lt;BR /&gt;suppressed: false&lt;BR /&gt;task-details:&lt;BR /&gt;- publishResponse:&lt;BR /&gt;numberOfPublishedChanges: 0&lt;BR /&gt;mode: "async"&lt;BR /&gt;revision: "d214d4f0-1644-49e4-9ddd-60ebf4f67ab6"&lt;/P&gt;
&lt;P&gt;message: "OK"&lt;/P&gt;
&lt;P&gt;[Expert@MGMT:0]# cat nohup.out&lt;BR /&gt;code: "err_validation_failed"&lt;BR /&gt;message: "Validation failed with 1 error"&lt;BR /&gt;errors:&lt;BR /&gt;- message: "More than one object named 'dummy' exists."&lt;/P&gt;
&lt;P&gt;[Expert@MGMT:0]#&lt;/P&gt;</description>
      <pubDate>Sun, 26 Jan 2025 16:35:22 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/mgmt-cli-validation-check/m-p/239644#M8922</guid>
      <dc:creator>Amir_Senn</dc:creator>
      <dc:date>2025-01-26T16:35:22Z</dc:date>
    </item>
    <item>
      <title>Re: mgmt_cli validation check</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/mgmt-cli-validation-check/m-p/243851#M8982</link>
      <description>&lt;P&gt;I know this is a little late for this party, but I would highly suggest you do this with Ansible and the Check Point modules. &amp;nbsp;It handles all of this backend dirty work for you, plus you gain consistency along the way. &amp;nbsp;You can also structure your playbooks to collect before/after states and completely discard the entire operation, giving you the chance to fully test the entire sequence so you can run it at a later date (such as "maintenance weekend", when you're already short on time and short on patience).&lt;/P&gt;
&lt;P&gt;Shameless plug: I have a series (link in my signature line below) on setting up an Ansible host, dependencies, and introductions to playbooks, etc. &amp;nbsp;First 5 episodes are available now, with more coming.&lt;/P&gt;</description>
      <pubDate>Sat, 15 Mar 2025 04:29:25 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/mgmt-cli-validation-check/m-p/243851#M8982</guid>
      <dc:creator>Duane_Toler</dc:creator>
      <dc:date>2025-03-15T04:29:25Z</dc:date>
    </item>
  </channel>
</rss>

