O'Reilly logo

JUNOS Automation Cookbook by Adam Chappell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

We're going to make a new Python app, rest.py, that will connect to the Junos OS REST interface over SSH.

  1. Create the Python script preamble and import the necessary standard library routines.
    #!/usr/bin/env python    import sys    import httplib    import ssl    import base64    import argparse    import getpass    import json    import os    import io     # Error handling     def onError(exception_type, exception, traceback):       sys,stderr.write("%s: %s\n" % (exception_type.__name__,        exception))     sys.excepthook = onError
  1. Parse the following command-line arguments:
   cmdline = argparse.ArgumentParser(description="Python                                     JUNOS REST Client")   cmdline.add_argument("target", help="router - target                          router to query") cmdline.add_argument("-t", choices=["xml", "json", ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required