1+ def main ():
2+ from Fortimanager_Master import FortiManager
3+ import fwinput_cfg as data
4+ host = "https://172.19.255.53/jsonrpc"
5+ usr = "fortiapi"
6+ pwd = "fortiapi"
7+ i = input ("Enter the no.of vlan's you want to Migration:" )
8+ for x in range (int (i )):
9+ try :
10+ device_name = (data .vlan_list [x ]["device_name" ])
11+ scope = (data .vlan_list [x ]["scope" ])
12+ adom = (data .vlan_list [x ]["adom" ])
13+ vdom_name = (data .vlan_list [x ]["vdom_name" ])
14+ l3out_int_name = (data .vlan_list [x ]["l3out_int_name" ])
15+ vlan_id = (data .vlan_list [x ]["vlan_id" ])
16+ gw_network = (data .vlan_list [x ]["gw_network" ])
17+ gw_mask = (data .vlan_list [x ]["gw_mask" ])
18+ l3out_phy_interface = (data .vlan_list [x ]["l3out_phy_interface" ])
19+ l3out_ipadd = (data .vlan_list [x ]["l3out_ipadd" ])
20+ aci_l3out_nexthop = (data .vlan_list [x ]["aci_l3out_nexthop" ])
21+ l3out_rm_name = (data .vlan_list [x ]["l3out_rm_name" ])
22+ current_vlan_interface = (data .vlan_list [x ]["current_vlan_interface" ])
23+ l3out_nh_interface = l3out_int_name
24+ l3out_vlan_name = l3out_int_name + "_L3out"
25+ distance = "10"
26+ prefix = "prefix"
27+ zone_name = l3out_int_name + "_Zone"
28+ access_list = "rof_adv_list"
29+ gw_subnet = gw_network + "/" + gw_mask
30+ config_block = "system/zone" #"system/zone" or "router/ospf" or "router/static" or "router/access-list" or "router/route-map"
31+ interface_block = "global/system/interface"
32+ print ("" )
33+ print ("Logging into Fortimanager Controller:{}" .format (host ))
34+ print ("Configure the FW :{}" .format (device_name ))
35+ print ("Configure the new L3out interface :{}" .format (l3out_int_name ))
36+ print ("Associate the new L3out int. into member of :{}" .format (zone_name ))
37+ print ("Configure the .1Q Sub-interface vlan for L3out #:{}" .format (vlan_id ))
38+ print ("Configure the Physical interface for new L3out:{}" .format (l3out_phy_interface ))
39+ print ("Configure the Next-hop IP address ACI :{}" .format (aci_l3out_nexthop ))
40+ print ("Configure the GW_subnet Next-hop to ACI :{}" .format (gw_subnet ))
41+ print ("Configure the IP address on L3out interface:{}" .format (l3out_ipadd ))
42+ print ("Configure the Route-map {} on FW:{}" .format (l3out_rm_name ,device_name ))
43+ print ("Configure the Access-list {} on the FW:{}" .format (access_list ,device_name ))
44+ print ("Configure the Current interface on the FW:{}" .format (current_vlan_interface ))
45+
46+
47+ ACTION = input ("Are you sure you want to push the configuration (y/n): " )
48+ if ACTION in ("y" ,"yes" ,"Y" ,"YES" ):
49+ FM = FortiManager (usr , pwd , host )
50+ print ("Calling the Master function -> Authenticating into the FM Controller" )
51+ t0 = FM .login ()
52+ t1 = FM .add_vlan_interface (device_name , l3out_vlan_name , vdom_name , l3out_ipadd ,vlan_id ,l3out_phy_interface )
53+ t2 = FM .add_zone_interface (device_name , zone_name , l3out_vlan_name ,current_vlan_interface )
54+ t3 = FM .add_static_route (device_name , l3out_vlan_name , gw_subnet , distance , aci_l3out_nexthop )
55+ t4 = FM .create_access_list (device_name , access_list )
56+ t100 = FM .get_acl_block (device_name ,scope , "router/access-list" )
57+ get_acl = t100
58+ verify_acl = get_acl ["result" ][0 ]["data" ][0 ]
59+ #print(verify_acl)
60+ acl_rule = verify_acl ["rule" ]
61+ acl_name = verify_acl ["name" ]
62+ if None == acl_rule and acl_name == access_list :
63+ print ("ACL Entries not exist." )
64+ t5 = FM .add_access_list (device_name , gw_subnet , access_list )
65+ else :
66+ print ("ACL entire already exist" )
67+ t101 = FM .get_acl_block (device_name ,scope , "router/access-list" )
68+ get_acl1 = t101
69+ curr_data = get_acl1 ["result" ][0 ]["data" ][0 ]["rule" ]
70+ prefix_len = int (len (curr_data ))
71+ i = 0
72+ prefix_list = []
73+ for i in range (prefix_len ):
74+ acl_prefix = curr_data [i ]["prefix" ]
75+ acl_prefixes = (acl_prefix [0 ])
76+ prefix_list .append (acl_prefixes )
77+ print (prefix_list )
78+ if gw_network in prefix_list :
79+ print ("The new prefix already exist !!: {}" .format (gw_network ))
80+ else :
81+ print ("updating the new prefix: {}" .format (gw_network ))
82+ t7 = FM .append_access_list (device_name , gw_subnet , t100 ,access_list )
83+ t8 = FM .add_acl_to_rm (device_name , l3out_rm_name , access_list )
84+ t9 = FM .add_rm_to_ospf (device_name , l3out_rm_name )
85+ #t7=FM.get_config_block(device_name,scope, config_block)
86+ #t8=FM.get_interface_block(device_name, interface_block)
87+ t9 = FM .quick_install_device (adom , device_name ,l3out_vlan_name ) ### Push the configuration from Fortimanager to Fortigate Firewall.
88+ t10 = FM .logout ()
89+ elif ACTION in ("n" ,"no" ,"N" ,"No" ):
90+ print ("Ending the script" )
91+ else :
92+ print ("Please enter yes or no." )
93+ except IndexError :
94+ print ("Oops! Out of the vlan migration range. please check and Try again..." )
95+
96+ if __name__ == '__main__' :
97+ main ()
0 commit comments