我编写了一个perl脚本来输出一个包含ip地址和端口的文本文件,然后扫描到microsoft中。既然数据以excel格式显示,我的老板希望我以csv格式组织文件,例如
Server, port, protocol, random, random, random
ns1, 25, tcp, stuff, stuff, stuff有人能帮我吗?
代码:
#!/usr/bin/perl
$input = `Cat /cygdrive/c/Windows/System32/test11.txt | grep -v 'SYN Stealth'`;
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);这是我的一份文件
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到目前为止,我的代码将我的txt文件输出到excel,现在我需要将数据格式化如下:
期望产出:
Server, port, protocol, random, random, random
ns1, 25, tcp, stuff, stuff, stuff发布于 2011-03-28 21:14:06
我想当你说69.25.194.2的时候,我猜你是指ns1。
use strict;
use warnings;
use Text::CSV_XS qw( );
my $csv = Text::CSV_XS->new({ binary => 1, eol => "\n" });
$csv->print(\*STDOUT, [qw(
Server
port
protocol
random
random
random
)]);
my $host = '[unknown]';
while (<>) {
$host = $1 if /Nmap scan report for (\S+)/;
my ($port, $protocol) = m{(\d+)/(\w+) (?:open|closed)/
or next;
$csv->print(\*STDOUT, [
$host,
$port,
$protocol,
'stuff',
'stuff',
'stuff',
]);
}用法:
grep -v 'SYN Stealth' /cygdrive/c/Windows/System32/test11.txt | perl to_csv.pl > /cygdrive/c/Users/bpaul/Desktop/194.csvXS
更新:用扫描机器的地址替换硬编码的ns1。
更新:将一般用法替换为OP将使用的内容。
https://stackoverflow.com/questions/5464835
复制相似问题