解析命令输出为变量LIVE(网络流量监控)



我正在用bash编写一个网络监控脚本。我使用的基本命令是ettercap -T -M ARP -i en1 // //。然后我把egrep --color 'Host:|GET'放进去。

我得到的一个示例输出看起来像这样:

GET /images/srpr/logo11w.png HTTP/1.1.
Host: www.google.com.
GET /en-us/us/products HTTP/1.1.
Host: www.caselogic.com.

我想要的输出是:

Title: logo11w.png
URL: www.google.com/images/srpr/logo11w.png HTTP/1.1.
Title: Products - Case Logic
URL: www.caselogic.com/en-us/us/products

注意事项:主机末端的HTTP/1.1..消失。它们也组成一个URL,每个Title/URL清单后面都有一个空白行。我试图通过将命令输出解析为带有

的变量来将它们组合成一个URL。
var=`sudo ettercap -T -M ARP -i en1 // // | egrep --color 'Host:|GET'` | echo $var

,但显然这不起作用,因为变量的输入是一个命令,直到用户请求停止(CTRL + C)才完成。

要获取HTML页面的标题,我使用命令wget -qO- 'https://url.goes/here' | perl -l -0777 -ne 'print $1 if /<title.*?>s*(.*?)s*</title/si'。如果是没有标题的东西,比如图片,没有标题就可以。

任何帮助都是非常感谢的,如果我写的东西很难读,很抱歉,请随时提问。

试试这个:

title_host.pl

#!/usr/bin/env perl
use warnings;
use strict;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
my ($get,$host,$title);
while (<>) {
    if (m|^GET (S+) |) {
        $get = $1;
    } elsif ( m|^Host: (S+).| ) {
        $host = $1;
    } else {
        # Unrecognized line...reset
        $get = $host = $title = '';
    }
    if ($get and $host) {
        my ($title) = $get =~ m|^.*/(.+?)$|; # default title
        my $url = 'http://' . $host . $get;
        $mech->get($url);
        if ($mech->success) {
            # HTML may have title, images will not
            $title = $mech->title() || $title;
        }
        print "Title: $titlen";
        print "URL: $urln";
        print "n";
        $get = $host = $title = '';
    }
}
输入

GET /images/srpr/logo11w.png HTTP/1.1.
Host: www.google.com.
GET /en-us/us/products HTTP/1.1.
Host: www.caselogic.com.

现在只需将您的输入管道到perl脚本中:

cat input | perl title_host.pl
输出:

Title: logo11w.png
URL: http://www.google.com/images/srpr/logo11w.png
Title: Products - Case Logic
URL: https://www.caselogic.com/en-us/us/products