FortiSOAR Discussions
arthurhardt
New Contributor

[FortiSOAR] [Connectors] - Code Snippet Tips

Hello FortiSOAR community,

 

I’ve been using Code Snippet in a Playbook, and I really missed having documentation or code samples, so I decided to share some tips and use cases with you. I know it's very basic tip, but didn't find this in my searchs. If you have more materials or use cases to contribute, feel free to share, I didn't find anything.

 

  • Using a variable defined in the playbook

variable = {{ vars.<name of variable set in set variable> }}
  • To show it in the Playbook History Output, or for use in next steps, you need to print the result

variable = {{ vars.test }}
print(variable)

 

Hands-On

 

  • Setting Variables

 
 

image.png

 

 

 

  • Outputting Code Snippet Execution

image.png

 

 

  • Output


image.png

 

Simple code to check if an IP is contained in a CIDR list

 

P.S.: You need to set blockIps and sourceIps (type: list) using the ‘Set Variable’ function before the Code Snippet.

 

from ipaddress import ip_network, ip_address

block_cidrs = {{vars.blockIps}}
source_ips  = {{vars.sourceIps}}

block_networks = [ip_network(cidr) for cidr in block_cidrs]

filtered_ips = []
for ip_str in source_ips:
    ip_obj = ip_address(ip_str)
    blocked = False
    for net in block_networks:
        if ip_obj in net:
            blocked = True
            break
    if not blocked:
        filtered_ips.append(ip_str)

print(filtered_ips)

 

Regards,

0 REPLIES 0