Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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;

In reply to Extracting multiple column from csv file and adding/appending onto new csv file with first columns already existing by bks

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 16:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found