Blog Viewer

Scripting How-To: clear-bgp-neighbor script requires confirmation

By Erdem posted 08-12-2015 17:24

  
 

Overview

Safe version of clear bgp neighbor, requires confirmation before clearing all peers. This applies to SLAX version 1.0 and higher.

 

Description

 

This script provides a safe version of the clear bgp neighbor command. That command allows an operator to accidently clear all BGP sessions if  no peer is specified.This script requires "peer all" to be specified in order to clear all BGP peers,and it requires confirmation from the operator that they do indeed wish to clear all their BGP peers.

 

Minimum JUNOS Version: 9.4.
Latest Script Version: 1.0
MD5 Checksum: 25fce00659624c11c49d99101564be1d
SHA-256 Checksum: bd7ff2f070008a05c6a5ea44ed9ead97bd26db274021c2d61f25db689a0e3f6c

 

Source Code

 

GitHub Links

 

The source code below is also available from GitHub at the following locations:

 

 

SLAX Script Contents

 

001	/*
002	 * Author        : Curtis Call
003	 * Version       : 1.0
004	 * Last Modified : October 7, 2009
005	 * Platform      : all
006	 * Release       : 9.4 and above
007	 * License       : Public Domain
008	 *
009	 * This script provides a safe version of the clear bgp neighbor command.  That command
010	 * allows an operator to accidently clear all BGP sessions when no peer is specified.
011	 * This script requires "peer all" to be specified in order to clear all BGP peers,
012	 * and it requires confirmation from the operator that they do indeed wish to clear
013	 * all their BGP peers.
014	 *
015	 * Minimum JUNOS version is 9.4 due to the new jcs:input() extension function.
016	 */
017	 
018	version 1.0;
019	 
020	ns junos = "http://xml.juniper.net/junos/*/junos";
021	ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
022	ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
023	 
024	import "../import/junos.xsl";
025	 
026	/* Command-line arguments */
027	var $arguments = {
028	    <argument> {
029	        <name> "peer";
030	        <description> "Neighbor Address to drop or ALL for all sessions";
031	    }
032	    <argument> {
033	        <name> "instance";
034	        <description> "Perform action on specific routing-instance";
035	    }
036	    <argument> {
037	        <name> "logical-system";
038	        <description> "Perform action on specific logical-system or ALL";
039	    }
040	    <argument> {
041	        <name> "refresh";
042	        <description> "Specify 'soft', 'soft-inbound', or 'soft-minimum-igp'";
043	    }
044	}
045	param $peer;
046	param $logical-system;
047	param $instance;
048	param $refresh;
049	 
050	/* The base BGP command, we'll add to this based on the selected options */
051	var $base-command = "clear bgp neighbor";
052	 
053	match / {
054	    <op-script-results> {
055	         
056	        /* A peer is required, if not set then error and exit */
057	        if( not( $peer ) ) {
058	            <xsl:message terminate="yes"> "You must specify which peer you want to clear.";
059	        }
060	     
061	        /* Convert to upper case to check if they specified all peers */
062	        var $check-all = { call to-upper-case( $string = $peer ); }
063	         
064	        /* If peer ALL is used then ask for confirmation */
065	        if( $check-all == "ALL" ) {
066	         
067	            /* Request confirmation from user */
068	            var $response = jcs:input("This will clear ALL sessions\nAre you sure you want to do this? (yes/[no])" );
069	             
070	            /* Translate user response into upper case for easier matching */
071	            var $check-response = { call to-upper-case( $string = $response ); }
072	             
073	            /* If they want to go ahead with it then assemble the command */
074	            if( $check-response == "YES" ) {
075	             
076	                /* Retrieve all BGP neighbors */
077	                var $bgp-neighbors = jcs:invoke( "get-bgp-neighbor-information" ); 
078	                 
079	                var $results := {
080	                    for-each( $bgp-neighbors/bgp-peer ) {
081	                        /* Execute for each neighbor */
082	                        call execute-command( $passed-command = $base-command _ " " _ peer-address );
083	                    }
084	                }
085	                /* Return any errors */
086	                for-each ( $results//xnm:error ) {
087	                    copy-of .;
088	                }
089	                /* Report how many were cleared */
090	                var $count = count( $results/*[. == "Cleared 1 connections"] );
091	                if( $count > 0 ) {
092	                    <output> "Cleared " _ $count _ " connections";
093	                }
094	            }
095	            /* They decided not to clear all the sessions */
096	            else {
097	                <output> "Clear ALL cancelled";
098	            }
099	        }
100	        /* In this case, a specific peer has been specified */
101	        else {
102	            /* Execute with the base-command and a specific peer */
103	            call execute-command( $passed-command = $base-command _ " " _ $peer );
104	        }
105	    }
106	}
107	 
108	/*
109	 * This template executes the provided command along with any set global
110	 * parameters and prints the output to the screen.
111	 */
112	  
113	template execute-command( $passed-command ) {
114	     
115	    var $command-string = {
116	        expr $passed-command;
117	        if( string-length($refresh) > 0 ) {
118	            if( $refresh == "soft" ) {
119	                expr " soft";
120	            }
121	            else if( $refresh == "soft-inbound" ) {
122	                expr " soft-inbound";
123	            }
124	            else if( $refresh == "soft-minimum-igp" ) {
125	                expr " soft-minimum-igp";
126	            }
127	            else { /* Invalid setting, complain and exit now */
128	                <xsl:message terminate="yes"> "Invalid refresh argument: " _ $refresh;
129	            }
130	        }  
131	        if( string-length($instance) > 0 ) {
132	            expr " instance " _ $instance;
133	        }
134	        if( string-length($logical-system) > 0 ) {
135	            expr " logical-system " _ $logical-system;
136	        }  
137	    }
138	 
139	    /* There is no rpc for this command until JUNOS 9.6 so we have to do the raw command */
140	    var $command = <rpc> {
141	        <command> $command-string;
142	    }
143	 
144	    /* Execute the assembled command and print any output to the screen */
145	    var $result = jcs:invoke( $command );
146	    /*
147	     * Copy any output from the command to the screen.  Either error or
148	     * success will be shown to the user.  Use jcs:output so that the output
149	     * is shown inline with the other commands.
150	     */
151	    copy-of $result;
152	}
153	 
154	/*
155	 * Converts the provided string into upper case and returns the upper case
156	 * string to the starting template for variable assignment.
157	 */
158	template to-upper-case( $string = "" ) {
159	    expr translate( $string, "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
160	}

 

XML Script Contents

 

01	<?xml version="1.0"?>
02	<script>
03	  <title>clear-bgp-neighbor.slax</title>
04	  <author>curtisstephencall</author>
05	  <synopsis>
06	    Safe version of clear bgp neighbor, requires confirmation before clearing all peers.
07	  </synopsis>
08	  <coe>op</coe>
09	  <type>protocols</type>
10	 
11	  <description>
12	<![CDATA[
13	 This script provides a safe version of the clear bgp neighbor command.  That command
14	 allows an operator to accidently clear all BGP sessions when no peer is specified.
15	 This script requires "peer all" to be specified in order to clear all BGP peers,
16	 and it requires confirmation from the operator that they do indeed wish to clear
17	 all their BGP peers.
18	 
19	 Minimum JUNOS Version: 9.4.
20	 Latest Script Version: 1.0
21	 MD5 Checksum: 25fce00659624c11c49d99101564be1d
22	 SHA-256 Checksum: bd7ff2f070008a05c6a5ea44ed9ead97bd26db274021c2d61f25db689a0e3f6c
23	]]>
24	 
25	  </description>
26	 
27	  <keyword>slax</keyword>
28	  <keyword>bgp</keyword>
29	  <keyword>clear</keyword>
30	 
31	  <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
32	                src="../../../../../web/leaf.js"
33	            type="text/javascript"/>
34	</script>

#How-To
#ScriptingHow-To
#Slax
#opscript
#BGP

Permalink