Ejemplo n.º 1
0
class Manager():
    def __init__(self,host,user,password):
        self.dev = Device(host=host,user=user,password=password)
        self.dev.bind(cu=Config)

    def open(self):
        try:
            self.dev.open()
            print "Connection Open"
        except Exception as err:
            print err

    def close(self):
        try:
            self.dev.close()
            print "Connection Closed"
        except Exception as err:
            print err

    def get_facts(self):
        pprint(self.dev.facts)

    def open_config(self,type):
        try:
            #attempt to open a configuration
            output = self.dev.rpc("<open-configuration><{0}/></open-configuration>".format(type))
            print "Open {0} configuration".format(type)
        except Exception as err:
            #output an error if the configuration is not availble
            print err

    def load_config_template(self,template,template_vars):
        new_template = Template(template)
        final_template = new_template.render(template_vars)

        try:
            output = self.dev.cu.load(final_template,format="text",merge=True)
            print "Load Complete"
        except Exception as err:
            print err

    def commit_config(self):
        try:
            self.dev.rpc.commit_configuration()
            self.dev.rpc.close_configuration()
            print "Commit Complete"
        except Exception as err:
            print err
Ejemplo n.º 2
0
# you can also obtain the XML RPC for the associated command by
# doing this:

print "showing as XML RPC command:"
xml_cmd = jdev.cli("show version | display xml rpc")

# this is an actual XML element, so we can dump it for debug:
etree.dump(xml_cmd)

# showing as XML RPC command:
# <get-software-information>
# </get-software-information>

# you can then take that output then feed it back into the :rpc: metaexec

cmd_rsp = jdev.rpc(xml_cmd)

# now dump the XML response output;

print "showing as XML RPC response:"
etree.dump(cmd_rsp)

# showing as XML RPC response:
# <software-information>
# <host-name>jnpr-dc-fw</host-name>
# <product-model>junosv-firefly</product-model>
# <product-name>junosv-firefly</product-name>
# <package-information>
# <name>junos</name>
# <comment>JUNOS Software Release [12.1X44-D10.4]</comment>
# </package-information>
Ejemplo n.º 3
0
class JunosDevice():
    """
    JunosDevice

    Args:
        :host: string containing the host to connect to
        :username: string containing the username to authenticate with
        :password: string contining the password to authenticate with



    Examples:

    Basic device connection:

    .. code-block:: python

        from pyJunosManager import JunosDevice

        dev = JunosDevice(host="1.2.3.4",username="******",password="******")
        dev.open()
        print dev.get_facts()
        dev.close()

    """
    def __init__(self, host="", username="", password=""):
        self.dev = Device(host=host, user=username, password=password)
        self.dev.bind(cu=Config)

    def open(self):
        """
        Opens a NETCONF session to the specified Junos-based device

        Example:

        .. code-block:: python

            from pyJunosManager import JunosDevice

            //creates a connection to the Junos device
            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()

        """
        try:
            self.dev.open()
        except Exception as err:
            print err

    def close(self):
        """
        Closes a NETCONF session to the specified device

        Example:

        .. code-block:: python

            from pyJunosManager import JunosDevice

            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()
            dev.close()

        """
        try:
            self.dev.close()
        except Exception as err:
            print err

    def get_facts(self):
        """
        Returns the device facts as a Python dict

        Example:

        .. code-block:: python

            from pyJunosManager import JunosDevice
            import pprint

            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()
            facts = dev.get_facts()
            dev.close()

            pprint facts
        """
        return self.dev.facts

    def open_config(self, type="shared"):
        """
        Opens the configuration of the currently connected device

        Args:
            :type: The type of configuration you want to open. Any string can be provided, however the standard supported options are: **exclusive**, **private**, and **shared**. The default mode is **shared**.

        Examples:

        .. code-block:: python

            #Open shared config

            from pyJunosManager import JunosDevice

            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()
            dev.open_config()
            dev.close_config()
            dev.close()

            #Open private config

            from pyJunosManager import JunosDevice

            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()
            dev.open_config("private")
            dev.close_config()
            dev.close()
        """
        try:
            #attempt to open a configuration
            output = self.dev.rpc(
                "<open-configuration><{0}/></open-configuration>".format(type))
        except Exception as err:
            #output an error if the configuration is not availble
            print err

    def close_config(self):
        """
        Closes the exiting opened configuration

        Example:

        .. code-block:: python

            from pyJunosManager import JunosDevice

            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()
            dev.open_config()
            dev.close_config()
            dev.close()

        """
        try:
            self.dev.rpc.close_configuration()
        except Exception as err:
            print err

    def load_config_template(self, template, template_vars, type="text"):
        """
        :template: A templated string using Jinja2 templates
        :template_vars: A dict containing the vars used in the :template: string
        :type: The type of configuration to load. The default is "text" or a standard Junos config block. Other options are: "set" for set style commands, "xml" for xml configs

        Uses standard `Jinja2`_ Templating.

        .. _`Jinja2`: http://jinja.pocoo.org/

        Example:

        .. code-block:: python

            from pyJunosManager import JunosDevice

            config_template = "system { host-name {{ hostname }}; }"

            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()
            dev.open_config()
            dev.load_config_template(config_template,hostname="foo")
            dev commit_and_quit()
            dev.close()

        """
        new_template = Template(template)
        final_template = new_template.render(template_vars)

        try:
            output = self.dev.cu.load(final_template, format=type, merge=True)
        except Exception as err:
            print err

    def commit_config(self):
        """
        Commits exiting configuration

        Example:

        .. code-block:: python

            from pyJunosManager import JunosDevice

            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()
            dev.open_config()
            dev.commit_config()
            dev.close_config()
            dev.close()
        """
        try:
            self.dev.rpc.commit_configuration()
        except Exception as err:
            print err

    def commit_and_quit(self):
        """
        Commits and closes the currently open configration. Saves a step by not needing to manually close the config.

        Example:

        .. code-block:: python

            from pyJunosManager import JunosDevice

            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()
            dev.load_config_template("system{ host-name {{ hostname }};}",hostname="foo")
            dev commit_and_quit()
            dev.close()

        """
        try:
            self.dev.rpc.commit_configuration()
            self.close_config()
        except Exception as err:
            print err
Ejemplo n.º 4
0
        term deny-everything-else {
            then reject;
        }
    }
}
'''

dev = Device(host="10.0.1.234",user="******",password="******")
dev.open()
dev.bind(cu=Config)

pprint(dev.facts)

try:
    #attempt to open a configuration
    output = dev.rpc("<open-configuration><ephemeral/></open-configuration>")
except Exception as err:
    #output an error if the configuration is not availble
    print err

try:
    output = dev.cu.load(igmp_policy_template1,format="text",merge=True)
except Exception as err:
    print err

output = dev.rpc.commit_configuration()

output = dev.rpc.close_configuration()

print output
Ejemplo n.º 5
0
cmdline.add_argument("target", help="Target router to query")
cmdline.add_argument("command", help="Command line to run")
cmdline.add_argument("-p", metavar="port", help="TCP port", default=830)
cmdline.add_argument("-u",
                     metavar="username",
                     help="Remote username",
                     default=getpass.getuser())
args = cmdline.parse_args()

password = getPass(args.u + "@" +
                   args.target) if 'getPass' in globals() else ""
password = '******'
dev = Device(host=args.target, user=args.u, port=args.p, password=password)
dev.open()
dev.timeout = 60

rpc = dev.display_xml_rpc(args.command)
print etree.tostring(rpc, pretty_print=True)

if type(rpc) is etree._Element:
    rpc.attrib['format'] = 'text'
    print rpc
    result = dev.rpc(rpc)
    print result
    print result.text

else:
    print "XML RPC for command not available: ", rpc

dev.close()
Ejemplo n.º 6
0
class JunosDevice():
    """
    JunosDevice

    Args:
        :host: string containing the host to connect to
        :username: string containing the username to authenticate with
        :password: string contining the password to authenticate with



    Examples:

    Basic device connection:

    .. code-block:: python

        from pyJunosManager import JunosDevice

        dev = JunosDevice(host="1.2.3.4",username="******",password="******")
        dev.open()
        print dev.get_facts()
        dev.close()

    """
    def __init__(self,host="",username="",password=""):
        self.dev = Device(host=host,user=username,password=password)
        self.dev.bind(cu=Config)

    def open(self):
        """
        Opens a NETCONF session to the specified Junos-based device

        Example:

        .. code-block:: python

            from pyJunosManager import JunosDevice

            //creates a connection to the Junos device
            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()

        """
        try:
            self.dev.open()
        except Exception as err:
            print err

    def close(self):
        """
        Closes a NETCONF session to the specified device

        Example:

        .. code-block:: python

            from pyJunosManager import JunosDevice

            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()
            dev.close()

        """
        try:
            self.dev.close()
        except Exception as err:
            print err

    def get_facts(self):
        """
        Returns the device facts as a Python dict

        Example:

        .. code-block:: python

            from pyJunosManager import JunosDevice
            import pprint

            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()
            facts = dev.get_facts()
            dev.close()

            pprint facts
        """
        return self.dev.facts

    def open_config(self,type="shared"):
        """
        Opens the configuration of the currently connected device

        Args:
            :type: The type of configuration you want to open. Any string can be provided, however the standard supported options are: **exclusive**, **private**, and **shared**. The default mode is **shared**.

        Examples:

        .. code-block:: python

            #Open shared config

            from pyJunosManager import JunosDevice

            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()
            dev.open_config()
            dev.close_config()
            dev.close()

            #Open private config

            from pyJunosManager import JunosDevice

            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()
            dev.open_config("private")
            dev.close_config()
            dev.close()
        """
        try:
            #attempt to open a configuration
            output = self.dev.rpc("<open-configuration><{0}/></open-configuration>".format(type))
        except Exception as err:
            #output an error if the configuration is not availble
            print err

    def close_config(self):
        """
        Closes the exiting opened configuration

        Example:

        .. code-block:: python

            from pyJunosManager import JunosDevice

            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()
            dev.open_config()
            dev.close_config()
            dev.close()

        """
        try:
            self.dev.rpc.close_configuration()
        except Exception as err:
            print err

    def load_config_template(self,template,template_vars,type="text"):
        """
        :template: A templated string using Jinja2 templates
        :template_vars: A dict containing the vars used in the :template: string
        :type: The type of configuration to load. The default is "text" or a standard Junos config block. Other options are: "set" for set style commands, "xml" for xml configs

        Uses standard `Jinja2`_ Templating.

        .. _`Jinja2`: http://jinja.pocoo.org/

        Example:

        .. code-block:: python

            from pyJunosManager import JunosDevice

            config_template = "system { host-name {{ hostname }}; }"

            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()
            dev.open_config()
            dev.load_config_template(config_template,hostname="foo")
            dev commit_and_quit()
            dev.close()

        """
        new_template = Template(template)
        final_template = new_template.render(template_vars)

        try:
            output = self.dev.cu.load(final_template,format=type,merge=True)
        except Exception as err:
            print err

    def commit_config(self):
        """
        Commits exiting configuration

        Example:

        .. code-block:: python

            from pyJunosManager import JunosDevice

            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()
            dev.open_config()
            dev.commit_config()
            dev.close_config()
            dev.close()
        """
        try:
            self.dev.rpc.commit_configuration()
        except Exception as err:
            print err

    def commit_and_quit(self):
        """
        Commits and closes the currently open configration. Saves a step by not needing to manually close the config.

        Example:

        .. code-block:: python

            from pyJunosManager import JunosDevice

            dev = JunosDevice(host="1.2.3.4",username="******",password="******")
            dev.open()
            dev.load_config_template("system{ host-name {{ hostname }};}",hostname="foo")
            dev commit_and_quit()
            dev.close()

        """
        try:
            self.dev.rpc.commit_configuration()
            self.close_config()
        except Exception as err:
            print err