troubleshooting Question

Search for string, echo value to new file

Avatar of Rich Rumble
Rich RumbleFlag for United States of America asked on
Web Languages and StandardsPerl
14 Comments1 Solution1438 ViewsLast Modified:
Hopefully this is more of a challenge than most of the find/replace perl scripts I've been searching through....

I want to parse data from cisco router and switch config files saved in a central folder, and place certain values in new files between meta-tags (xml specifically)
I want the script to detect if the file is a switch or router, based on the host name value, then use the appropriate "find" for that device.

My router config files have this line: "hostname uscalart0003" (no quotes in the file btw.) another router in NewYork is, "hostname usnynyrt0001" each host name will contain "rt" then 4 digits, mostly triple zero then 1-5 max.
My switches have this is the hostname value: "set system name  uscalasw0002" or "set system name  usnynysw0001" this time "sw000X" is how we can differentiate a router from a switch config, there are strings that overlap that make me think we need to add that step.

The values I wish to pull from the switch are in the lines:
#version 7.6(10)  (echo this to a new file inbetween these meta-tags     <version>7.6(10)</version>
set system name  usnynysw0001   echo'd to <name>usnynysw0001</name>
set tacacs server 10.0.1.15
set tacacs server 10.1.1.15  (echo these to <tacacs>10.0.1.215, 10.1.1.215</tacacs>   or  <tacacs>10.0.1.15</tacacs>  and <tacacs>10.1.1.15</tacacs>

Things like that, I can take the code an run with it, adding values to search for etc...

For the router files, let's start with this:
version 12.2            <version>12.2</version>
hostname uscalart0003    <name>uscalart0003</name>
boot system flash:c7200-p-mz.122-7b.bin        <bootfile>c7200-p-mz.122-7b.bin</bootfile>
interface FastEthernet0/0          <interface>FastEthernet0/0</interface>
description trunk to uscalasw0002 3/21 <description>trunk to uscalasw0002 3/21</description>
duplex full      <duplex>full</duplex>

let me know if you need more clarification, thanks for your time!
-rich

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 14 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 14 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros