Skip to content

Instantly share code, notes, and snippets.

@dirkakrid
dirkakrid / _core.py
Created March 12, 2017 23:02 — forked from justanr/_core.py
Clean Architecture In Python
from abc import ABC, ABCMeta, abstractmethod
from collections import namedtuple
from itertools import count
PayloadFactory = namedtuple('PayloadFactory', [
'good', 'created', 'queued', 'unchanged', 'requires_auth',
'permission_denied', 'not_found', 'invalid', 'error'
])
"""
import simplejson as json
import lxml
class objectJSONEncoder(json.JSONEncoder):
"""A specialized JSON encoder that can handle simple lxml objectify types
>>> from lxml import objectify
>>> obj = objectify.fromstring("<Book><price>1.50</price><author>W. Shakespeare</author></Book>")
>>> objectJSONEncoder().encode(obj)
'{"price": 1.5, "author": "W. Shakespeare"}'
"""
@dirkakrid
dirkakrid / jsonp-in-flask.py
Created February 15, 2017 09:14 — forked from farazdagi/jsonp-in-flask.py
JSONP in Flask
import json
from functools import wraps
from flask import redirect, request, current_app
def support_jsonp(f):
"""Wraps JSONified output for JSONP"""
@wraps(f)
def decorated_function(*args, **kwargs):
callback = request.args.get('callback', False)
if callback:
@dirkakrid
dirkakrid / config.py
Created February 14, 2017 03:37 — forked from jeffbrl/config.py
Using xpath to parse junos router configuration
from jnpr.junos import Device
from lxml import etree
dev = Device(host='2001:DB8::1', user='jeffl', passwd='pass123')
dev.open()
config = dev.rpc.get_config()
interface_ip = config.xpath("//interface[name='ge-0/0/1']/unit/family/inet/address/name")[0]
@dirkakrid
dirkakrid / namespace.py
Created February 14, 2017 03:37 — forked from jeffbrl/namespace.py
Using pyez's remove_namespaces function to strip namespace information
from lxml import etree
from jnpr.junos.jxml import remove_namespaces
# file contains output of 'show interfaces ge-0/0/0.0'
fh = open('router_output2.xml', 'r')
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(fh, parser)
tree = remove_namespaces(tree)
@dirkakrid
dirkakrid / voidstrap.sh
Created December 21, 2016 09:10 — forked from radare/voidstrap.sh
voidstrap.sh
#!/bin/sh
# voidstrap - bootstrap xbps from any *nix
# author: pancake - 2013
if [ -z "$1" ]; then
echo "Usage: voidstrap.sh /path/to/new/root"
exit 1
fi
VOIDROOT="$1"
VOIDPKG=base-chroot
@dirkakrid
dirkakrid / python27_on_centos65.md
Created December 13, 2016 01:24 — forked from dalegaspi/python27_on_centos65.md
Installing Python 2.7 on CentOS 6.5

Installing Python 2.7 on Centos 6.5

Centos 6.* comes with Python 2.6, but we can't just replace it with v2.7 because it's used by the OS internally (apparently) so you will need to install v2.7 (or 3.x, for that matter) along with it. Fortunately, CentOS made this quite painless with their Software Collections Repository

sudo yum update # update yum
sudo yum install centos-release-scl # install SCL 
sudo yum install python27 # install Python 2.7

To use it, you essentially spawn another shell (or script) while enabling the newer version of Python:

@dirkakrid
dirkakrid / ncclient_demo.py
Created December 7, 2016 05:38 — forked from jeffbrl/ncclient_demo.py
Executing Arbitrary Junos 'Show' Commands with PyEZ and ncclient
#!/usr/bin/env python
# Demonstrates the use of the 'command' tag to execute arbritrary 'show' commands.
# This code was inspired by Ebben Aries's command-jnpr.py at
# https://github.com/leopoul/ncclient/blob/master/examples/juniper/command-jnpr.py
#
# usage: python ncclient_demo.py <show command> <xpath expression>
# python ncclient_demo.py 'show route 2600::/64' '//rt-entry/nh'
#
# Jeff Loughridge
# August 2014
@dirkakrid
dirkakrid / bash
Created December 6, 2016 08:00 — forked from mikemackintosh/bash
Python Pygments and colored cat output
sudo easy_install Pygments
alias cat='pygmentize -O console256 -g'
@dirkakrid
dirkakrid / firewall-start
Created December 6, 2016 07:54 — forked from mikemackintosh/firewall-start
Asus Router Firewall
#!/bin/sh
##################################################################################################
## 01/01/2015 --- RT-AC56U / RT-ACRT66U / RT-AC68U Firewall Addition v0.7.6 #
##################################################################################################
### ----- Make Sure To Edit The Following Files ----- #
### /jffs/scripts/firewall <-- Blacklists IP's From /tmp/home/root/ipset.txt #
### /jffs/scripts/ipset.txt <-- Banned IP List/IPSet Rules #
##################################################################################################
##############################