本文介绍了将命令的输出解析为变量 LIVE(网络流量监控)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 bash 编写网络监控脚本.我使用的基本命令是 ettercap -T -M ARP -i en1////.然后我将 egrep --color 'Host:|GET' 输入其中.

I am writing a network monitoring script in bash. The base command I am using is ettercap -T -M ARP -i en1 // //. Then I pipe egrep --color 'Host:|GET' into it.

我得到的示例输出如下所示:

A sample output I am getting looks like this:

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

我想要的输出是这样的:

My desired output is this:

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

Things to notice: HTTP/1.1. and the . at the end of the host are gone. They also are formed into one URL and there is a blank line after each Title/URL listing. I attempted forming them into one URL by parsing the commands output into a variable with

var=`sudo ettercap -T -M ARP -i en1 // // | egrep --color 'Host:|GET'` | echo $var

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

but obviously that doesn't work because the input to the variable is a command the isn't done until the user requests a stop (CTRL + C).

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

To get the title of an HTML page, I use the command wget -qO- 'https://url.goes/here' | perl -l -0777 -ne 'print $1 if /<title.*?>s*(.*?)s*</title/si'. If it is something that doesn't have a title, such as an image, no title is fine.

非常感谢任何帮助,如果我写的内容难以阅读,请随时提问.

Any help is greatly appreciated, and sorry if what I wrote is hard to read, feel free to ask questions.

推荐答案

试试这个:

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: $title
";
        print "URL: $url
";
        print "
";

        $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

这篇关于将命令的输出解析为变量 LIVE(网络流量监控)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 18:42