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

Powershell Script to use with mgmt_cli

Thought I would add this to show how to automate adding objects using a script on Windows machines.

1. create hosts.csv with two columns headed host ipv4 and color eg

host,ipv4,color
node1,1.1.1.2,green
node2,2.2.2.3,yellow

2. Open powershell and cd to the Checkpoint mgmt_cli location

cd "C:\Program Files (x86)\CheckPoint\SmartConsole\R80\PROGRAM"

3. Edit the below to point to the location of your csv file.
Also edit the management server ip, username and password.  These could be passed as $variables ??

4. Paste all the below lines between ~#~#~  into a powershell.

~#~#~#~#~#~

$hosts = Import-csv c:\path\to\hosts.csv
  
#This will process the CSV row by row. Each row contains information to create ahost object with a name and ipv4-address
 
foreach ($node in $hosts)
 
   {


   $name = $node.host
   $ipv4 = $node.ipv4
   $color = $node.color
     
   #create hosts from csv file, management server (-m) username (-u) and password (-p) are required fields.
 
   .\mgmt_cli add host name $name ip-address $ipv4 color $color -m <MGMT_SERVER_HERE> -u <USER_HERE> -p <PASSWORD_HERE>

  } 
  
~#~#~#~#~#~

This will publish each line after every pass.

7 Replies
Uri_Bialik

Did you know:  mgmt_cli can accept csv files as inputs using the --batch option.

The first row should contain the argument names and the rows below it should hold the values for these parameters.

So an equivalent solution to the powershell script could look like this:

data.csv:

nameipv4-addresscolor
host1192.168.35.1black
host2192.168.35.2red
host3192.168.35.3blue

mgmt_cli add host --batch data.csv -u <username> -p <password> -m <management server>

This can work with any type of command not just "add host" : simply replace the column names with the ones relevant to the command you need.

Eric_Beasley
Employee
Employee

You can stream line the authentication elements of that script/command line with the "-s <session_id_file>" instead of having to provide "-u <username> -p <password> -m <management server>", like this:

mgmt_cli login user <smartconsole_administrator_name> -m <management server> > id.txt

...

mgmt_cli add host --batch data.csv -s id.txt

...

mgmt_cli publish -s id.txt

mgmt_cli logout -s id.txt

This will also then publish the changes and logout, closing that session, as it relates to the session id file.

mgmt_cli defaults to using the management server at 127.0.0.1 or as defined by the environment variable "MGMT_CLI_MANAGEMENT".

Richard_Mason
Participant

No missed that, makes it even better and easier then Smiley Happy

I just wanted to show options for powershell as previous posts and scripts tend to be linux based.  Due to smartconsole on windows we should all have powershell to work with.

Not applicable

Love the API. Great forum. Thanks.

BUT:

API documentation found in misc. forum posts is bad.

API without proper online documentation diminishes the power of the API

See aws.amazon.com for what APIs with documentation should look like.

Thanks

dreez

Eric_Beasley
Employee
Employee

Your feedback is probably best added here:  Management API reference where Yonatan Philip is asking for input.

0 Kudos
Juan_Concepcion
Advisor

I tried to add all of the mgmt_cli commands into a file and execute but end up with following error:

Error: The parameters of login command should be provided in pairs (key and value). You have provided an odd number of parameters which suggests that you are probably missing a parameter.

If I copy and paste the same commands into cli it works fine??

0 Kudos
Robert_Decker
Advisor

Can you please specify which commands and in which order?

Please be more specific and informative.

CLI window in SmartConsole works differently than mgmt_cli tool on the management server.

Robert.

0 Kudos

Leaderboard

Epsum factorial non deposit quid pro quo hic escorol.

Upcoming Events

    CheckMates Events