开发者

Modify perl script text to csv

Hi I wrote a perl script to output a text file with port scans to excel now I need to format the text file so that when it prints to excel it's in csv format. Like this for example Server, port, protocol, state 69.25.194.14, 25, tcp, http

Here is my code that I hope you guys could modify, The code so far outputs the 开发者_开发知识库txt file to excel which is good now I just need it modified so that it can display it in csv format within the text file and output the txt file to excel :

$input = `Cat /cygdrive/c/Windows/System32/test11.txt | grep -v 'SYN Stealth' |grep -v'Discovered'`;
chomp input;
$output =" /cygdrive/c/Users/bpaul/Desktop/194.csv ";
if (! -e "$output")
{
`touch $output`;
}
open (OUTPUTFILE, ">$output") || die "Can't Open file $output";
print OUTPUTFILE "$input\n";
close (OUTPUTFILE);

Here is a piece of my txt file

Nmap scan report for 69.25.194.2 Host is up (0.072s latency). 
Not shown: 9992 filtered ports PORT STATE SERVICE 
25/tcp open smtp
80/tcp open http
82/tcp open xfer
443/tcp open
https 4443/tcp closed
pharos 5666/tcp closed
nrpe 8080/tcp closed
http-proxy 9443/tcp closed tungsten-https

Could anybody please modify my code. Thanks!


Once you figure what you want in what columns (I can't tell from your question), I'd look at Text::CSV::print for output.


This is close but the input needs more filtering.

#!/usr/bin/env perl

use strict;
use warnings;

use Text::CSV;
my $csv = Text::CSV->new();

my $in_file = '/cygdrive/c/Windows/System32/test11.txt';
open my $in_fh, '<', $in_file or die "could not open $in_file: $!\n";

my @rows = ();
while( my $line = <$in_fh> ){
  chomp $line;
  next if $line =~ m{ SYN Stealth }msx;
  next if $line =~ m{ Discovered }msx;

  push @rows, [ split m{ \s+ }msx, $line ];
}
close $in_fh or die "could not close $in_file: $!\n";

$csv->eol( "\n" );

my $out_file = '/cygdrive/c/Users/bpaul/Desktop/194.csv';
open my $out_fh, '>', $out_file or die "could not open $out_file: $!\n";

for my $row ( @rows ){
  $csv->print( $out_fh, $row ) or die "could not print to $out_file: $!\n";;
}
close $out_fh or die "could not close $out_file: $!\n";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜