Using Django's Template system for network configs

I wrote hatch a few years ago to make cookie cutter network config templates. One thing I've wanted to implement for a while is a DSL for use in hatch. I've experimented with jscc and more recently with peg.js. While writing grammar for peg.js I said to myself "it would be great if I could used Django's template system and filters without re-inventing the wheel."

So I started experimenting and found out its pretty easy to do. A little more tinkering and I could possibly replace hatch's mechanics with Django's builtin template, tag, and filter code.

from django.conf import settings
from django import template
settings.configure(DEBUG=True, TEMPLATE_DEBUG=True)

s = u'conf t\n ip address {{ip_address}} {{subnet_mask}} \n end \n write'
t = template.Template(s)
c = template.Context({'ip_address': '','subnet_mask':''})
rendered = t.render(c)
print rendered

print "----------------------------------"

s = u'conf t\n ip address 10.{{site_code}}.1.1 {{subnet_mask}} \n end \n write'
t = template.Template(s)
c = template.Context({'site_code': '66','subnet_mask':''})
rendered = t.render(c)
print rendered

print "----------------------------------"

s = u'conf t\n {% if site_code == 66 %}snmp-server location Sydney{% endif%}'
t = template.Template(s)
c = template.Context({'site_code': 66})
rendered = t.render(c)
print rendered

akonkol@dev:~/Code/messy$ ./
conf t
 ip address
conf t
 ip address
conf t
 snmp-server location Sydney

Tagged as cisco , django template , templates
Written by Andrew Konkol on January 13th, 2014 | 0 Comments

Generate IP Addresses and valid CIDR notations

I needed to test some networking tools I've been developing, and to do that I needed IP addresses. To get IP addresses I wrote this snippet.


import random
def generate_ip():
   ip_class = random.choice(CLASSES)

   if ip_class == "A":
        first_octet = random.randint(1,126)

   if ip_class == "B":
        first_octet = random.randint(128,191)

   if ip_class == "C":
        first_octet = random.randint(192,223)

   second_octet = random.randint(0,254)
   third_octet = random.randint(0,254)
   fourth_octet = random.randint(0,254)
   return "%i.%i.%i.%i" %(first_octet,second_octet,third_octet,fourth_octet)

I also needed to generate random valid slash notations for a given ip:

def generate_cidr(network_id):
   first_octet = int(str(network_id).split(".")[0])

   if first_octet <= 126 and first_octet >= 1:
      cidr_bits =  random.randint(8,30)

   if first_octet >=127 and first_octet <= 191:
      cidr_bits =  random.randint(16,30)

   if first_octet >=192 and first_octet <= 223:
      cidr_bits =  random.randint(24,30)

   return cidr_bits
Tagged as cidr , IP python
Written by Andrew Konkol on October 22nd, 2013 | 0 Comments

Using common linux utilities over IPV6

I've needed to test a variety of network architectures and access-lists over the past couple of weeks.  Most linux utilities require some sort of ipv6 flag on the command-line which has been constantly challenging my muscle memory.


IPv6 enabled sites used for testing: (if you see the swimming turtle you are accessing the site with ipv6)

2001:4860:4860::8888 (google's ipv6 equivalent of (2600:3c01::f03c:91ff:fe93:cd19 host to portscan) (2a03:2880:2110:df07:face:b00c:0:1) (2600:3c02::f03c:91ff:fedf:21c3) 


DNS lookups:

dig -6 @2001:4860:4860::8888 AAAA


dig -6 @2001:4860:4860::8888 AAAA +short


snmpwalk -v2c -c public udp6:[2001:0db8:85a3:0:0:8a2e:0370:99]:161 .


ping6 2001:0db8:85a3:0:0:8a2e:0370:99

traceroute6 2001:0db8:85a3:0:0:8a2e:0370:99

web browsing:

lynx http://\[2a03:2880:2110:df07:face:b00c:0:1\]


nmap -sV -6 2600:3c01::f03c:91ff:fe93:cd19


telnet 2a03:2880:2110:df07:face:b00c:0:1 80


ssh 2a03:2880:2110:df07:face:b00c:0:1
Tagged as cli , ipv6 linux
Written by Andrew Konkol on September 11th, 2013 | 0 Comments

Cold Brew Fridge Coffee Bucket

I've been expierementing with different brewing vessels and I decided to make my own.  I used spare parts I had from beer brewing.


Bill of Materials:

Item Source Price
2 Gallon Food Grade Bucket  Brew Camp  $5.00
Bottling spigot  Brew Camp  $3.00
Fine mesh steeping bag  Brew Camp  $4.60
Step bit  NewEgg  N/A

Step one: Drill out 1" hole as close to the bottom of the bucket as possible keeping enough room for the spigot nut to have clearance.

Step two: Remove one rubber gasket from spigot nipple, slide spigot assembly into 1" hole, slide rubber gasket over the nipple and screw on the nylon nut over the nipple until hand tight

Step three: Drill small hole in lid to allow for ventilation while using spigot

Step four: Put grounds in nylon bag and tie tight, place bag in bucket, add water, let sit overnight in fridge.

Ste five: Turn spigot downward and turn handle to pour.

Tagged as brew , coffee cold , cold brew fridge
Written by Andrew Konkol on August 25th, 2013 | 0 Comments

Using ZeroClipboard on your website to Copy text to your clipboard

I've used ZeroClipboard in a couple of my projects which allows me to provide a clickable object which copies text of my choosing into the client's clipboard.  I usually use copy and paste keyboard shortcuts, but there are times when it's nice and more effecient to have a click-to-copy button.

Get the files


Import the files and create the basic structure

<div id="copy_initiator">Click me to Copy Text!</div>
<script src="ZeroClipboard.min.js" type="text/javascript"></script>
<script type="text/javascript">

var clip = new ZeroClipboard( document.getElementById("copy_initiator"), {
  moviePath: "ZeroClipboard.swf"
} );

//this event happens upon initiating the copy 
clip.on( 'dataRequested', function ( client, args ) {
    clip.setText( "whatever text you want");
} );

//this event happens upon the copy finishing
clip.on( 'complete', function(client, args) {
  alert("Copied text to clipboard: " + args.text );
} );


And thats it! You should have a div with the id of "copy_initiator" which when clicked will copy "whatever text you want" into the client's clipboard. Using the setText() method you can use any source of text including anything object attributes, html, text via javascript or jquery.

Tagged as copy , js ZeroClipboard
Written by Andrew Konkol on August 1st, 2013 | 0 Comments