Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Read() -Multiple Files-

by Anonymous Monk
on Jan 26, 2012 at 16:13 UTC ( #950153=note: print w/replies, xml ) Need Help??


in reply to Read() -Multiple Files-

Simple way

#!/usr/bin/perl -- use strict; use warnings; Main( @ARGV ); exit( 0 ); sub Main { my( $directory ) = @_; my $dbh = ...; for my $file ( GetFiles( $directory ) ){ LogFromPcap( $file, $dbh ); } $dbh->disconnect; } sub GetFiles { use Cwd(); my $cwd = Cwd::cwd(); chdir $directory or die $!; my @files = glob '*.pcap'; chdir $cwd; return @files; } sub LogFromPcap { my( $file, $dbh ) = @_; ... }

See File::Glob and File::Find::Rule (and search perlmonks for examples of each)

For getopt example see Re: help with loop

Replies are listed 'Best First'.
Re^2: Read() -Multiple Files-
by jboy4 (Initiate) on Jan 26, 2012 at 17:20 UTC

    Im just not sure how to make this work with my code... If you could help me out that would be much appreciated.

      Im just not sure how to make this work with my code...

      Can you identify any of your code in what I posted?

        Yes your variables $file,$dbh and extension pcap.

        Here is kinda what i was getting to... Just not sure if this is all correct, when i get a chance ill test it.

        #!/usr/bin/perl -- use strict; use warnings; Main( @ARGV ); exit( 0 ); sub Main { my( $directory ) = @_; my $dbh = DBI->connect('DBI:mysql:test', 'root', 'nstar' ) || die "Could not connect to +database: $DBI::errstr" +; for my $file ( GetFiles( $directory ) ){ LogFromPcap( $file, $dbh ); } $dbh->disconnect; } sub GetFiles { use Cwd(); my $cwd = Cwd::cwd(); chdir $directory or die $!; my @files = glob '*.pcap'; chdir $cwd; return @files; } sub LogFromPcap { my( $file, $dbh ) = @_; $log->read('$file'); #INFO from PCAP file foreach my $index ($log->indexes) { my ($length_orig, $length_incl, $drops, $secs, $msecs) = $log->hea +der ($index); my $data = $log->data($index); my $eth_obj = NetPacket::Ethernet->decode($data); next unless $eth_obj->{type} == NetPacket::Ethernet::ETH_TYPE_IP +; my $ip_obj = NetPacket::IP->decode($eth_obj->{data}); next unless $ip_obj->{proto} == NetPacket::IP::IP_PROTO_TCP; my $tcp_obj = NetPacket::TCP->decode($ip_obj->{data}); #get date time stamp of packet my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me( +$secs + $msecs/1000); $mon+=1; my $time = sprintf("%02d-%02d %02d:%02d:%02d", $mon, $mday, $hour, $min, $sec); #Info in Table $dbh->do( "INSERT INTO test2 (Date,Source,Destination,Packets +,Port) values ( '$time', '$ip_obj->{src_ip}', '$ip_obj->{dest_ip}', '$ip_obj->{len}', '$tcp_obj->{dest_port}')"); } }

        I actually went a different route using opendir and readdir. I got it working after a little trial and error. Thanks for your input as well tho.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://950153]
help
Chatterbox?
[Eily]: someday FF will start posting back messages where you tell to ignore the previous message
[Eily]: and Hilarity Ensues
Eily says, while failing to write a link properly

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2017-03-27 16:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (320 votes). Check out past polls.