bks has asked for the wisdom of the Perl Monks concerning the following question:
Hi All
My requirement is to connect to multiple network devices(cisco,juniper) and pull some data to csv file and further filtering it (column wise) and putting it in different csv.
However i was able to extract single column and display it onto console, but as said i wanted multiple columns to be filtered.
Sample command file output and csv file
Command output
Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd
2.2.2.2 4 100 273 274 2 0 0 04:30:05 0
3.3.3.3 4 100 273 274 2 0 0 04:30:05 0
4.4.4.4 4 100 273 273 2 0 0 04:30:00 0
CSV FILE
Neighbor,V,AS,MsgRcvd,MsgSent,TblVer,InQ,OutQ,Up/Down,State/PfxRcd
2.2.2.2,4,100,273,274,2,0,0,04:30:05,0
3.3.3.3,4,100,273,274,2,0,0,04:30:05,0
4.4.4.4,4,100,273,273,2,0,0,04:30:00,0
Required output
Host/Device IP,Neighbor,Up/Down
192.168.10.3,2.2.2.2,4:30:05
192.168.10.3,3.3.3.3,4:30:05
192.168.10.3,4.4.4.4,4:30:00
I would be getting similar output from different devices and i would like append the output to same csv file
I tried installing text::csv_xs, but unfortunately i wasnt able extract *.tgz as it gave some checksum error. Please guide me or point me in right direction about how to install modules that are in *.tgz format
Note: Running perl on solaris 10
Please post some sample code to achieve my requirement that would really be of great help
My code
#!/usr/bin/perl use Net::Telnet::Cisco; use Text::CSV; ######### Command line argument while executing the script ########### +#### if ($#ARGV < 0) { print "Please run the script with correct arguments\n\n"; print "Usage Details:perl bgp_summary_xls.pl <Device IP>\n"; print "Ex: perl bgp_summary_xls.pl 192.168.3.10\n"; exit; } $ip = $ARGV[0]; print "$ip\n"; #Connecting to Cisco Device my $session = Net::Telnet::Cisco->new(Host => $ip); $session->login('cisco', 'cisco'); # Execute a command my @output = $session->cmd('show ip bgp summary | begin Neighbor'); #my $file = /AutomationScripts/Korea_health_checks/test; open my $fh, ">test" or die "Cant open file died unexpectedly"; foreach (@output) { #@output = split(/\t/, $_); print $fh $_; } close $fh; ###### To write to csv ###### open (TABFILE, 'test') or die "test: $!"; open (CSV,">test.csv") or die "test csv: $!"; my @fld; while (<TABFILE>) { chomp; my @fld = split('\t', $_); #print "Before @fld"; for (@fld) { s/\+//g; s/ +/,/g; } print CSV "@fld\n"; } close CSV; ############### To extract column ################ my $csv = Text::CSV->new ({ binary => 1 }); open (CSV, "<test.csv") or die "Can't open $file: $!\n"; while (<CSV>) { if ($csv->parse($_)) { my @column = $csv->fields(); print "$column[8]\n"; } else { my $err = $csv->error_input; print "ERROR: Failed to parse line: $err"; } } close CSV;
|
---|