Giter VIP home page Giter VIP logo

go-junos's Introduction

go-junos

GoDoc Travis-CI Go Report Card

A Go package that interacts with Junos devices, as well as Junos Space, and allows you to do the following:

  • Run operational mode commands, such as show, request, etc..
  • Compare the active configuration to a rollback configuration (diff).
  • Rollback the configuration to a given state or a "rescue" config.
  • Configure devices by submitting commands, uploading a local file or from a remote FTP/HTTP server.
  • Commit operations: lock, unlock, commit, commit at, commit confirmed, commit full.
  • Device views - This will allow you to quickly get all the information on the device for the specified view.
  • [SRX] Convert from a zone-based address book to a global one.

Junos Space <= 15.2

  • Get information from Junos Space managed devices.
  • Add/remove devices from Junos Space.
  • List all software image packages that are in Junos Space.
  • Stage and deploy software images to devices from Junos Space.
  • Create, edit and delete address and service objects/groups.
  • Edit address and service groups by adding or removing objects to them.
  • View all policies managed by Junos Space.
  • Publish policies and update devices.
  • Add/modify polymorphic (variable) objects.

Installation

go get -u github.com/scottdware/go-junos

Note: This package makes all of it's calls over Netconf using the go-netconf package from Juniper Networks. Please make sure you allow Netconf communication to your devices:

set system services netconf ssh
set security zones security-zone <xxx> interfaces <xxx> host-inbound-traffic system-services netconf

Authentication Methods

There are two different ways you can authenticate against to device. Standard username/password combination, or use SSH keys. There is an AuthMethod struct which defines these methods that you will need to use in your code. Here is an example of connecting to a device using only a username and password.

auth := &junos.AuthMethod{
    Credentials: []string{"scott", "deathstar"},
}

jnpr, err := junos.NewSession("srx.company.com", auth)
if err != nil {
    fmt.Println(err)
}

If you are using SSH keys, here is an example of how to connect:

auth := &junos.AuthMethod{
    Username:   "scott",
    PrivateKey: "/home/scott/.ssh/id_rsa",
    Passphrase: "mysecret",
}

jnpr, err := junos.NewSession("srx.company.com", auth)
if err != nil {
    fmt.Println(err)
}

If you do not have a passphrase tied to your private key, then you can omit the Passphrase field entirely. In the above example, we are connecting from a *nix/Mac device, as shown by the private key path. No matter the OS, as long as you provide the location of the private key file, you should be fine.

If you are running Windows, and using PuTTY for all your SSH needs, then you will need to generate a public/private key pair by using Puttygen. Once you have generated it, you will need to export your private key using the OpenSSH format, and save it somewhere as shown below:

alt-text

Examples

Visit the GoDoc page for package documentation and examples.

Connect to a device, and view the current config to rollback 1.

auth := &junos.AuthMethod{
    Credentials: []string{"admin", "Juniper123!"},
}

jnpr, err := junos.NewSession("qfx-switch.company.com", auth)
if err != nil {
    fmt.Println(err)
}

defer jnpr.Close()

diff, err := jnpr.Diff(1)
if err != nil {
    fmt.Println(err)
}

fmt.Println(diff)

// Will output the following

[edit vlans]
-   zzz-Test {
-       vlan-id 999;
-   }
-   zzz-Test2 {
-       vlan-id 1000;
-   }

View the routing-instance configuration.

auth := &junos.AuthMethod{
    Username:   "admin",
    PrivateKey: "/home/scott/.ssh/id_rsa",
}

jnpr, err := junos.NewSession("srx.company.com", auth)
if err != nil {
    fmt.Println(err)
}

defer jnpr.Close()

riConfig, err := jnpr.GetConfig("text", "routing-instances")
if err != nil {
    fmt.Println(err)
}

fmt.Println(riConfig)

// Will output the following

## Last changed: 2017-03-24 12:26:58 EDT
routing-instances {
    default-ri {
        instance-type virtual-router;
        interface lo0.0;
        interface reth1.0;
        routing-options {
            static {
                route 0.0.0.0/0 next-hop 10.1.1.1;
            }
        }
    }
}

Views

Device views allow you to quickly gather information regarding a specific "view," so that you may use that information however you wish. A good example, is using the "interface" view to gather all of the interface information on the device, then iterate over that view to see statistics, interface settings, etc.

Note: Some of the views aren't available for all platforms, such as the ethernetswitch and virtualchassis on an SRX or MX.

Current out-of-the-box, built-in views are:

Views CLI equivilent
arp show arp
route show route
bgp show bgp summary
interface show interfaces
vlan show vlans
ethernetswitch show ethernet-switching table
inventory show chassis hardware
virtualchassis show virtual-chassis status
staticnat show security nat static rule all
sourcenat show security nat source rule all
storage show system storage
firewallpolicy show security policies (SRX only)
lldp show lldp neighbors

NOTE: Clustered SRX's will only show the NAT rules from one of the nodes, since they are duplicated on the other.

When using the interface view, by default it will return all of the interfaces on the device. If you wish to see only a particular interface and all of it's logical interfaces, you can optionally specify the name of an interface using the option parameter, e.g.:

jnpr.View("interface", "ge-0/0/0")

Creating Custom Views

You can even create a custom view by creating a struct that models the XML output from using the GetConfig() function. Granted, this is a little more work, and requires you to know a bit more about the Go language (such as unmarshalling XML), but if there's a custom view that you want to see, it's possible to do this for anything you want.

I will be adding more views over time, but feel free to request ones you'd like to see by emailing me, or drop me a line on Twitter.

Example: View the ARP table on a device

view, err := jnpr.View("arp")
if err != nil {
    fmt.Println(err)
}

fmt.Printf("# ARP entries: %d\n\n", view.Arp.Count)
for _, a := range view.Arp.Entries {
    fmt.Printf("MAC: %s\n", a.MACAddress)
    fmt.Printf("IP: %s\n", a.IPAddress)
    fmt.Printf("Interface: %s\n\n", a.Interface)
}

// Will print out the following

# ARP entries: 4

MAC: 00:01:ab:cd:4d:73
IP: 10.1.1.28
Interface: reth0.1

MAC: 00:01:ab:cd:0a:93
IP: 10.1.1.30
Interface: reth0.1

MAC: 00:01:ab:cd:4f:8c
IP: 10.1.1.33
Interface: reth0.1

MAC: 00:01:ab:cd:f8:30
IP: 10.1.1.36
Interface: reth0.1

go-junos's People

Contributors

daemus avatar dfex avatar jamesboswell avatar scottdware avatar zachfi avatar zlesnr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

go-junos's Issues

using SSH keys

would love to have ability to use SSH keys to authenticate instead of password

Create/modify a policy

  • Specify source, destination, ports, etc. to build policy.
  • Assign to a device as well?

Allow user to specify RPC calls

Currently, users can run commands and output the format in text or XML, but these only run in operational mode.

  • Give the user the ability to run an RPC command of their choice (i.e.: jnpr.RPC("get-routing-engine-information")).
  • Have the ability to specify the output they want to see. A "struct-on-demand" type config?

Technically, this can be done via the RunCommand() call. You just can't specify the actual RPC XML in the request, but the "show" version of it instead.

Does this need to be changed?

Possible to configure from string text

Looking over the Config() method, its not clear to me if I can pass the config text in as a string. I see loading a file and such, but say I were to render a template. Would I be able to pass text in to the Config() method and have it get loaded to the device?

ConfigDiff() doesn't work on candidate configurations

ConfigDiff() uses rpcGetRollbackCompare which is using the following RPC

<get-rollback-information>
   <rollback>0</rollback>
   <compare>0</compare>
   <format>text</format>
</get-rollback-information>

That RPC is for comparing amongst active and previous rollback points. It does not work on candidate configurations.

Juniper.net documentation shows that the proper RPC to use is:

<get-configuration compare="rollback" rollback="[0-49]" format="text"/>

Where 0 would compare the candidate configuration to rollback 0 (the active config). Or one could compare to any of the previous commits (0-49)

Using the get-configuration RPC instead of get-rollback-information would allow
a diff to be generated for a for Config that has not yet been Commit.

This is useful for interactive sessions or merely logging the diff output to file
or other output (API/console, etc.)

Bug for CommitAt

if len(message) > 0 { command = fmt.Sprintf(rpcCommitAtLog, time) }

Should not it be :
if len(message) > 0 { command = fmt.Sprintf(rpcCommitAtLog, message) }

Get basic info per device type

Add functions to get some basic information from commonly used commands per device type. One for SRX, MX/routing, EX/switching, etc.

SRX

  • Session information
  • Zone information
  • IPSec tunnels

MX/routing

  • Route table information
  • Protocol information (BGP, OSPF, etc.)

EX/switching

  • MAC address table
  • Interface status/information

Create/modify a policy

  • Specify source, destination, ports, etc. to build policy.
  • Assign to a device as well?

bring your own SSH session/options

I think it would be nice to have a way to bring your own SSH session (net.Conn) or SSH config (ssh.ClientConfig). For example, to specify SSH connection timeout, or connect via a proxy.

IMHO, it would make the most sense (for backwards compatibility) to refactor NewSession and move all the post session-establishment code into a new function, and create an additional function to deal with the new BYOS features.

Let me know if you agree with adding this functionality and I'll implement it and submit a PR.

RPC error "expecting </configuration>" returned on use of GetConfig

Hi,

Whenever I try to retrieve a Junos config using:

jnpr, err := junos.NewSession(hostname, username, password)
config, err := jnpr.GetConfig("", "text")

I always get this error: netconf rpc [error]: syntax error, expecting </configuration>.

This doesn't seem to be device-specific as I get the same error on SRX, MX and QFX.

Is this a know bug or simply an error on my part?

I used go get about a week ago so the go-junos code I'm using should be fairly recent.

Thanks,
-Martin

Examples on updating

i was wondering do you have any code snippets for updating the switch , i.e. - rather than just operational commands, e.g. - set interface ge-0/0/44 description HbWjpLzzwGVwSMjsuUoL

Rollback without commit

In the case where I would like to compare a candidate config to the running config, the uncommitted session remains around waiting for commit. Rollback(0) looks like it would commit, even when there was nothing to commit.

Error on commit

Trying to commit now I get:

expected element type <commit-results> but have <ok>

Is this perhaps something upstream? I see that we are looking for commit-results in the XML, so perhaps this is a change on newer firmware?

Add SRX policy builder

Allow the creation of policies on an SRX like so:

  • Create a struct that will hold all the policies (XML format?) p := j.BuildPolicy()
  • Add functions to create multiple rules; many can be added: p.AddRule("name", "src-zone", src", "dst-zone", "dst", "service", "action")
    • src and dst can have multiple values, i.e.: "1.1.1.1/32, 2.2.2.0/24" or "[]string{"1.1.1.1/32", "2.2.2.0/24"}"
  • Assign all the rules to a variable which will get passed to Config(): config := p.CreatePolicy()
  • Create similar function for Junos Space?

Functions to create basic configuration items

  • Interfaces, VLAN's, routing-instances, users, etc.
  • SRX
    • Zones
  • Build the config in a struct first, then apply it?
  • Individual functions as well, for giving users different options?

jnpr.Views("...") or jnpr.View("...")

There seem to be a documentation error on your readme :

When I use jnpr.Views("..."), i get following errors:
./main.go:21:20: jnpr.Views undefined (type *junos.Junos has no field or method Views)

When I change to jnpr.View("..."), things will work, if this is an error, can you fix it ?

Filter for one particular interface

First, thanks for fixing the documentation error.

I have another question, if I just want to display one particular interface, how can I filter by interface name instead of getting all the interfaces ? Is there a way to pass in a filter on the following ?

  "interface":      "<get-interface-information/>",

Query job status

  • Create a function to query the jobs
  • Filter on a specific status?
  • Add the ability to filter on job ID, and return the info for said job.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.