#!/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 \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 () { 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, ") { 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;