Skip to content

Instantly share code, notes, and snippets.

@netmanchris
netmanchris / add_client_reservations_ps.j2
Created July 24, 2016 17:01
Jinja2 template to automate creation of powershell script to add DHCP client reservations
#File to use powershell to automatically build all required DHCP reservations for lab environment
{% for client in reservations %}
Add-DhcpServerv4Reservation -ScopeId {{ client.ScopeId }} -IPAddress {{ client.IPAddress }} -ClientId "{{ client.ClientId }}" -Description "{{ client.Description }}"
Set-DhcpServerv4OptionValue -ComputerName {{ client.DHCPServer }} -ReservedIP {{ client.IPAddress }} -OptionId 67 -Value {{ client.InitialConfigFile }}
{% endfor %}
@netmanchris
netmanchris / gen_scopes_posh.py
Created July 24, 2016 16:52
Python script to generate Powershell script from add_dhcp_ps.j2 template
'''Copyright 2015 Christopher Young (@netmanchris)
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the
License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required'''
from jinja2 import Environment, FileSystemLoader, Template
import yaml
@netmanchris
netmanchris / dhcp_scopes.yaml
Created July 24, 2016 16:47
YAML file which contains the desired DHCP Scope Definitions
lab_globals:
{tftp: 10.101.0.203,
DNS: 10.101.0.20,
Domain: lab.local,
CCM: 10.101.0.1,
Server: 10.101.0.20}
dhcpscopes:
- {Server: 10.101.0.20,
ScopeID: 10.11.0.0,
@netmanchris
netmanchris / gen_dhcp_scope.j2
Last active July 24, 2016 16:30
Jinja2 template to generate Powershell script which will create multiple DHCP scopes on a single Microsoft Windows 2012R2 DHCP server
#File to use powershell to automatically build all required DHCP scopes for lab environment
Add-DhcpServerv4OptionDefinition -Name tftp -OptionId 150 -Type IPv4Address
{% for scope in dhcpscopes.dhcpscopes %}
Add-DHCPServerv4Scope -EndRange {{ scope.EndRange }} -Name {{ scope.Name }} -StartRange {{ scope.StartRange }} -SubnetMask {{ scope.SubnetMask }} -State Active
Set-DHCPServerv4OptionValue -ComputerName {{ dhcpscopes.lab_globals.Server }} -ScopeId {{ scope.ScopeID }} -DnsServer {{ dhcpscopes.lab_globals.DNS }} -DnsDomain {{ dhcpscopes.lab_globals.Domain }} -Router {{ scope.Gateway }}
Set-DhcpServerv4OptionValue -ComputerName {{ scope.Server }} -ScopeId {{ scope.ScopeID }} -OptionId 66 -Value {{ dhcpscopes.lab_globals.tftp }}
Set-DhcpServerv4OptionValue -ComputerName {{ scope.Server }} -ScopeId {{ scope.ScopeID }} -OptionId 67 -Value {{ scope.InitialConfig }}
@netmanchris
netmanchris / set_interface_description.py
Created December 11, 2015 03:50
Python code using pysnmp library to set interface description on a network device
import pysnmp
def set_snmp_single(rwstring, ip_address, ifAlias, description):
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.proto import rfc1902
cmdGen = cmdgen.CommandGenerator()
cmdGen.setCmd(
cmdgen.CommunityData(rwstring),
@netmanchris
netmanchris / get_netrange.py
Last active August 29, 2015 14:28
Find netrange from two IP addresses
import ipaddress
def ip_in_network(startIp, endIp):
mask = 32
startIp = ipaddress.ip_address(startIp)
endIp = ipaddress.ip_address(endIp)
netrange = ipaddress.ip_network(str(startIp)+'/'+str(mask), strict=False)
while ((endIp in netrange) == False):
mask -= 1
netrange = ipaddress.ip_network(str(startIp)+'/'+str(mask), strict=False)
>>> y = xmltodict.parse(x)
>>> y
OrderedDict([('device', OrderedDict([('id', '116'), ('label', 'Cisco2811.haw.int'), ('ip', '10.101.0.1'), ('mask', '255.255.255.0'), ('status', '1'), ('statusDesc', 'Normal'), ('sysName', 'Cisco2811.haw.int'), ('contact', 'changed this too'), ('location', 'changed this too'), ('sysOid', '1.3.6.1.4.1.9.1.576'), ('sysDescription', None), ('devCategoryImgSrc', 'router'), ('topoIconName', 'iconroute'), ('categoryId', '0'), ('symbolId', '1147'), ('symbolName', 'Cisco2811.haw.int'), ('symbolType', '3'), ('symbolDesc', None), ('symbolLevel', '2'), ('parentId', '1'), ('typeName', 'Cisco 2811'), ('mac', '00:1b:d4:47:1e:68'), ('link', OrderedDict([('@op', 'GET'), ('@rel', 'self'), ('@href', 'http://10.3.10.220:8080/imcrs/plat/res/device/116')]))]))])
>>>
>>> print (yaml.dump(y, default_flow_style = False))
!!python/object/apply:collections.OrderedDict
dictitems:
device: !!python/object/apply:collections.OrderedDict
dictitems:
categoryId: '0'
contact: changed this too
devCategoryImgSrc: router
id: '116'
ip: 10.101.0.1
>>> print (json.dumps(y, indent = 4))
{
"device": {
"id": "116",
"label": "Cisco2811.haw.int",
"ip": "10.101.0.1",
"mask": "255.255.255.0",
"status": "1",
"statusDesc": "Normal",
"sysName": "Cisco2811.haw.int",