LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-05-2008, 02:19 PM   #1
bboyz
LQ Newbie
 
Registered: May 2008
Posts: 4

Rep: Reputation: 0
Parsing input into separate files


I'm trying to parse the input of 1 command set into 3 separate files.
Running ciscocmd program with 3 commands listed in a command file - show cdp nei, show vtp status and show tech.

The script goes out and runs the commands on the devices and then sends the output back to the screen or to a batch file for further processing line by line until the entire job is complete.

OutPut ex.

TEC-2950-2#show cdp nei
Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge
S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone

Device ID Local Interface Holdtme Capability Platform Port ID
TEC-XXXX-2
Gig 0/1 129 R S I WS-XXXXX Gig 2/3
TEC-2950-2#show vtp status
VTP Version : 2
Configuration Revision : 89
Maximum VLANs supported locally : 250
Number of existing VLANs : 60
VTP Operating Mode :
VTP Domain Name :
VTP Pruning Mode :
VTP V2 Mode :
VTP Traps Generation :
MD5 digest :
Configuration last modified by x.x.x.x at 1-22-08 19:10:35
TEC-XXXX-2#

My batch:
#!/bin/sh
#
# mybatch

while read line
do echo "$line" >> $CISCOHOST.txt

Which outputs everything into a file.
What I'd like to have is $CISCOHOST-show_cdp_nei.txt and $CISCOHOST-show_vtp_status.txt .... files

Would it be easier to parse from a file or from the input as it's coming in?
 
Old 05-05-2008, 03:23 PM   #2
eggixyz
Member
 
Registered: Apr 2008
Posts: 310

Rep: Reputation: 30
Hey There,

If you're running the batch and it's executing the commands remotely, you could just split the output based on the command execution.

If I'm misunderstanding and you're just getting the output back with the command name, it would be just as easy to parse while the info's coming back as it would parsing a file. The only thing I'm not sure about, with the cisco is whether your real-time tty echo's that you get back don't sometimes get clogged or jammed up, in which case parsing the final file would be the way to go about it.

Sorry if that's more confusing than helpful. If you have anything more specific, please post and I'd be glad to look.

Best wishes,

Mike
 
Old 05-05-2008, 09:35 PM   #3
bboyz
LQ Newbie
 
Registered: May 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks eggixyz for the reply.

There are two files ciscocmd and mybatch.
The ciscocmd funnels the output to the batch file, and the batch file conducts further processing on the stream.

I'm not sure how to split the stream based on the commands.

For instance I'd like to maybe save the entire stream to a variable or array and then say get and print all of the information (each line) from "show cdp nei" to the line before "show vtp status", and save to $CISCOHOST-show_cdp_nei.txt.

How would I parse the information in the variable or array? I just can't seem to figure it out.
 
Old 05-05-2008, 11:58 PM   #4
eggixyz
Member
 
Registered: Apr 2008
Posts: 310

Rep: Reputation: 30
Hey again,

I see, so the batch isn't actually running the commands. I was misunderstanding you.

If you're just getting the output I would just parse it as it comes in. For example, this script could run by doing

./yourCiscoCMDscript|./theFollowingScript

Code:
#!/bin/bash

output_file=""

while read line
do
        if [ "$line" = "TEC-2950-2#show cdp nei" ]
        then
                output_file=showcpdnei.file
                echo $line >>$output_file
        elif [ "$line" = "TEC-2950-2#show vtp status" ]
        then
                output_file=showvtpstatus.file
                echo VTP status >>$output_file
        elif [ "$line" = "TEC-2950-2#show tech" ]
        then
                output_file=showvtptech.file
                echo SHOW tech >>$output_file
        else
                echo $line >>$output_file
        fi
done
Hope that helps and best wishes,

Mike
 
Old 05-06-2008, 09:06 PM   #5
bboyz
LQ Newbie
 
Registered: May 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Added commands to batch file. set -x to see output.

Processing... TEC-2950-2
+ output_file=
+ read line
+ echo $'\r'
+ '[' $'\r' = 'TEC-2950-2#show cdp nei' ']'
+ '[' $'\r' = 'TEC-2950-2#show vtp status' ']'
+ '[' $'\r' = 'TEC-2950-2#show tech' ']'
+ read line
' echo 'TEC-2950-2#show cdp nei
' = 'TEC-2950-2#show cdp nei' ']'
' = 'TEC-2950-2#show vtp status' ']'
' = 'TEC-2950-2#show tech' ']'
+ read line
' echo 'Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge
' = 'TEC-2950-2#show cdp nei' ']'er, T - Trans Bridge, B - Source Route Bridge
' = 'TEC-2950-2#show vtp status' ']' T - Trans Bridge, B - Source Route Bridge
' = 'TEC-2950-2#show tech' ']'outer, T - Trans Bridge, B - Source Route Bridge
+ read line
' echo 'S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone
' = 'TEC-2950-2#show cdp nei' ']'IGMP, r - Repeater, P - Phone
' = 'TEC-2950-2#show vtp status' ']'P, r - Repeater, P - Phone
' = 'TEC-2950-2#show tech' ']' - IGMP, r - Repeater, P - Phone
+ read line
+ echo $'\r'
+ '[' $'\r' = 'TEC-2950-2#show cdp nei' ']'
+ '[' $'\r' = 'TEC-2950-2#show vtp status' ']'
+ '[' $'\r' = 'TEC-2950-2#show tech' ']'
+ read line
' echo 'Device ID Local Intrfce Holdtme Capability Platform Port ID
' = 'TEC-2950-2#show cdp nei' ']'rfce Holdtme Capability Platform Port ID
' = 'TEC-2950-2#show vtp status' ']'e Holdtme Capability Platform Port ID
' = 'TEC-2950-2#show tech' ']'Intrfce Holdtme Capability Platform Port ID
+ read line
+ echo $'TEC-4506-2.tec.csu.net\r'
+ '[' $'TEC-4506-2.tec.csu.net\r' = 'TEC-2950-2#show cdp nei' ']'
+ '[' $'TEC-4506-2.tec.csu.net\r' = 'TEC-2950-2#show vtp status' ']'
+ '[' $'TEC-4506-2.tec.csu.net\r' = 'TEC-2950-2#show tech' ']'
+ read line
' echo 'Gig 0/1 146 R S I WS-C4506 Gig 2/3
' = 'TEC-2950-2#show cdp nei' ']' R S I WS-C4506 Gig 2/3
' = 'TEC-2950-2#show vtp status' ']' R S I WS-C4506 Gig 2/3
' = 'TEC-2950-2#show tech' ']' R S I WS-C4506 Gig 2/3
+ read line
' echo 'TEC-2950-2#show vtp status
' = 'TEC-2950-2#show cdp nei' ']'
' = 'TEC-2950-2#show vtp status' ']'
' = 'TEC-2950-2#show tech' ']'tus
+ read line
' echo 'VTP Version : 2
' = 'TEC-2950-2#show cdp nei' ']' : 2
' = 'TEC-2950-2#show vtp status' ']' : 2
' = 'TEC-2950-2#show tech' ']' : 2
+ read line
' echo 'Configuration Revision : 89
' = 'TEC-2950-2#show cdp nei' ']' : 89
' = 'TEC-2950-2#show vtp status' ']' : 89
' = 'TEC-2950-2#show tech' ']' : 89
+ read line
' echo 'Maximum VLANs supported locally : 250
' = 'TEC-2950-2#show cdp nei' ']'cally : 250
' = 'TEC-2950-2#show vtp status' ']'ly : 250
' = 'TEC-2950-2#show tech' ']' locally : 250
+ read line
' echo 'Number of existing VLANs : 60
' = 'TEC-2950-2#show cdp nei' ']' : 60
' = 'TEC-2950-2#show vtp status' ']' : 60
' = 'TEC-2950-2#show tech' ']'s : 60
+ read line
' echo 'VTP Operating Mode : Client
' = 'TEC-2950-2#show cdp nei' ']' : Client
' = 'TEC-2950-2#show vtp status' ']' : Client
' = 'TEC-2950-2#show tech' ']' : Client
+ read line
' echo 'VTP Domain Name : TEC
' = 'TEC-2950-2#show cdp nei' ']' : TEC
' = 'TEC-2950-2#show vtp status' ']' : TEC
' = 'TEC-2950-2#show tech' ']' : TEC
+ read line
' echo 'VTP Pruning Mode : Enabled
' = 'TEC-2950-2#show cdp nei' ']' : Enabled
' = 'TEC-2950-2#show vtp status' ']' : Enabled
' = 'TEC-2950-2#show tech' ']' : Enabled
+ read line
' echo 'VTP V2 Mode : Enabled
' = 'TEC-2950-2#show cdp nei' ']' : Enabled
' = 'TEC-2950-2#show vtp status' ']' : Enabled
' = 'TEC-2950-2#show tech' ']' : Enabled
+ read line
' echo 'VTP Traps Generation : Enabled
' = 'TEC-2950-2#show cdp nei' ']' : Enabled
' = 'TEC-2950-2#show vtp status' ']' : Enabled
' = 'TEC-2950-2#show tech' ']' : Enabled
+ read line
' echo 'MD5 digest : 0xB0 0xF7 0xB6 0xBE 0x16 0x64 0x59 0xE6
' = 'TEC-2950-2#show cdp nei' ']' : 0xB0 0xF7 0xB6 0xBE 0x16 0x64 0x59 0xE6
' = 'TEC-2950-2#show vtp status' ']' : 0xB0 0xF7 0xB6 0xBE 0x16 0x64 0x59 0xE6
' = 'TEC-2950-2#show tech' ']' : 0xB0 0xF7 0xB6 0xBE 0x16 0x64 0x59 0xE6
+ read line
' echo 'Configuration last modified by 10.254.254.2 at 1-22-08 19:10:35
' = 'TEC-2950-2#show cdp nei' ']'d by 10.254.254.2 at 1-22-08 19:10:35
' = 'TEC-2950-2#show vtp status' ']'y 10.254.254.2 at 1-22-08 19:10:35
' = 'TEC-2950-2#show tech' ']'fied by 10.254.254.2 at 1-22-08 19:10:35
+ read line
+ echo $'TEC-2950-2#\r'
+ '[' $'TEC-2950-2#\r' = 'TEC-2950-2#show cdp nei' ']'
+ '[' $'TEC-2950-2#\r' = 'TEC-2950-2#show vtp status' ']'
+ '[' $'TEC-2950-2#\r' = 'TEC-2950-2#show tech' ']'
+ read line
+ echo TEC-2950-2#
+ '[' TEC-2950-2# = 'TEC-2950-2#show cdp nei' ']'
+ '[' TEC-2950-2# = 'TEC-2950-2#show vtp status' ']'
+ '[' TEC-2950-2# = 'TEC-2950-2#show tech' ']'
+ read line

The script ran but no output files where generated.

The ciscocmd uses expect to reach the devices, run the commands and gather the results. The results are then sent back line by line, exported via the cat command to a tmp file and then piped to the batch file.

I thought about using a while within a while or storing everything in a container and then enacting some type of awk against it?
 
Old 05-06-2008, 09:37 PM   #6
eggixyz
Member
 
Registered: Apr 2008
Posts: 310

Rep: Reputation: 30
Hey Again,

I was going to suggest making sure stderr got tied to stdout after the execution of the expect script that sends the output:

./script 2>&1

Which might help. Given your description, though, it looks like, wherever it gets set, that:

echo $'\r'

Either needs to be double escaped, or doesn't need to be escaped if you run the output from stdin.

perhaps:

echo $r

or

echo $'\\\r'

I think probably the first one.

I'll keep checking back.
 
Old 05-06-2008, 09:45 PM   #7
bboyz
LQ Newbie
 
Registered: May 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Found this which allows me to save the output into an array.

SHOWARRAY[$index]="$line"
index=$(($index+1))

Tested with an echo outside of the while loop.
echo ${SHOWARRAY[*]}

Rewarded with this output

+ echo $'\r' TEC-2950-2#show cdp $'nei\r' Capability Codes: R - Router, T - Trans Bridge, B - Source Route $'Bridge\r' S - Switch, H - Host, I - IGMP, r - Repeater, P - $'Phone\r' $'\r' Device ID Local Intrfce Holdtme Capability Platform Port $'ID\r' $'TEC-4506-2.tec.csu.net\r' Gig 0/1 166 R S I WS-C4506 Gig $'2/3\r' TEC-2950-2#show vtp $'status\r' VTP Version : $'2\r' Configuration Revision : $'89\r' Maximum VLANs supported locally : $'250\r' Number of existing VLANs : $'60\r' VTP Operating Mode : $'Client\r' VTP Domain Name : $'TEC\r' VTP Pruning Mode : $'Enabled\r' VTP V2 Mode : $'Enabled\r' VTP Traps Generation : $'Enabled\r' MD5 digest : 0xB0 0xF7 0xB6 0xBE 0x16 0x64 0x59 0xE6 $'\r' Configuration last modified by 10.254.254.2 at 1-22-08 $'19:10:35\r' $'TEC-2950-2#\r' TEC-2950-2#
 
Old 05-07-2008, 02:08 AM   #8
eggixyz
Member
 
Registered: Apr 2008
Posts: 310

Rep: Reputation: 30
Yeah, looks like the same issue. The $\r should probably be $r. Try it without the $.

If you echoed through that array and it all came out on one line, the array probably only has one value, which is everything (normally, the \r would be a line separate, but you have $\r

It almost has to be getting goofed before you parse it because if I put your output in a file, and the run the batch I put up before, I get this:

-bash-3.2$ cat X|./Y
-bash-3.2$ ls -tr
showvtpstatus.file showcpdnei.file
-bash-3.2$ cat show*
TEC-2950-2#show cdp nei
Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge
S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone

Device ID Local Interface Holdtme Capability Platform Port ID
TEC-XXXX-2
Gig 0/1 129 R S I WS-XXXXX Gig 2/3
TEC-2950-2#show cdp nei
Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge
S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone

Device ID Local Interface Holdtme Capability Platform Port ID
TEC-XXXX-2
Gig 0/1 129 R S I WS-XXXXX Gig 2/3
VTP status
VTP Version : 2
Configuration Revision : 89
Maximum VLANs supported locally : 250
Number of existing VLANs : 60
VTP Operating Mode :
VTP Domain Name :
show tech binger
VTP Pruning Mode :
VTP V2 Mode :
VTP Traps Generation :
MD5 digest :
Configuration last modified by x.x.x.x at 1-22-08 19:10:35
TEC-XXXX-2#

VTP status
VTP Version : 2
Configuration Revision : 89
Maximum VLANs supported locally : 250
Number of existing VLANs : 60
VTP Operating Mode :
VTP Domain Name :
VTP Pruning Mode :
VTP V2 Mode :
VTP Traps Generation :
MD5 digest :
Configuration last modified by x.x.x.x at 1-22-08 19:10:35
TEC-XXXX-2#

, Mike
 
  


Reply

Tags
parse, parsing, scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Encoding separate audio channels to separate files omnio Linux - Software 0 06-01-2007 07:46 AM
Looking for a tool to auto crop and separate images in to separate files.. mlsfit Linux - Software 2 08-06-2006 03:13 PM
parsing standard input with bash arosales Programming 3 07-16-2005 10:42 AM
input single integer value, output the value as separate digits hubabuba Programming 2 03-02-2005 10:42 AM
parsing a user input string daphne19 Programming 1 04-22-2004 07:40 AM


All times are GMT -5. The time now is 03:53 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration