PYEZ - Display set | Match command

1.7k views Asked by At

I have been trying to run the below commands through below program but they are fetching the whole of config file and not the result of match command . Is match function not supported through PYEZ?

show configuration | display set | match RI- show configuration | display set | match POOL- show configuration | display set | match SERVICE-

My purpose was to get the set command lines of match RI command, replace set with delete and load to the device.

Please help.

try: log.debug("collecting the router configuration")

    now = datetime.datetime.now()
    datets = str(now.year) + str(now.month) + str(now.day) + "_" + str(now.hour) + str(now.minute) + str(now.second)
    log.debug("timestamp set to " + str(datets))

    if (format == "cnf"):
        cnf = dev.cli("show configuration", warning=False)
        FileName = rtName + "." + datets + ".cnf"
        log.debug("The configuration will be stored in filename as %s", FileName)

        # saving the configuration into a CNF file
        f = open(FileName, 'w+')
        f.write(cnf)
        f.close
        return FileName

    elif (format == "set"):
        cnf = dev.cli("show configuration | display set | match pool-", warning=False)
        FileName = rtName + "." + datets + ".txt"
        log.debug("The configuration will be stored in filename as %s", FileName)
        # saving the configuration into a Text file
        f = open(FileName, 'w+')
        f.write(cnf)
        f.close
        return FileName

    else: # defaults to XML
        cnf = dev.rpc.get_config()
        FileName = rtName + "." + datets + ".xml"
        log.warn("The configuration will be stored in filename as %s", FileName)

        # saving the configuration into a XML file
        f = open(FileName, 'w+')
        f.write(etree.tostring(cnf))
        f.close
        return FileName

except Exception as e:
    log.error("could not collect the router configuration via RPC")
    log.error(e.message)
    return None


# if the execution gets here, the return will be None
return FileName
1

There are 1 answers

0
Stacy Smith On BEST ANSWER

As the warning says, the dev.cli() method is for debug purposes only. It does not support any | match modifiers. This is because it's not actually executing the command at the CLI prompt, but sending the <command> RPC over a NETCONF session, and there is a Junos limitation that the <command> RPC doesn't support | match modifiers.

In your case, I suggest you retrieve the configuration using dev.rpc.get_config(). You can specify the filter_xml argument to retrieve a subset of the configuration. http://junos-pyez.readthedocs.io/en/2.1.2/jnpr.junos.html#jnpr.junos.rpcmeta._RpcMetaExec.get_config

For example, to retrieve all [edit routing-instances] configuration in set format you could do:

>>> rsp = dev.rpc.get_config(filter_xml='<routing-instances/>', options={'format':'set'})
>>> print rsp.text

set routing-instances bar instance-type virtual-router
set routing-instances foo instance-type virtual-router

You can only specify a configuration hierarchy using the filter_xml argument. You can not match a specific text pattern. However, you can always download a configuration hierarchy and then do the string matching on the response using normal Python string methods.

--Stacy