Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pyez/device_list-ALL.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
# Add devices to this file in the form of:
# Hostname: 'ip address'

MX480#1: 'xxx.xxx.xxx.xxx'
MX480#2: 'xxx.xxx.xxx.xxx'
MX480#3: 'xxx.xxx.xxx.xxx'
50 changes: 50 additions & 0 deletions pyez/get_config_push_gitlab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from jnpr.junos import Device
from jnpr.junos.exception import ConnectError
import getpass
from lxml import etree
from pprint import pprint
from subprocess import PIPE
from subprocess import Popen
import cmd, sys
import os
import yaml

input_file = 'device_list-ALL.yml' #Yaml file containing all devices

#user = input("Junos OS username: ")
#passwd = getpass.getpass("Junos OS password: ")

for key, value in yaml.safe_load(open(input_file)).items():

# with Device(host=value, user='<username>', passwd='<password>', port='22') as dev: # Option 1::Creds in clear text

# with Device(host=value, user=user, passwd=passwd, port='22') as dev: # Option 2::Prompts for User/Passwd

with Device(host=value, ssh_priv_key_file='~/.ssh/gitlab_ed25519') as dev: # Option 3::Using SSH Keys | Preferred

try:
dev.open()
except ConnectError as err:
print ('Cannot connect to device: {0}'.format(err))
sys.exit(1)
except Exception as err:
print (err)
sys.exit(1)
data = dev.rpc.get_config(options={'database' : 'committed', 'format':'text'})

config_file = open(key + '.txt', 'w')
config_file.write(etree.tostring(data, encoding='unicode', method='xml'))
print ('Successfully Collected Configuration from {}' .format(key))
config_file.close()

dev.close()

cmds = ['mv *.txt /Users/me/git/network-configs/','cd /Users/me/git/network-configs/', 'git add --all','git commit -m "updating config files"', 'git push']
encoding = 'unicode'
p = Popen('/bin/bash', stdin=PIPE, stdout=PIPE, stderr=PIPE)

for cmd in cmds:
#In python 3+, str is a default for subprocess, therefore, we need to convert our command to bytes.
p.stdin.write(bytes(cmd + "\n", 'utf-8'))
p.stdin.close()
print ('Pushing uncommitted config files to GitLab Completed')
17 changes: 17 additions & 0 deletions pyez/get_config_push_gitlab_README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Use Case No. 1

### Overview
This python script was built by putting together small portions of code available at the "[Junos PyEZ Developer Guide](https://www.juniper.net/documentation/en_US/junos-pyez/information-products/pathway-pages/junos-pyez-developer-guide.html)" aiming to automaticatically feed a Git repository with Network configuration data (may the Network Engineers fail/forget to do so), keeping the teams distributed across different timezones informed and making it easy to co-relate events and/or incidents to changes (to begin with), thus making troubleshooting smoother.

![Example No.1 Diagram](/images/ex-no1.png)

### Workflow
* The script is set to run from a cron job call at a predetermined schedule.
* A NETCONF session over SSH is established to all nodes in the `.yml` file sequentially.
* A [`get_config()`](https://www.juniper.net/documentation/us/en/software/junos/netconf/topics/ref/tag/netconf-get-config.html) Remote Procedure Call (RPC) is executed to request the complete configuration.
* On receiving the config it opens/creates a file (for each node) to write the data into, and name it after the entry on the `.yml` file for which it's performing the RPC to, and makes it a `.txt` on closing the file.
* Upon completion the script runs [`git add --all`](http://git-scm.com/docs/git-add) to add them all to the repo, then [`git commit`](http://git-scm.com/docs/git-commit) and [`git push`](http://git-scm.com/docs/git-push).

Happy Labbing!

/me