解析命令的输出为可变LIVE(网络流量监控) [英] Parse Output of Command into Variable LIVE (Network Traffic Monitoring)
问题描述
我写在bash网络监控脚本。我使用的基本命令是 ettercap -T -M ARP -i EN1 // //
。然后,我管的egrep --color'主持人:| 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后,一个空行
上市。我试图形成他们到一个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
)
要得到一个HTML页面的标题,我用命令 wget的-qO-的https://url.goes/here| perl的-l -0777 -ne'打印如果$ 1 /<标题*方式>?\\ S * \\ S *&LT(*?); \\ /标题/ SI
。如果选择的是不具有一个标题,诸如图像,没有标题是细
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.
任何帮助是极大AP preciated,和对不起,如果我写的是难以阅读,请随时提问。
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\n";
print "URL: $url\n";
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
这篇关于解析命令的输出为可变LIVE(网络流量监控)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!