راځئ چې ټول پیرامیټونه او د دوی ډیفالټ ارزښتونه وګورو چې د API سرور سره وصل کیدو پرمهال کارول کیدی شي:
د APIClientArgs ټولګي د __init__ میتود دلیلونه
class APIClientArgs:
"""
This class provides arguments for APIClient configuration.
All the arguments are configured with their default values.
"""
# port is set to None by default, but it gets replaced with 443 if not specified
# context possible values - web_api (default) or gaia_api
def __init__(self, port=None, fingerprint=None, sid=None, server="127.0.0.1", http_debug_level=0,
api_calls=None, debug_file="", proxy_host=None, proxy_port=8080,
api_version=None, unsafe=False, unsafe_auto_accept=False, context="web_api"):
self.port = port
# management server fingerprint
self.fingerprint = fingerprint
# session-id.
self.sid = sid
# management server name or IP-address
self.server = server
# debug level
self.http_debug_level = http_debug_level
# an array with all the api calls (for debug purposes)
self.api_calls = api_calls if api_calls else []
# name of debug file. If left empty, debug data will not be saved to disk.
self.debug_file = debug_file
# HTTP proxy server address (without "http://")
self.proxy_host = proxy_host
# HTTP proxy port
self.proxy_port = proxy_port
# Management server's API version
self.api_version = api_version
# Indicates that the client should not check the server's certificate
self.unsafe = unsafe
# Indicates that the client should automatically accept and save the server's certificate
self.unsafe_auto_accept = unsafe_auto_accept
# The context of using the client - defaults to web_api
self.context = context
له دې وروسته تاسو کولی شئ د ټولګي متغیرات او میتودونه وکاروئ API ځواب(دواړه د شرایطو مدیر دننه او بهر). په ټولګي کې API ځواب 4 میتودونه او 5 متغیرونه مخکې تعریف شوي دي؛ موږ به په خورا تفصیل سره په خورا مهم ډول وګورو.
برياليتوب
د پیل کولو لپاره، دا به ښه نظر وي چې ډاډ ترلاسه کړئ چې د API کال بریالی و او پایله یې بیرته راستانه شوه. د دې لپاره یوه طریقه شتون لري برياليتوب:
In [49]: api_versions.success
Out[49]: True
ریښتیا راګرځي که چیرې د API کال بریالۍ وي (د ځواب کوډ - 200) او غلط که بریالی نه وي (د کوم بل ځواب کوډ). دا د ځواب کوډ پراساس مختلف معلومات ښودلو لپاره د API زنګ وروسته سمدلاسه کارول اسانه دي.
if api_ver.success:
print(api_versions.data)
else:
print(api_versions.err_message)
په دې حالت کې، دا ممکن اړین وي چې د بریالیتوب حالت ارزښت بدل کړي. په تخنیکي توګه، تاسو کولی شئ هلته هر څه واچوئ، حتی یو منظم تار. مګر یو ریښتینی مثال به د دې پیرامیټر بیا تنظیم کول د ځینې شرایطو لاندې غلط ته. لاندې، مثال ته پام وکړئ کله چې د مدیریت سرور کې دندې پرمخ ځي، مګر موږ به دا غوښتنه ناکامه وګڼو (موږ به د بریالیتوب متغیر ترتیب کړو. غلط، سره له دې چې د API کال بریالی و او کوډ 200 بیرته راستانه شو).
for task in task_result.data["tasks"]:
if task["status"] == "failed" or task["status"] == "partially succeeded":
task_result.set_success_status(False)
break
ځواب()
د ځواب میتود تاسو ته اجازه درکوي د ځواب کوډ (status_code) او د غبرګون بدن (body) سره قاموس وګورئ.
هر څه همداسې دي README.md د کنټرول سرور څخه مستقیم د Python سکریپټونو چلولو څرنګوالي په اړه معلومات لري. دا اسانه کیدی شي کله چې تاسو نشئ کولی د بل ماشین څخه د API سرور سره وصل شئ. ما یو شپږ دقیقې ویډیو ثبت کړه چې په هغه کې زه د ماډل نصبولو ته ګورم cpapi او په کنټرول سرور کې د Python سکریپټونو چلولو ځانګړتیاوې. د مثال په توګه، یو سکریپټ چلول کیږي چې د یوې دندې لپاره د نوي دروازې ترتیب اتومات کوي لکه د شبکې پلټنې د امنیت معاینه. د هغه ځانګړتیاو په مینځ کې چې ما ورسره معامله کړې وه: فنکشن لاهم په Python 2.7 کې نه دی څرګند شوی وتنی، نو د هغه معلوماتو پروسس کولو لپاره چې کارونکي یې ننوځي ، یو فنکشن کارول کیږي raw_input. که نه نو، کوډ د نورو ماشینونو څخه د پیل کولو لپاره ورته دی، یوازې د فعالیت کارول خورا اسانه دي login_as_root، نو د مدیریت سرور خپل کارن نوم ، پټنوم او IP پته بیا مشخص نه کړئ.
د امنیت چیک اپ ګړندي تنظیم کولو لپاره سکریپټ
from __future__ import print_function
import getpass
import sys, os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from cpapi import APIClient, APIClientArgs
def main():
with APIClient() as client:
# if client.check_fingerprint() is False:
# print("Could not get the server's fingerprint - Check connectivity with the server.")
# exit(1)
login_res = client.login_as_root()
if login_res.success is False:
print("Login failed:n{}".format(login_res.error_message))
exit(1)
gw_name = raw_input("Enter the gateway name:")
gw_ip = raw_input("Enter the gateway IP address:")
if sys.stdin.isatty():
sic = getpass.getpass("Enter one-time password for the gateway(SIC): ")
else:
print("Attention! Your password will be shown on the screen!")
sic = raw_input("Enter one-time password for the gateway(SIC): ")
version = raw_input("Enter the gateway version(like RXX.YY):")
add_gw = client.api_call("add-simple-gateway", {'name' : gw_name, 'ipv4-address' : gw_ip, 'one-time-password' : sic, 'version': version.capitalize(), 'application-control' : 'true', 'url-filtering' : 'true', 'ips' : 'true', 'anti-bot' : 'true', 'anti-virus' : 'true', 'threat-emulation' : 'true'})
if add_gw.success and add_gw.data['sic-state'] != "communicating":
print("Secure connection with the gateway hasn't established!")
exit(1)
elif add_gw.success:
print("The gateway was added successfully.")
gw_uid = add_gw.data['uid']
gw_name = add_gw.data['name']
else:
print("Failed to add the gateway - {}".format(add_gw.error_message))
exit(1)
change_policy = client.api_call("set-access-layer", {"name" : "Network", "applications-and-url-filtering": "true", "content-awareness": "true"})
if change_policy.success:
print("The policy has been changed successfully")
else:
print("Failed to change the policy- {}".format(change_policy.error_message))
change_rule = client.api_call("set-access-rule", {"name" : "Cleanup rule", "layer" : "Network", "action": "Accept", "track": {"type": "Detailed Log", "accounting": "true"}})
if change_rule.success:
print("The cleanup rule has been changed successfully")
else:
print("Failed to change the cleanup rule- {}".format(change_rule.error_message))
# publish the result
publish_res = client.api_call("publish", {})
if publish_res.success:
print("The changes were published successfully.")
else:
print("Failed to publish the changes - {}".format(install_tp_policy.error_message))
install_access_policy = client.api_call("install-policy", {"policy-package" : "Standard", "access" : 'true', "threat-prevention" : 'false', "targets" : gw_uid})
if install_access_policy.success:
print("The access policy has been installed")
else:
print("Failed to install access policy - {}".format(install_tp_policy.error_message))
install_tp_policy = client.api_call("install-policy", {"policy-package" : "Standard", "access" : 'false', "threat-prevention" : 'true', "targets" : gw_uid})
if install_tp_policy.success:
print("The threat prevention policy has been installed")
else:
print("Failed to install threat prevention policy - {}".format(install_tp_policy.error_message))
# add passwords and passphrases to dictionary
with open('additional_pass.conf') as f:
line_num = 0
for line in f:
line_num += 1
add_password_dictionary = client.api_call("run-script", {"script-name" : "Add passwords and passphrases", "script" : "printf "{}" >> $FWDIR/conf/additional_pass.conf".format(line), "targets" : gw_name})
if add_password_dictionary.success:
print("The password dictionary line {} was added successfully".format(line_num))
else:
print("Failed to add the dictionary - {}".format(add_password_dictionary.error_message))
main()