Skip to content

Instantly share code, notes, and snippets.

@devinci-it
Created January 22, 2023 02:24
Show Gist options
  • Select an option

  • Save devinci-it/6cf6f001d1c6bcd0e0c3a438a3d2ba28 to your computer and use it in GitHub Desktop.

Select an option

Save devinci-it/6cf6f001d1c6bcd0e0c3a438a3d2ba28 to your computer and use it in GitHub Desktop.
IOS DHCP config generator.
from ipaddress import IPv4Network,IPv4Address
def config_dhcp(dhcp_pool_name, network_address, dns_address=None,dg=None,dns=None,exclude_ips=None):
"""
"BY DEFAULT WILL EXCLUDE THE FIRST 100 USABLE HOST IP"
AND SUBNET'S FIRST USABLE ADDRESS AS DG
AND THE 10th as DNS
if not defined : DEFAULT GATEWAY : FIRST USABLE HOST ADDRESS
:
:param excluded_addresses:(min, max)
:param dhcp_pool_name:str
:param network_address:str
:param dns_address:
:param dg:
:return: str
"""
net_address = IPv4Network(network_address)
if dg is None:
dhcp_default_gateway = net_address[1]
else:
dhcp_default_gateway = dg
if exclude_ips is None:
dhcp_excluded_min = net_address[1]
dhcp_excluded_max = net_address[10]
else:
dhcp_excluded_min = (IPv4Address(exclude_ips[0]))
dhcp_excluded_max = (IPv4Address(exclude_ips[1]))
if dns_address is None:
dns_address = net_address[10]
dhcp_poolname = dhcp_pool_name
dhcp_network = net_address
config = f"""
ip dhcp excluded-address {dhcp_excluded_min} {dhcp_excluded_max}
ip dhcp pool {dhcp_poolname}
network {dhcp_network.network_address} {dhcp_network.netmask}
default-router {dhcp_default_gateway}
dns address {dns_address}
exit
service dhcp
"""
return config
#sample=config_dhcp("V10","1.1.3.0/24",dns_address="1.1.1.2",exclude_ips=("1.1.2.10","1.1.2.11"))
#print(sample)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment