Automation
Automation

Scripting How-To: Pick up the LDP label

by Cordelia on ‎08-10-2015 10:29 AM - edited on ‎09-18-2017 02:25 PM by Administrator Administrator (940 Views)

Overview

Use this op-script to pick up the LDP label and FEC based on session and direction. This applies to SLAX version 1.0 and higher.

 

Description

 

This op-script supports LDP in logical router.


Source Code

 

GitHub Links


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

Example Configuration

 

01	1. You can use this op-script to pick up the LDP Label & FEC based on session & direction. This op-script supports LDP in logical router.
02	2. How to use?
03	    a. Upload ldp-label.slax to /var/db/scripts/op
04	    b. configure router with command 'set system scripts op file ldp-label.slax'
05	3. You can use the following command
06	    a. op ldp-label -- the same as 'show ldp database'
07	    b. op ldp-label direction [input|output] -- display input label or output label
08	    c. op ldp-label fec <ip address> -- display the FEC you want to check
09	    d. op ldp-label label <label> -- display the ldp binded label
10	    e. op ldp-label session <ldp peer address> -- display input/output label binding from a peer
11	    f. op ldp-label session <ldp peer address> fec <ip address> label <label> direction [input|output] -- combine them to pick up what exactly you want

 

Example Output

 

001	user@cli> op ldp-label
002	Input label database, 1.1.1.1:0--2.2.2.2:0
003	  Label     Prefix
004	 299840     1.1.1.1/32
005	      3     2.2.2.2/32
006	 299792     3.3.3.3/32
007	 299808     4.4.4.4/32
008	 
009	Output label database, 1.1.1.1:0--2.2.2.2:0
010	  Label     Prefix
011	      3     1.1.1.1/32
012	 299872     2.2.2.2/32
013	 299888     3.3.3.3/32
014	 299904     4.4.4.4/32
015	 
016	Input label database, 1.1.1.1:0--3.3.3.3:0
017	  Label     Prefix
018	 299840     1.1.1.1/32
019	 299792     2.2.2.2/32
020	      3     3.3.3.3/32
021	 299808     4.4.4.4/32
022	 
023	Output label database, 1.1.1.1:0--3.3.3.3:0
024	  Label     Prefix
025	      3     1.1.1.1/32
026	 299872     2.2.2.2/32
027	 299888     3.3.3.3/32
028	 299904     4.4.4.4/32
029	 
030	user@cli> op ldp-label session 3.3.3.3
031	Input label database, 1.1.1.1:0--3.3.3.3:0
032	  Label     Prefix
033	 299840     1.1.1.1/32
034	 299792     2.2.2.2/32
035	      3     3.3.3.3/32
036	 299808     4.4.4.4/32
037	 
038	Output label database, 1.1.1.1:0--3.3.3.3:0
039	  Label     Prefix
040	      3     1.1.1.1/32
041	 299872     2.2.2.2/32
042	 299888     3.3.3.3/32
043	 299904     4.4.4.4/32
044	 
045	user@cli> op ldp-label direction input
046	Input label database, 1.1.1.1:0--2.2.2.2:0
047	  Label     Prefix
048	 299840     1.1.1.1/32
049	      3     2.2.2.2/32
050	 299792     3.3.3.3/32
051	 299808     4.4.4.4/32
052	 
053	Input label database, 1.1.1.1:0--3.3.3.3:0
054	  Label     Prefix
055	 299840     1.1.1.1/32
056	 299792     2.2.2.2/32
057	      3     3.3.3.3/32
058	 299808     4.4.4.4/32
059	 
060	user@cli> op ldp-label fec 3.3.3.3
061	Input label database, 1.1.1.1:0--2.2.2.2:0
062	  Label     Prefix
063	 299792     3.3.3.3/32
064	 
065	Input label database, 1.1.1.1:0--3.3.3.3:0
066	  Label     Prefix
067	      3     3.3.3.3/32
068	 
069	Output label database, 1.1.1.1:0--2.2.2.2:0
070	  Label     Prefix
071	 299888     3.3.3.3/32
072	 
073	Output label database, 1.1.1.1:0--3.3.3.3:0
074	  Label     Prefix
075	 299888     3.3.3.3/32
076	 
077	user@cli> op ldp-label label 299888
078	Output label database, 1.1.1.1:0--2.2.2.2:0
079	  Label     Prefix
080	 299888     3.3.3.3/32
081	 
082	Output label database, 1.1.1.1:0--3.3.3.3:0
083	  Label     Prefix
084	 299888     3.3.3.3/32
085	 
086	user@cli> op ldp-label direction output session 3.3.3.3 label 3 fec 1.1.1.1
087	Output label database, 1.1.1.1:0--3.3.3.3:0
088	  Label     Prefix
089	      3     1.1.1.1/32
090	 
091	user@cli> op ldp-label logical-router r2
092	Input label database, 2.2.2.2:0--1.1.1.1:0
093	  Label     Prefix
094	      3     1.1.1.1/32
095	 299872     2.2.2.2/32
096	 299888     3.3.3.3/32
097	 299904     4.4.4.4/32
098	 
099	Output label database, 2.2.2.2:0--1.1.1.1:0
100	  Label     Prefix
101	 299840     1.1.1.1/32
102	      3     2.2.2.2/32
103	 299792     3.3.3.3/32
104	 299808     4.4.4.4/32
105	 
106	Input label database, 2.2.2.2:0--4.4.4.4:0
107	  Label     Prefix
108	 299840     1.1.1.1/32
109	 299808     2.2.2.2/32
110	 299776     3.3.3.3/32
111	      3     4.4.4.4/32
112	 
113	Output label database, 2.2.2.2:0--4.4.4.4:0
114	  Label     Prefix
115	 299840     1.1.1.1/32
116	      3     2.2.2.2/32
117	 299792     3.3.3.3/32
118	 299808     4.4.4.4/32

 

SLAX Script Contents

 

001	/*
002	 *  ldp-label.slax
003	 *
004	 *  Created by Jeffrey Wei (jeffwei@juniper.net) on 20090816.
005	 *  Copyright (c) 2009 Juniper Networks. All rights reserved.
006	 *
007	 *  Version History
008	 *  ===============
009	 *  v1.0     Initial release.
010	 *
011	 */
012	 
013	version 1.0;
014	ns junos = "http://xml.juniper.net/junos/*/junos";
015	ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
016	ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
017	import "../import/junos.xsl";
018	 
019	param $session;
020	param $label;
021	param $fec;
022	param $direction;
023	param $logical-router;
024	 
025	var $arguments = {
026	    <argument> {
027	        <name> "label";
028	        <description> "MPLS label for a FEC";
029	    }
030	    <argument> {
031	        <name> "session";
032	        <description> "IP address of session destination";
033	    }
034	    <argument> {
035	        <name> "fec";
036	        <description> "FEC in inet.3";
037	    }
038	    <argument> {
039	        <name> "direction";
040	        <description> "Direction for session, Input for label from peer, Output for label sent to peer";
041	    }
042	    <argument> {
043	        <name> "logical-router";
044	        <description> "Name of logical system, or 'all'";
045	    }
046	}
047	 
048	match / {
049	    <op-script-results> {
050	        var $ldp = <get-ldp-database-information> {
051	            if (jcs:empty($logical-router)) {
052	                <instance> "master";
053	            }
054	            else {
055	                <logical-router> $logical-router;
056	            }
057	        }
058	        var $ldpinfo = jcs:invoke($ldp);
059	        <ldp-database-information> {
060	            if ($ldpinfo//xnm:error) {
061	                copy-of $ldpinfo//xnm:error;
062	            }
063	            else if (jcs:empty($direction)) {
064	                if (jcs:empty($label) && jcs:empty($session) && jcs:empty($fec)) {
065	                    copy-of $ldpinfo/*;
066	                }
067	                else if (jcs:empty($label) && not(jcs:empty($session)) && jcs:empty($fec)) {
068	                    for-each ($ldpinfo/ldp-database) {
069	                        call session($session);
070	                    }
071	                }
072	                else {
073	                    for-each ($ldpinfo/ldp-database[ldp-database-type="Input label database"]/ldp-binding) {
074	                        if (not(jcs:empty($label)) && jcs:empty($session) && jcs:empty($fec)) {
075	                            call label($label);
076	                        }
077	                        else if (jcs:empty($label) && jcs:empty($session) && not(jcs:empty($fec))) {
078	                            call fec($fec);
079	                        }
080	                        else if (not(jcs:empty($label)) && not(jcs:empty($session)) && jcs:empty($fec)) {
081	                            call label-session($label,$session);
082	                        }
083	                        else if (jcs:empty($label) && not(jcs:empty($session)) && not(jcs:empty($fec))) {
084	                            call session-fec($session,$fec);
085	                        }
086	                        else if (not(jcs:empty($label)) && jcs:empty($session) && not(jcs:empty($fec))) {
087	                            call label-fec($label,$fec);
088	                        }
089	                        else {
090	                            call session-label-fec($label,$session,$fec);
091	                        }
092	                    }
093	                    for-each ($ldpinfo/ldp-database[ldp-database-type="Output label database"]/ldp-binding) {
094	                        if (not(jcs:empty($label)) && jcs:empty($session) && jcs:empty($fec)) {
095	                            call label($label);
096	                        }
097	                        else if (jcs:empty($label) && jcs:empty($session) && not(jcs:empty($fec))) {
098	                            call fec($fec);
099	                        }
100	                        else if (not(jcs:empty($label)) && not(jcs:empty($session)) && jcs:empty($fec)) {
101	                            call label-session($label,$session);
102	                        }
103	                        else if (jcs:empty($label) && not(jcs:empty($session)) && not(jcs:empty($fec))) {
104	                            call session-fec($session,$fec);
105	                        }
106	                        else if (not(jcs:empty($label)) && jcs:empty($session) && not(jcs:empty($fec))) {
107	                            call label-fec($label,$fec);
108	                        }
109	                        else {
110	                            call session-label-fec($label,$session,$fec);
111	                        }
112	                    }
113	                }
114	            }
115	            else {
116	                var $path = <output> jcs:printf("%jcs label database",$direction);
117	                if ($path = "Input label database" || $path = "Output label database") {
118	                    if (jcs:empty($label) && jcs:empty($session) && jcs:empty($fec)) {
119	                        copy-of $ldpinfo/ldp-database[ldp-database-type = $path];
120	                    }
121	                    else if (jcs:empty($label) && not(jcs:empty($session)) && jcs:empty($fec)) {
122	                        for-each ($ldpinfo/ldp-database[ldp-database-type=$path]) {
123	                            call session($session);
124	                        }
125	                    }
126	                    else {
127	                        for-each ($ldpinfo/ldp-database[ldp-database-type=$path]/ldp-binding) {
128	                            if (not(jcs:empty($label)) && jcs:empty($session) && jcs:empty($fec)) {
129	                                call label($label);
130	                            }
131	                            else if (jcs:empty($label) && jcs:empty($session) && not(jcs:empty($fec))) {
132	                                call fec($fec);
133	                            }
134	                            else if (not(jcs:empty($label)) && not(jcs:empty($session)) && jcs:empty($fec)) {
135	                                call label-session($label,$session);
136	                            }
137	                            else if (jcs:empty($label) && not(jcs:empty($session)) && not(jcs:empty($fec))) {
138	                                call session-fec($session,$fec);
139	                            }
140	                            else if (not(jcs:empty($label)) && jcs:empty($session) && not(jcs:empty($fec))) {
141	                                call label-fec($label,$fec);
142	                            }
143	                            else {
144	                                call session-label-fec($label,$session,$fec);
145	                            }
146	                        }
147	                    }
148	                }
149	                else {
150	                    <xnm:error> {
151	                        <message> "You need to type Input or Output!!";
152	                    }
153	                }
154	            }
155	        }
156	    }
157	}
158	 
159	template label() {
160	    <ldp-database> {
161	        var $ldp-label = ./ldp-label;
162	        if ($ldp-label = $label) {
163	            copy-of ../ldp-database-type;
164	            copy-of ../ldp-session-id;
165	            copy-of .;
166	        }
167	    }
168	}
169	 
170	template session() {
171	    var $session-id = ./ldp-session-id;
172	    var $remote-session = jcs:split("--",$session-id);
173	    var $remote-ip = jcs:split(":",$remote-session[2]);
174	    if ($remote-ip[1] = $session) {
175	        copy-of .;
176	    }
177	}
178	 
179	template fec() {
180	    <ldp-database> {
181	        var $prefix = ./ldp-prefix;
182	        var $regex = "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+";
183	        var $ip = jcs:regex($regex,$prefix);
184	        if ($ip[1] = $fec) {
185	            copy-of ../ldp-database-type;
186	            copy-of ../ldp-session-id;
187	            copy-of .;
188	        }
189	    }
190	}
191	 
192	template label-session() {
193	    <ldp-database> {
194	        var $session-id = ../ldp-session-id;
195	        var $remote-session = jcs:split("--",$session-id);
196	        var $remote-ip = jcs:split(":",$remote-session[2]);
197	        var $ldp-label = ./ldp-label;
198	        if ($ldp-label = $label && $remote-ip[1] = $session) {
199	            copy-of ../ldp-database-type;
200	            copy-of ../ldp-session-id;
201	            copy-of .;
202	        }
203	    }
204	}
205	 
206	template session-fec() {
207	    <ldp-database> {
208	        var $session-id = ../ldp-session-id;
209	        var $remote-session = jcs:split("--",$session-id);
210	        var $remote-ip = jcs:split(":",$remote-session[2]);
211	        var $prefix = ./ldp-prefix;
212	        var $regex = "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+";
213	        var $ip = jcs:regex($regex,$prefix);
214	        if ($remote-ip[1] = $session && $ip[1] = $fec) {
215	            copy-of ../ldp-database-type;
216	            copy-of ../ldp-session-id;
217	            copy-of .;
218	        }
219	    }
220	}
221	 
222	template label-fec() {
223	    <ldp-database> {
224	        var $ldp-label = ./ldp-label;
225	        var $prefix = ./ldp-prefix;
226	        var $regex = "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+";
227	        var $ip = jcs:regex($regex,$prefix);
228	        if ($ldp-label = $label && $ip[1] = $fec) {
229	            copy-of ../ldp-database-type;
230	            copy-of ../ldp-session-id;
231	            copy-of .;
232	        }
233	    }
234	}
235	 
236	template session-label-fec() {
237	    <ldp-database> {
238	        var $session-id = ../ldp-session-id;
239	        var $remote-session = jcs:split("--",$session-id);
240	        var $remote-ip = jcs:split(":",$remote-session[2]);
241	        var $ldp-label = ./ldp-label;
242	        var $prefix = ./ldp-prefix;
243	        var $regex = "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+";
244	        var $ip = jcs:regex($regex,$prefix);
245	        if ($ldp-label = $label && $remote-ip[1] = $session && $ip[1] = $fec) {
246	            copy-of ../ldp-database-type;
247	            copy-of ../ldp-session-id;
248	            copy-of .;
249	        }
250	    }
251	}

 

XML Script Contents

 

01	<?xml version="1.0"?>
02	<script>
03	<title>ldp-label.slax</title>
04	<author>jeffwei</author>
05	<synopsis>
06	Use this op-script to pick up the LDP label and FEC based on session and direction.
07	</synopsis>
08	<coe>op</coe>
09	<type>protocols</type>
10	 
11	<description>
12	This op-script supports LDP in logical router.
13	   
14	 
15	</description>
16	 
17	 <example>
18	 <title>Basic Example</title>
19	 <config>example-1.conf</config>
20	 <output>example-1.output</output>
21	 </example>
22	 
23	<xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
24	src="../../../../../web/leaf.js"
25	type="text/javascript"/>
26	</script>