IPv6 routed block
This commit is contained in:
59
main.py
59
main.py
@ -1,4 +1,4 @@
|
||||
import argparse, requests, json
|
||||
import argparse, requests, json, ipaddress, re
|
||||
from netmiko import ConnectHandler
|
||||
|
||||
### Define API
|
||||
@ -23,14 +23,14 @@ header = {
|
||||
def get_switch_info(server_id:int):
|
||||
server_url = f'https://{TENANTOS}/api/servers/{server_id}/connections'
|
||||
server_response = requests.get(server_url, headers=header)
|
||||
data = json.load(server_response.json())
|
||||
data = server_response.json()
|
||||
for value in data["result"]:
|
||||
if "switchId" in value["related_data"]["meta"]:
|
||||
switch:int = value["related_data"]["meta"]["switchId"]
|
||||
port_id:int = value["related_data"]["meta"]["portId"]
|
||||
switch_url = f"https://{TENANTOS}/api/networkDevices/{switch}"
|
||||
switch_response = requests.get(switch_url, headers=header)
|
||||
switch_data = json.load(switch_response.json())
|
||||
switch_data = switch_response.json()
|
||||
switch_address:str = switch_data["result"]["host"]
|
||||
switch_user:str = switch_data["result"]["managementUser"]
|
||||
switch_pass:str = switch_data["result"]["managementPassword"]
|
||||
@ -43,54 +43,29 @@ def check_tag(server_id:int):
|
||||
exist:bool = False
|
||||
tag_url = f'https://{TENANTOS}/api/servers/{server_id}'
|
||||
tag_response = requests.get(tag_url, headers=header)
|
||||
tag_data = json.load(tag_response.json())
|
||||
tag_data = tag_response.json()
|
||||
tag_lists:list = tag_data["result"]["tags"]
|
||||
for tag in tag_lists:
|
||||
if tag["name"] == ACTION_TAG:
|
||||
exist = True
|
||||
return exist
|
||||
|
||||
def get_ips(server_id:int):
|
||||
INET:list = []
|
||||
INET6:list = []
|
||||
ips_url = f'https://{TENANTOS}/api/servers/{server_id}'
|
||||
ips_response = requests.get(ips_url, headers=header)
|
||||
ips_data = json.load(ips_response.json())
|
||||
for value in ips_data["result"]["ipassignments"]:
|
||||
if value["ipAttributes"]["isIpv4"] == 1:
|
||||
INET.append(f'{value["ip"]}/32')
|
||||
elif value["ipAttributes"]["isIpv6"] == 1:
|
||||
INET6.append(f'{value["ip"]}/112')
|
||||
return {"INET": INET, "INET6": INET6}
|
||||
|
||||
def get_af(subnet_id:int):
|
||||
type:int = 4
|
||||
af_url = f'https://{TENANTOS}/api/subnets/{subnet_id}'
|
||||
af_response = requests.get(af_url, headers=header)
|
||||
af_data = json.load(af_response.json())
|
||||
af_data = af_response.json()
|
||||
if af_data["result"]["type"] == "v4":
|
||||
return type
|
||||
else:
|
||||
type = 6
|
||||
return type
|
||||
|
||||
# JunOS
|
||||
|
||||
def junos_add_config_builder(vrf:str, inet:list, inet6:list, port:str):
|
||||
if inet:
|
||||
config_inet = []
|
||||
for ip in inet:
|
||||
config_inet.append(f'set routing-instances {vrf} routing-options static route {ip} next-hop {port}')
|
||||
if inet6:
|
||||
config_inet6 = []
|
||||
for ip in inet6:
|
||||
config_inet6.append(f'set routing-instances {vrf} routing-options rib {vrf}.inet6.0 static route {ip} next-hop {port}')
|
||||
|
||||
if inet and inet6:
|
||||
return {"inet": config_inet, "inet6": config_inet6}
|
||||
else:
|
||||
return {"inet": config_inet}
|
||||
|
||||
def get_cidr(subnet_id:int):
|
||||
url = f'https://{TENANTOS}/api/subnets/{subnet_id}'
|
||||
response = requests.get(url, headers=header)
|
||||
data = response.json()
|
||||
return data["result"]["cidr"]
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@ -111,7 +86,6 @@ if __name__ == "__main__":
|
||||
ip = args.ip
|
||||
af = get_af(args.subnet)
|
||||
if args.action == "add":
|
||||
config = junos_add_config_builder(VRF, ips["INET"], ips["INET6"], switch["port_name"])
|
||||
switch_connect = ConnectHandler(
|
||||
device_type = "juniper_junos",
|
||||
host = switch["switch_address"],
|
||||
@ -119,8 +93,19 @@ if __name__ == "__main__":
|
||||
password = switch["switch_pass"],
|
||||
)
|
||||
if af == 4:
|
||||
config_command = []
|
||||
config_command = [ f'set routing-instances {VRF} routing-options static route {ip}/32 next-hop {switch["port_name"]}' ]
|
||||
elif af == 6:
|
||||
config_command = [ f'set routing-instances {VRF} routing-options rib {VRF}.inet6.0 static route {ip}/64 next-hop {switch["port_name"]}' ]
|
||||
config_command = []
|
||||
cidr = get_cidr(args.subnet)
|
||||
if cidr == 127:
|
||||
subnet = ipaddress.ip_network(f'{ip}/127')
|
||||
config_command = [ f'set interface {switch["port_name"]}.0 family inet6 address {subnet[0]}/127' ]
|
||||
else:
|
||||
show_command = [ f'show interfaces {switch["port_name"]}.0 terse | match inet6' ]
|
||||
show_output = switch_connect.send_command(show_command)
|
||||
if not show_output.strip():
|
||||
ptp = ipaddress.ip_network(f'{re.findall(r'inet6\s+([\da-f:]+)', show_output)}/127')[1]
|
||||
config_command = [ f'set routing-instances {VRF} routing-options static route {ip}/{cidr} next-hop {ptp}' ]
|
||||
switch_connect.send_config_set(config_command)
|
||||
switch_connect.disconnect()
|
||||
|
||||
Reference in New Issue
Block a user