Junos Automation (Scripting)
Reply
Contributor
tnhphuong
Posts: 32
Registered: ‎07-25-2008
0

write scripts delete next-hop for static route

Hi!

 

I have configuration static route:

 

 

        routing-options {
            static {
                route 200.200.200.0/24 {
                    next-hop 10.10.10.2;
                    qualified-next-hop 20.20.20.2 {
                        preference 7;
                    }
                }
            }
        }

[edit event-options]

policy remove-route {

    events ping_test_failed; à 

    within 10 {

        trigger on 1;

    }

    then {

        event-script remove-route.slax; 

    }

}

 

 

 [edit service rpm]

probe icmp_ping_test {

    test ping_test {

        probe-type icmp-ping;

        target address 70.70.70.1;

        test-interval 1;

        source-address 30.30.30.3;

    }


 

 

So i want when event PING_TEST_FAILED from RPM, the scripts automatic delete next-hop 10.10.10.2. New configuration will be:

 

 

        routing-options {
            static {
                route 200.200.200.0/24 {
                    qualified-next-hop 20.20.20.2 {
                        preference 7;
                    }
                }
            }
        
Please help me to write  remove-route.slax  to do this task. I have write this scripts but it don't working.
 
version 1.0;
import "../import/junos.xsl";
match / {
    <event-script-results> {
        
        var $con = jcs:smileysurprised:pen();
        if (not($con)) {
            <xnm:error> {
                <message> "Not able to connect with local mgd";
            }
        }
        
        var $change-route = { 
            <configuration> {
                <routing-options> {
                    <static> {
                        <route> {
                            <name> "200.200.200.0/24";
                            <next-hop delete="delete"> "10.10.10.2";
                        }
                        
                    }
                }                
            }
        }
        var $change-route-results = {
            call jcs:load-configuration($connection = $con,
                                        $configuration = $change-route);
        }
    }
}
 
or change: 
version 1.0;
import "../import/junos.xsl";
match / {
    <event-script-results> {
        
        var $con = jcs:smileysurprised:pen();
        if (not($con)) {
            <xnm:error> {
                <message> "Not able to connect with local mgd";
            }
        }
        
        var $change-route = { 
            <configuration> {
                <routing-options> {
                    <static> {
                        <route delete="delete"> {
                            <name> "200.200.200.0/24";
                            <next-hop> "10.10.10.2";
                        }                        
                    }
                }                
            }
        }
        var $change-route-results = {
            call jcs:load-configuration($connection = $con,
                                        $configuration = $change-route);
        }
    }
}
Please help me.
Thanks & best regards,
Phuong

 

Recognized Expert
Mattia
Posts: 198
Registered: ‎03-17-2010
0

Re: write scripts delete next-hop for static route

[ Edited ]

Hello Phuong,

 

I tested your script (using the logger utility in order to simulate the failing of the rpm probe) and actually it seems to work fine, as you can see from the following output:

 

 

root@SCRIPT-TEST# show 
route 200.200.200.0/24 {
    next-hop 10.10.10.2;
    qualified-next-hop 20.20.20.2 {
        preference 7;
    }
}

[edit routing-options static]
root@SCRIPT-TEST# run start shell 
root@SCRIPT-TEST% logger -e PING_TEST_FAILED
root@SCRIPT-TEST% exit
exit

[edit routing-options static]
root@SCRIPT-TEST# show 
route 200.200.200.0/24 {
    qualified-next-hop 20.20.20.2 {
        preference 7;
    }
}

[edit routing-options static]
root@SCRIPT-TEST# 

 

You can monitor the execution of the script also from the escript logs (note that I renamed your script into remove-routev0.slax, but I did not change anything in the code):

 

 

root@SCRIPT-TEST# run show log escript.log
Apr 15 16:40:09 inspecting event output 'remove-routev0.slax' Apr 15 16:40:09 finished event script 'remove-routev0.slax' Apr 15 16:40:09 event script processing ends Apr 15 16:41:34 event script processing begins Apr 15 16:41:34 reading event details Apr 15 16:41:34 testing event details Apr 15 16:41:34 running event script 'remove-routev0.slax' Apr 15 16:41:34 opening event script '/var/db/scripts/event/remove-routev0.slax' Apr 15 16:41:34 reading event script 'remove-routev0.slax' Apr 15 16:41:43 event script output Apr 15 16:41:43 begin dump <?xml version="1.0"?> <event-script-results xmlns:junos="http://xml.juniper.net/junos/*/junos" xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm" xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0"/> Apr 15 16:41:43 end dump Apr 15 16:41:43 inspecting event output 'remove-routev0.slax' Apr 15 16:41:43 finished event script 'remove-routev0.slax' Apr 15 16:41:43 event script processing ends

Maybe the event script is not being triggered correctly by the rpm probe? Or the commit is failing due to a configuration lock?

 

My advise is to check the messages and escript log file in order to understand what is not working..

 

Let me know if this helps,

 

kind regards,

 

Mattia

 

 

 

.................................................................................
JNCIP-ENT, JNCIP-SEC, JNCIS-SP
(If this post helped you, please mark it as an "Accepted Solution"; kudos are also appreciated!)


New User
danny_boy
Posts: 2
Registered: ‎03-21-2012
0

Re: write scripts delete next-hop for static route

Hi,

 

I am facing the same issue, seems the rpm service can;t not trigger the script running, would some please take a look?

 

I have a routing instance "test", and setting up a RPM which monitor the connection to 172.30.109.1. If it failed, delete the static route with next-hop 172.30.109.1 with destination 2.2.2.0/24. and use the static route with next-hop 172.30.109.5 instead.

 

Wooded someone pls advise?

 

 

root@primary# show event-options
inactive: policy remove-route {
    events ping_probe_failed;
    then {
        execute-commands {
            commands {
                "show configuration | match OSPF";
            }
        }
        event-script remove-route.slax;
    }
}
policy EVENT-POLICY-1 {
    events ping_test_failed;
    within 60 {
        trigger on 3;
    }
    then {
        event-script remove-route.slax;
    }
}
event-script {
    file remove-route.slax;
}

root@primary> show configuration services
rpm {
    probe OWNER-1 {
        test TEST-1 {
            probe-type icmp-ping;
            target address 172.30.109.1;
            test-interval 1;
            source-address 172.30.109.2;
            routing-instance test;
        }
    }
}

{primary:node0}

root@primary> show configuration routing-instances test
instance-type virtual-router;
interface reth2.0;
interface reth3.0;
routing-options {
    graceful-restart;
    static {
        route 2.2.2.0/24 {
            next-hop 172.30.109.1;
            qualified-next-hop 172.30.109.5 {
                preference 7;
            }
            resolve;
        }
    }
    router-id 10.240.88.3;
}


HEre is my scripts:

version 1.0;
ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
import "../import/junos.xsl";
match / {
    <event-script-results> {
       
        var $con = jcs:smileysurprised:pen();
        if (not($con)) {
            <xnm:error> {
                <message> "Not able to connect with local mgd";
            }
        }
       
var $change-route = <configuration> {
                <routing-instances> {
                    <instance> {
                      <name> "test";
                        <routing-options> {
                            <static> {
                                <route> {
                                   <name> "2.2.2.0/24";
                                         <next-hop delete="delete"> "172.30.109.1";
                        }
                    }
                }
            }
         }
}

        var $change-route-results = {
            call jcs:load-configuration($connection = $con,$configuration = $change-route);
        }
    }
}
 

 

 

Recognized Expert
Mattia
Posts: 198
Registered: ‎03-17-2010
0

Re: write scripts delete next-hop for static route

Hi, to investigate the triggering of the script, I'd configure traceoptions under the event-options configuration hierarchy:

 

set event-options traceoptions file TRACE_EVENT_OPTIONS
set event-options traceoptions file size 1m
set event-options traceoptions flag all

Then you can either disable the interface connected to the primary next-hop ( 172.30.109.1), or you can use the logger tool in order to simulate the failure of an rpm probe.

 

root@SCRIPT-TEST# run start shell 
root@SCRIPT-TEST% logger -e PING_TEST_FAILED
root@SCRIPT-TEST% exit

 

Then you should inspect the log file, typing "show log TRACE_EVENT_OPTIONS", in order to see if the script has been kicked off correctly.


Let me know what you find!

.................................................................................
JNCIP-ENT, JNCIP-SEC, JNCIS-SP
(If this post helped you, please mark it as an "Accepted Solution"; kudos are also appreciated!)


Trusted Contributor
acecanal
Posts: 149
Registered: ‎07-05-2011
0

Re: write scripts delete next-hop for static route

  Hi Phuong.

 

  I just start writting some scripts, and my first mistake was not properly register this, i dont know if your event options configuration is complete, but this looks like this is your mistake.

 

  The complete configuration should looks like this, look at the bottom :

 

[edit event-options]

policy remove-route {

    events ping_test_failed; à 

    within 10 {

        trigger on 1;

    }

    then {

        event-script remove-route.slax; 

    }

}

 

/* i miss this configuration from your event options configuration.

   this is the way to register the event script under junos, if not will look for the script as a OP script under the op directory.

   Registering this way the script will look under the event directory.

*/

event-script {

       file remove-route.slax;

}

 

  You are using an event script, so you will have to keep this in the /var/db/scripts/event directory, and register this.

 

   Br

   Alex

 

 

Br
Alex

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you want to say thanks, the word is Kudos!!.

Thx.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

JNCIA-JUNOS, JNCIS-ENT, JNCIS-SP, JNCIP-SP.
CCNA, CCNP, Written CCIE.
New User
danny_boy
Posts: 2
Registered: ‎03-21-2012
0

Re: write scripts delete next-hop for static route

[ Edited ]
hi, thanks for your reply.

however, I made a very sample rpm and operation scripts. and after I disconnect the interface, the test-out.log file doesnt created in /var/tmp/....
And I perform show service rpm withat that rpm, it shows the result is 100% failed.

I just wonder if I need to have any prerequisite on creating RPM....please advise.

I used SRX100 with cluser formed

The configuration is shown as below.
event-options {
    policy EVENT-POLICY-1 {
        events ping_test_failed;
        within 60 {
            trigger on 3;
        }
        then {
            execute-commands {
                commands {
                    "show system uptime | save /var/tmp/test-out.log";
                }                      
                output-format text;
            }
        }
    }
} 
services {
    rpm {
        probe OWNER-1 {
            test TEST-1 {
                probe-type icmp-ping;
                target address 172.30.109.1;
                test-interval 3;
                source-address 172.30.109.2;
routing-instance test;
 } } } }
Copyright© 1999-2013 Juniper Networks, Inc. All rights reserved.