cancel
Showing results for 
Search instead for 
Did you mean: 
Create a Post
Highlighted
Nickel

WebAPI one time script stucks at 10%

Hello. I wrote python script to get result of  cpstat fw command:

def api_call(ip_addr, port, command, json_payload, sid):
    url = 'https://' + ip_addr + ':' + str(port) + '/web_api/' + command
    if sid == '':
        request_headers = {'Content-Type' : 'application/json'}
    else:
        request_headers = {'Content-Type' : 'application/json', 'X-chkp-sid' : sid}
    r = requests.post(url,data=json.dumps(json_payload), headers=request_headers, verify=False)
    return r.json()


def login(user,password):
    payload = {'user':user, 'password' : password}
    response = api_call(mgmt_server, 443, 'login',payload, '')
    return response["sid"]

def run_script(script_name, script_command,sid):
    payload = {'script-name':script_name, 'script':script_command, 'targets':otr_gws}
    response = api_call(mgmt_server, 443, 'run-script', payload, sid)
    return response

def show_task(task_id, sid):
    payload = {'task-id':task_id, 'details-level':'full'}
    response = api_call(mgmt_server, 443, 'show-task', payload, sid)
    return response


def main():
    sid = login(mgmt_username,mgmt_password)
    print("session id: " + sid)

    cpstat = run_script("cpstat","cpstat fw",sid)
    
    print(cpstat)
    print("------------------------------")
    print("cpstat result: " + json.dumps(cpstat))
    print("------------------------------")
    
    task_id=cpstat["tasks"][0]["task-id"]
    result=show_task(task_id, sid)
    print(result)


    logout_result = api_call(mgmt_server, 443,"logout", {},sid)
    print("logout result: " + json.dumps(logout_result))


if __name__ == '__main__':
    main()

 

2019-04-25_16-41-38.png

 

But when I run script it just stucks on the management server and nothing hashappened. What could be the problem?


P.S. The main idea is I want to make script to check ISP Redundancy status and to switch ISP Manually.

0 Kudos
4 Replies
Highlighted
Admin
Admin

Re: WebAPI one time script stucks at 10%

I'm not a python programmer, but where are you defining the gateways on which to execute the cpstat fw command?
I don't see you passing it to your run_script function or defined anywhere in the script you pasted above.
0 Kudos
Highlighted
Nickel

Re: WebAPI one time script stucks at 10%

The first part of script is:

 

import requests
import json

mgmt_username = ‘user’
mgmt_password = ‘pass’
mgmt_server = ‘x.x.x.x’
otr_gws = [‘gw-2’]

 

0 Kudos
Highlighted
Admin
Admin

Re: WebAPI one time script stucks at 10%

I don’t see you doing a base64 decoding of the command output either.

See: https://community.checkpoint.com/t5/Developers-API-CLI/run-script-output/m-p/2835#M213

0 Kudos
Highlighted

Re: WebAPI one time script stucks at 10%