Skip to content

Instantly share code, notes, and snippets.

View jeffbrl's full-sized avatar

Jeff Loughridge jeffbrl

View GitHub Profile
@jeffbrl
jeffbrl / macro-test.slax
Created August 31, 2015 17:06
Example of using apply-macro as generic key/value store - SLAX
version 1.0;
ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
import "../import/junos.xsl";
match / {
@jeffbrl
jeffbrl / rro_display.slax
Last active September 11, 2015 17:50
Slax script to display RRO
version 1.0;
ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
import "../import/junos.xsl";
match /
{
<op-script-results> {
@jeffbrl
jeffbrl / NameCheck.py
Last active September 16, 2015 15:43
Simple class to be tested (robot framework example)
#!/usr/bin/env python
class NameChecker(object):
def __init__(self):
self._name = None
def set_name(self, name):
self._name = name
@jeffbrl
jeffbrl / NameCheckLibrary.py
Created September 16, 2015 15:42
Test Library for use with NameCheck.py (robot framework example)
from NameCheck import NameChecker
class NameCheckLibrary():
def __init__(self):
self._namechecker = NameChecker()
self._name = None
self._result = None
def Set_name(self, name):
@jeffbrl
jeffbrl / NameCheck.robot
Created September 16, 2015 15:46
Robot file for NameCheck testing (robot framework example)
*** Settings ***
Documentation Example test cases using the keyword-driven testing approach.
Library NameCheckLibrary.py
*** Test Cases ***
Test for validity
Set name Bruce
Result should be ${true}
Test for invalidity
@jeffbrl
jeffbrl / api.py
Created October 3, 2015 22:42
Simple JSON API example in Python
# test with
# curl -X POST -H "Content-Type: application/json" -d '{ "action" : "RUN"}'
# http://127.0.0.1:5000/example/
import json
from flask import request, url_for
from flask.ext.api import FlaskAPI, status, exceptions
app = FlaskAPI(__name__)
@jeffbrl
jeffbrl / namespace.py
Last active February 14, 2017 03:37
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)
@jeffbrl
jeffbrl / config.py
Created October 10, 2015 20:11
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]
@jeffbrl
jeffbrl / get_config.py
Created October 14, 2015 18:39
Pyez - Get router configuration and save to file
# code courtesy of shermdog on pyez google group
from jnpr.junos import Device
dev = Device(host='IP', user='user', passwd='password')
dev.open()
config = dev.rpc.get_config()
from lxml import etree
@jeffbrl
jeffbrl / TestApiConsumer.py
Created October 17, 2015 21:38
Faking an API call using mock object
from api_consumer.api_consumer import ApiConsumer
import unittest
import unittest.mock
import json
class TestApiConsumer(unittest.TestCase):
def setUp(self):
self.ac = ApiConsumer()