Overview
This document contains a blog post about getting started with Ruby for Junos Automation.
I need help getting started with Ruby
I’ve been asked by a few people to do a quick blog on getting started with Ruby. The focus on this blog will be setting up your computer with the Ruby language and libraries that you can use to write programs to automate Junos based networking devices.
Step-1: Install Ruby
Chances are you already have Ruby on your computer. If you have a Mac, you will, but the question is what version is installed (and what version you could use…). If you want to check if you have Ruby already installed, just do the following from a terminal window:
2 |
ruby 1.9.3p327 (2012-11-10 revision 37606) [i386-cygwin] |
If you do not have Ruby installed, you can find installation instructions here from the main Ruby language website here.
If you are using a Windows computer, you have two options: (1) Install Cygwin and all of the Ruby bits, or (2) use the native Ruby distribution for Windows.
Step-2: Install Ruby “Gem” Tool
The Ruby “gem” tool is the utility to manage Ruby libraries, aka “gems”. Ruby gems are hosted on RubyGems.org. The instructions for downloading the Ruby gems tool and installing it on your system can be found here. Once it’s installed, you can check the version:
Step-3: Install Gems
There are a few gems that I would recommend you install. You install these using the “gem” tool, and on Unix/like systems you will either need to do this as “root” or using “sudo”. Here is a list of gems I’d recommend installing first:
- rdoc – Get the latest copy of the Ruby Documentation tools
- pry – This allows you to set breaks in your code for debugging
- highline – This allows you to prompt users for “secret” input, like passwords
To install the “highline” gem, for example, you would do the following command:
1 |
jeremy@laptop$ gem install highline |
You can get a listing of all the gems you installed, for example:
01 |
jeremy@laptop$ gem list |
Step-4: Write Your First Program
Here is a short program to ask you for a secret and then puts it to the screen:
secret.rb
1 |
require 'highline/import' |
2 |
secret = ask( "What's your secret? " ){|a| a.echo = false } |
3 |
puts "I'm telling your secret: #{secret}" |
You execute this program on the shell and enter “magic” at the prompt:
1 |
jeremy @laptop $ ruby secret.rb |
3 |
I 'm telling your secret: magic |
Step-5: Install the Nokogiri Gem
Nokogiri is one of the most popular XML libraries for Ruby. Junos management is XML “under the hood”, so we need a library to handle XML. If you’re not familiar with XML, don’t worry, I’ll cover the basics in an upcoming blog.
The Ruby gem to automate Junos is called “netconf” (next step), and it uses the nokogiri gem. So if you just skip to the next step and install netconf, and if you don’t have nokogiri installed then the gem tool will install nokogiri first. The issue that I’ve commonly seen is that nokogiri requires “native extensions”, which basically means you need to have some system libraries on your computer. If you don’t have those libraries already installed, then the gem install will fail and you will need to follow the Nokogiri installation instructions on their website here.
1 |
jeremy@laptop$ gem install nokogiri |
2 |
Fetching: nokogiri-1.5.6.gem (100%) |
3 |
Building native extensions. This could take a while ... |
4 |
Successfully installed nokogiri-1.5.6 |
6 |
Installing ri documentation for nokogiri-1.5.6... |
7 |
Installing RDoc documentation for nokogiri-1.5.6... |
Step-6: Install the NETCONF Gem
Once you have Nokogiri installed, install the NETCONF gem. This should go smoothly.
1 |
jeremy@laptop$ gem install netconf |
2 |
Fetching: netconf-0.2.5.gem (100%) |
3 |
Successfully installed netconf-0.2.5 |
5 |
Installing ri documentation for netconf-0.2.5... |
6 |
Installing RDoc documentation for netconf-0.2.5... |
Now you’re ready to write your first Ruby program to automate Junos.
Step-7: Enable NETCONF on Junos
You will need to enable the NETCONF service on the Junos device, it is not enabled by default. At the Junos CLI do the following:
1 |
jeremy@junos> configure |
NETCONF is over TCP/830, so if you have a firewall along the path you will need to open access for that port.
Step-8: Write Your first Ruby Program for Junos
Here is a short program that will display the system up-time. I’ll cover the basics of Junos Automation programming in another blog. Here’s the sample output for this program
1 |
jeremy@laptop$ ruby get-sn.rb jeremy@mx960-190.inmylab.net |
get-sn.rb
01 |
require 'highline/import' |
03 |
target = ARGV [ 0 ].split '@' |
05 |
login[ :username ] = ( target.count > 1 ) ? target.shift : ENV [ 'USER' ] |
06 |
login[ :target ] = target.shift |
07 |
login[ :password ] = ask( "password: " ){ |a| a.echo = false } |
08 |
Netconf:: SSH . new ( login ) { |dev| |
10 |
inv = dev.rpc.get_chassis_inventory |
11 |
description = inv.xpath( 'chassis/description' ).text |
12 |
serial_number = inv.xpath( 'chassis/serial-number' ).text |
13 |
puts "#{description}: #{serial_number}" |
Summary
I hope you’ve found this brief tutorial helpful and you now have an environment to start writing automation programs!