How to create DHCP server and a policy in the Fortigate

Here is an example script that demonstrates how to use the  fortigate-api  package to create a DHCP server and a policy in the Fortigate   from fortigate_api import FortigateAPI # Create a FortigateAPI object fgt = FortigateAPI(host="host", username="username", password="password") # Create a DHCP server data = {     "default-gateway": "192.168.255.1",     "netmask": "255.255.255.0",     "interface": "vlan.123",     "ip-range": [         {"start-ip": "192.168.255.2", "end-ip": "192.168.255.254",}     ], } fgt.dhcp_server.create(data) # Create a policy in the Fortigate data = dict(     name="POLICY",     status="enable",     action="accept",     srcintf=[{"name": "any"}],     dstintf=[{"name": "any"}],     srcaddr=[{"name": "all"}],     dstaddr=[{"name&quo

PYTHON PROGRAM FOR FLSM USING CLASS C IP ADDRESS ONLY

PYTHON PROGRAM FOR FLSM USING CLASS C IP ADDRESS ONLY 

ip,cidr=input("Enter the the Network ID with CIDR value for which you need FLSM (ONLY FOR CLASS C): ").split('/')

ip1,ip2,ip3,ip4=ip.split('.')

req_host=int(input("Enter the no of Required Host: "))

for h in range(0,15):

    if 2**h >= req_host:

        sm=int(cidr)+8-h

        nid=8-h

        for i in range(2**nid):

            valid_host=(2**h)*i

            print(f"{ip1}.{ip2}.{ip3}.{int(ip4)+valid_host}/{sm}")

        break

 

 

OUTPUT


RESULT


Note: I n the Above Program I have created two variables on the first line named “IP and CIDR” These two variables are taking the input of Network ID and CIDR value of any class “C” subnet as a string and then splitting it with ‘/’ than on the next line I have created 4 variables which are dividing the IPv4 32 Bit into 4 octets and storing it into sperate variables that am asking the user for the required no of host required for each FLSM Network. So once I get all this information and run a for loop till 15 (as we all know that the maximum we can till “2^16” i.e; 65,536 hosts) inside the loop there is a if……..condition which is basically checking whether this expression “2^h >= req_host” (here ‘h’ is nothing but the host which should be greater than or equal to the required host) when the condition is true that means I got the required bit what I have done here is I have created a variable called “SM” in which I have subtracted the total network bit (8) from the required (2) which I have added to the variable called CIDR that was nothing but the default subnet of class C which was in my case /24 CIDR after that I have created a variable called “nid” where I have “8-h” and then again there is a for loop which will run till 2^nid and inside the valid_host variable it will store “2^h*i” (It is the actually the valid host which ne

Comments

Popular posts from this blog

How to Configure VXLAN in FortiGate Firewall

Higher Availability in FortiGate and VRRP in Cisco

Python Script to take the Backup of Multiple device in a Network