LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Parsing input into separate files (https://www.linuxquestions.org/questions/linux-newbie-8/parsing-input-into-separate-files-640105/)

bboyz 05-05-2008 02:19 PM

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?

eggixyz 05-05-2008 03:23 PM

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

bboyz 05-05-2008 09:35 PM

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.

eggixyz 05-05-2008 11:58 PM

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 :)

bboyz 05-06-2008 09:06 PM

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?

eggixyz 05-06-2008 09:37 PM

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.

bboyz 05-06-2008 09:45 PM

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#

eggixyz 05-07-2008 02:08 AM

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


All times are GMT -5. The time now is 11:55 AM.