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.

#!/usr/bin/python
from django.conf import settings
from django import template
settings.configure(DEBUG=True, TEMPLATE_DEBUG=True)
#https://github.com/django/django/blob/master/django/template/__init__.py

s = u'conf t\n ip address {{ip_address}} {{subnet_mask}} \n end \n write'
t = template.Template(s)
c = template.Context({'ip_address': '192.168.1.1','subnet_mask':'255.255.255.0'})
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':'255.255.255.0'})
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$ ./temp_hack.py
conf t
 ip address 192.168.1.1 255.255.255.0
 end
 write
----------------------------------
conf t
 ip address 10.66.1.1 255.255.255.0
 end
 write
----------------------------------
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.

#!/usr/bin/python

import random
def generate_ip():
   CLASSES='ABC'
   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:

www.kame.net (if you see the swimming turtle you are accessing the site with ipv6)

2001:4860:4860::8888 (google's ipv6 equivalent of 8.8.8.8)

scanme.nmap.org (2600:3c01::f03c:91ff:fe93:cd19 host to portscan)

facebook.com (2a03:2880:2110:df07:face:b00c:0:1)

six.use.io (2600:3c02::f03c:91ff:fedf:21c3) 

 

DNS lookups:

dig -6 @2001:4860:4860::8888 facebook.com AAAA

or 

dig -6 @2001:4860:4860::8888 facebook.com AAAA +short

snmpwalk:

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

ping/traceroute:

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:

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

telnet:

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

ssh:

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

wget https://github.com/zeroclipboard/ZeroClipboard/raw/master/ZeroClipboard.swf
wget https://raw.github.com/zeroclipboard/ZeroClipboard/master/ZeroClipboard.min.js

Import the files and create the basic structure

<html>
<head>
</head>
<body>
<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 );
} );

</script>
</body>
</html>

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