Automation
Automation

Scripting How-To: Correlate a host IP address to a switch port interface

by Cordelia on ‎08-10-2015 10:01 AM - edited on ‎09-11-2017 04:43 PM by Administrator Administrator (1,352 Views)

Correlate a Host IP Address to a Switch Port Interface

 

For SLAX version 1.0 and higher, you can use an operation command shortcut to see what IP address is bound to which interface. The shortcut combines the output of 'show ethernet-switching table', 'show arp' and 'show vlans'.

 

Source Code and GitHub Links


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

Example Output

 

01	admin@geri> show arp no-resolve
02	MAC Address       Address         Interface     Flags
03	00:1c:c0:61:56:e1 10.128.10.2     vlan.0               none
04	06:0c:c3:50:3f:5e 10.128.10.5     vlan.0               none
05	 
06	admin@geri> show ethernet-switching table   
07	Ethernet-switching table: 5 entries, 3 learned
08	  VLAN              MAC address       Type         Age Interfaces
09	  default           *                 Flood          - All-members
10	  default           00:0c:c3:50:3f:5e Learn          0 ge-0/0/1.0
11	  default           00:1c:c0:61:56:e1 Learn          0 ge-0/0/0.0
12	  default           00:26:88:06:85:c8 Static         - Router
13	  default           06:0c:c3:50:3f:5e Learn          0 ge-0/0/1.0
14	 
15	From these you can see that 10.128.10.5 (06:0c:c3:50:3f:5e) is in ge-0/0/1.0 and 10.128.10.2 (00:1c:c0:61:56:e1) is in ge-0/0/0.0
16	 
17	This find-ip op-script will enable you to quickly show the above mapping.

 

SLAX Script Contents

 

001	/*
002	*
003	*    NAME: find-ip.slax
004	*    PURPOSE: Created for SouthCentral Team to combine the output of
005	*             the "show ethernet-switching table" command, the "show
006	*             arp" command, and the "show vlans" command.
007	*
008	*    CREATED: 03/22/09
009	*    BY: Jay Wilson (Solutions Architect - Western Area)
010	*    LAST MOD: 03/23/09
011	*    BY: Jay Wilson
012	*    VERSION: 3.00
013	*
014	*    MODIFICATION HISTORY:
015	*        V1.00 = Initial release
016	*        V2.00 = Added template WRITE-IT
017	*                Added the "z" option used for tracing
018	*                  script flow.
019	*        V2.50 = Added vlan information to the output
020	*                Added age information to the output
021	*        V3.00 = Created the template mash-vid that
022	*                  is used to put the VID (vlan-id) on
023	*                  the output.
024	*
025	*/
026	version 1.0;
027	ns junos = "http://xml.juniper.net/junos/*/junos";
028	ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
029	ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
030	import "../import/junos.xsl";
031	var $arguments = {
032	    <argument> {
033	        <name> "ip";
034	        <description> "IP address you are searching for";
035	    }
036	    <argument> {
037	        <name> "z";
038	    }
039	}
040	param $ip;
041	param $z;
042	 
043	 
044	match / {
045	    <op-script-results> {
046	/*
047	*
048	*  RPC that returns the "show ethernet-switching table" output
049	*
050	*/
051	        var $rpc = <get-ethernet-switching-table-information> {
052	        }
053	        var $switch-output = jcs:invoke($rpc);
054	/*
055	*
056	*  RPC that returns the "show arp" output
057	*
058	*/
059	    var $rpc2 = <get-arp-table-information> {
060	    }
061	    var $arp-output = jcs:invoke($rpc2);
062	/*
063	*
064	*  RPC that returns the "show vlans" output
065	*
066	*/
067	    var $rpc3 = <get-vlan-information> {
068	    }
069	    var $vlans-output = jcs:invoke($rpc3);
070	/*
071	*
072	*   Print out the Header line
073	*
074	*/
075	 
076	        <output> jcs:printf("%-16s %-18s %-12s %5s  %4s  %-s\n","IP","MAC","Interface","Age","VID","VLAN");
077	 
078	/*
079	*
080	*   Process the arp table information.  If an specific,
081	*   IP was requested, only grab that ip.
082	*
083	*/
084	    if ($ip) {
085	       call write-it ($message = "ip specified");
086	       for-each ($arp-output/arp-table-entry) {
087	           if ($ip == ./ip-address) {
088	           call write-it ($message = "ip found");
089	           call mash-up ($arp-ip = ./ip-address, $arp-mac = ./mac-address, $yes-ip = $ip, $switch-output = $switch-output, $vlans-output = $vlans-output);
090	           }
091	       }
092	    }
093	    else {
094	       call write-it ($message = "no ip specified");
095	       for-each ($arp-output/arp-table-entry) {
096	           call mash-up ($arp-ip = ./ip-address, $arp-mac = ./mac-address, $yes-ip = $ip, $switch-output = $switch-output, $vlans-output = $vlans-output);
097	       }
098	    }
099	    }
100	}
101	/*
102	*   NAME: MASH-UP
103	*   PURPOSE: To combine the ethernet-switching-table-information output
104	*            with the arp-table-information output.
105	*   CALLED: Called for all interfaces that are returned by the RPC
106	*           to ethernet-switching-table-information and that match any
107	*           additional criteria, such IP
108	*
109	*   PARMS PASSED:
110	*        $arp-ip = the IP address on the current arp table record
111	*        $arp-mac = the MAC address retrieved from the arp table
112	*        $yes-ip = the IP address the user is looking for
113	*        $switch-output = the structure that goes with the interface
114	*                         that was returned with the RPC
115	*                         ethernet-switching-table-information.
116	*        $vlans-output = the structure for all vlan information.
117	*                        this will be used to get the VID (vlan-id).
118	*
119	*/
120	template mash-up($arp-ip, $arp-mac, $yes-ip, $switch-output, $vlans-output) {
121	 
122	       for-each($switch-output/ethernet-switching-table/mac-table-entry) {
123	           if ($arp-mac == ./mac-address) {
124	               call write-it($message = "arp-found");
125	               call mash-vid($arp-ip = $arp-ip, $arp-mac = $arp-mac, $mac-interface = mac-interfaces-list/mac-interfaces, $mac-age = mac-age, $mac-vlan = mac-vlan, $vlans-output = $vlans-output);
126	           }
127	       }
128	}
129	/*
130	*   NAME: MASH-VID
131	*   PURPOSE: To combine the vlan information needed (vlan-id) with the
132	*            information already retrieved from
133	*            ethernet-switching-table-information output and the
134	*            arp-table-information output.
135	*   CALLED: Called by MASH-UP after a match has been found between
136	*           ethernet-switching-table and the arp-table.
137	*
138	*   PARMS PASSED:
139	*        $arp-ip = the IP address on the current arp table record
140	*        $arp-mac = the MAC address retrieved from the arp table
141	*        $mac-inteface = the inteface the found MAC address is attached to
142	*        $mac-age = the age timer for the MAC address
143	*        $mac-vlan = the vlan "name" the MAC address is a member of
144	*                    this is used to get the vlan-id
145	*        $vlans-output = the structure for all vlan information.
146	*                        this will be used to get the VID (vlan-id).
147	*
148	*/
149	template mash-vid($arp-ip, $arp-mac, $mac-interface, $mac-age, $mac-vlan, $vlans-output) {
150	    for-each($vlans-output/vlan) {
151	        if ($mac-vlan == vlan-name) {
152	                   <output> jcs:printf("%-16s %-18s %-12s %5s  %4s  %-s\n",$arp-ip,$arp-mac,$mac-interface,$mac-age,vlan-tag,$mac-vlan);
153	        }
154	    }
155	}
156	/*
157	*   NAME: WRITE-IT
158	*   PURPOSE: Writes a message to standard output that is passed to it.
159	*   CALLED: Can be called at any time from any where; however, the
160	*           "z" option must be set to a non-null value when the script
161	*           is invoked.
162	*
163	*   PARMS PASSED:
164	*        $message = The string to print out
165	*
166	*/
167	template write-it($message) {
168	    if ($z) {
169	        <xnm:warning> {
170	            <message> {
171	                expr $message;
172	            }
173	        }
174	    }
175	}

 

XML Script Contents

 

01	<?xml version="1.0"?>
02	<script>
03	  <title>find-ip.slax</title>
04	  <author>jayw</author>
05	  <synopsis>
06	    See what IP address is bound to which interface
07	  </synopsis>
08	  <coe>op</coe>
09	  <type>display</type>
10	 
11	  <description>
12	Combine the output of 'show ethernet-switching table' command, the 'show arp' command, and the 'show vlans' command.
13	 
14	  </description>
15	 
16	  <example>
17	    <title>Output</title>
18	    <description>A shortcut command to quickly see what ip address is bound to which interface</description>
19	    <output>example-1.conf</output>
20	  </example>
21	 
22	  <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
23	                src="../../../../../web/leaf.js"
24	            type="text/javascript"/>
25	</script>