Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to filter policies by multiple conditions #39

Open
Manchuker1120 opened this issue Dec 31, 2024 · 5 comments
Open

how to filter policies by multiple conditions #39

Manchuker1120 opened this issue Dec 31, 2024 · 5 comments

Comments

@Manchuker1120
Copy link

Manchuker1120 commented Dec 31, 2024

cmdb.firewall.policy.get(efilter=["dstaddr==2.2.2.2",'srcaddr==1.1.1.1'])
it does not work.

@Manchuker1120 Manchuker1120 changed the title how to how to filter policies by multiple conditions Dec 31, 2024
@vladimirs-git
Copy link
Owner

filter - parameter can be used to filter by any data key/value.
efilter - parameter is designed to filter by prefixes using the syntax A.B.C.D/LEN.
In your example of using efilter, you need to fix the prefix syntax.

Links to some tutorials:
Extended filtering conditions
Filter policies

@Manchuker1120
Copy link
Author

filter - parameter can be used to filter by any data key/value. efilter - parameter is designed to filter by prefixes using the syntax A.B.C.D/LEN. In your example of using efilter, you need to fix the prefix syntax.

Links to some tutorials: Extended filtering conditions Filter policies

How do I filter if there are multiple source-ip addresses?

@Manchuker1120 Manchuker1120 reopened this Jan 1, 2025
@Manchuker1120
Copy link
Author

I want to find all policies whose source ip address contains 1.1.1.1, but I find that one of my policies whose source ip address is 1.1.1.0/24 is not matched.

@vladimirs-git
Copy link
Owner

Please try this example to filter your source ip

HOST = ""
USERNAME = ""
PASSWORD = ""

api = FortiGateAPI(
    host=HOST,
    username=USERNAME,
    password=PASSWORD,
)

# Get all firewall policies and print the total count
policies_all = api.cmdb.firewall.policy.get()
print(f"{len(policies_all)=}")  # len(policies_all)=245

# Get policies by an exact source address using Extended-filter parameter
policies_efilter = api.cmdb.firewall.policy.get(efilter=["srcaddr==1.1.1.1/32"])
print(f"{len(policies_efilter)=}")  # len(policies_efilter)=1

# Get policies by an exact source address using filter parameter
policies_filter = []
addresses = api.cmdb.firewall.address.get(filter="subnet==1.1.1.1 255.255.255.255")
for item in api.cmdb.firewall.policy.get():
    dstaddr = [d["name"] for d in item["srcaddr"]]
    for address in addresses:
        if address["name"] in dstaddr:
            policies_filter.append(item)
print(f"{len(policies_filter)=}")  # len(policies_filter)=1

@Manchuker1120
Copy link
Author

Please try this example to filter your source ip

HOST = ""
USERNAME = ""
PASSWORD = ""

api = FortiGateAPI(
    host=HOST,
    username=USERNAME,
    password=PASSWORD,
)

# Get all firewall policies and print the total count
policies_all = api.cmdb.firewall.policy.get()
print(f"{len(policies_all)=}")  # len(policies_all)=245

# Get policies by an exact source address using Extended-filter parameter
policies_efilter = api.cmdb.firewall.policy.get(efilter=["srcaddr==1.1.1.1/32"])
print(f"{len(policies_efilter)=}")  # len(policies_efilter)=1

# Get policies by an exact source address using filter parameter
policies_filter = []
addresses = api.cmdb.firewall.address.get(filter="subnet==1.1.1.1 255.255.255.255")
for item in api.cmdb.firewall.policy.get():
    dstaddr = [d["name"] for d in item["srcaddr"]]
    for address in addresses:
        if address["name"] in dstaddr:
            policies_filter.append(item)
print(f"{len(policies_filter)=}")  # len(policies_filter)=1

its kind of you
i will try
ty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants