CSV 文件格式

发布于 2024-10-27 15:14:34 字数 1124 浏览 1 评论 0原文

我编写了一个 perl 脚本来输出一个文本文件,其中填充了我扫描到 Microsoft Excel 中的 IP 地址和端口。现在数据在 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

I wrote a perl script to output a text file filled with ip addresses and ports that i scanned into microsoft excel. Now that the data is in excel my boss wants me to organize the file in csv format such as

Server, port, protocol, random, random, random
ns1,    25,   tcp,      stuff,  stuff,  stuff

Can any one help me with this Please?

Code:

#!/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);

Here is a piece of my 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

So far my code took my txt file and outputted it to excel now I need to format the data like this:

Desired Output:

Server, port, protocol, random, random, random
ns1,    25,   tcp,      stuff,  stuff,  stuff

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肤浅与狂妄 2024-11-03 15:14:35

我假设您说 ns1 时指的是 69.25.194.2

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.csv

Text::CSV_XS

更新:替换为硬编码ns1 带有扫描机器的地址。

更新:将通用用法替换为 OP 将使用的内容。

I'm assuming you meant 69.25.194.2 when you said 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',
    ]);
}

Usage:

grep -v 'SYN Stealth' /cygdrive/c/Windows/System32/test11.txt | perl to_csv.pl > /cygdrive/c/Users/bpaul/Desktop/194.csv

Text::CSV_XS

Update: Replaced hardcoded ns1 with address of scanned machine.

Update: Replaced generic usage with what the OP would use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文