Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在ngrep周围编写一个突出显示匹配项的包装器?_Python_Perl_Unix_Networking - Fatal编程技术网

Python 如何在ngrep周围编写一个突出显示匹配项的包装器?

Python 如何在ngrep周围编写一个突出显示匹配项的包装器?,python,perl,unix,networking,Python,Perl,Unix,Networking,我刚刚了解了一个很酷的程序,它可以让你轻松地嗅探与特定字符串匹配的数据包 唯一的问题是,很难在大量的产出中看到匹配。我想编写一个包装器脚本来突出显示这些匹配项——它可以使用ANSI转义序列: echo -e 'This is \e[31mRED\e[0m.' 我对Perl最为熟悉,但我对Python或任何其他语言的解决方案非常满意。最简单的方法是: while (<STDIN>) { s/$keyword/\e[31m$keyword\e[0m/g; print; }

我刚刚了解了一个很酷的程序,它可以让你轻松地嗅探与特定字符串匹配的数据包

唯一的问题是,很难在大量的产出中看到匹配。我想编写一个包装器脚本来突出显示这些匹配项——它可以使用ANSI转义序列:

echo -e 'This is \e[31mRED\e[0m.'
我对Perl最为熟悉,但我对Python或任何其他语言的解决方案非常满意。最简单的方法是:

while (<STDIN>) {
   s/$keyword/\e[31m$keyword\e[0m/g;
   print;
}
while(){
s/$keyword/\e[3100万美元keyword\e[0m/g;
印刷品;
}
然而,这不是一个好的解决方案,因为每当ngrep接收到不匹配的数据包时,它都会打印出不带换行符的哈希标记,并且上面的代码将抑制这些哈希标记的打印,直到脚本看到换行符为止


有没有什么方法可以在不抑制哈希标记即时出现的情况下进行高亮显示?

如果您有答案,应该不会太难


(基本上,一次读取一个字符,如果它是散列,则打印它。如果它不是散列,则保存该字符以便稍后打印。)

啊,忘了它吧。这太痛苦了。将源代码获取到ngrep并使其将散列标记打印到stderr要容易得多:

--- ngrep.c     2006-11-28 05:38:43.000000000 -0800
+++ ngrep.c.new 2008-10-17 16:28:29.000000000 -0700
@@ -687,8 +687,7 @@
     }

     if (quiet < 1) {
-        printf("#");
-        fflush(stdout);
+      fprintf (stderr, "#");
     }

     switch (ip_proto) {                 
-ngrep.c 2006-11-2805:38:43.000000000-0800
+++纽约2008-10-17 16:28:29.000000000-0700
@@ -687,8 +687,7 @@
}
如果(安静<1){
-printf(“#”);
-fflush(stdout);
+fprintf(标准字符“#”);
}
交换机(ip_协议){
那么,过滤就是小菜一碟:

while (<CMD>) {
  s/($keyword)/\e[93m$1\e[0m/g;
  print;
}
while(){
s/($keyword)/\e[9300万美元1\e[00万美元/克;
印刷品;
}

这在python中很简单

#!/usr/bin/env python
import sys, re

keyword = 'RED'

while 1:
    c = sys.stdin.read(1)
    if not c:
        break
    if c in '#\n':
        sys.stdout.write(c)
    else:
        sys.stdout.write(
            (c+sys.stdin.readline()).replace(
            keyword, '\x1b[31m%s\x1b[0m\r' % keyword))

您还可以通过管道将输出传递出去。--passthru标志会有所帮助。

请参阅上的脚本。它是用Perl编写的,并使用CPAN Term::ANSIColor模块。

这似乎做到了这一点,至少比较了两个窗口,一个运行一个直接的ngrep(例如ngrep,无论什么),另一个被输送到以下程序中(使用ngrep whatever | ngrephl目标字符串)


为什么不直接用-q参数调用ngrep来消除散列标记呢?

好吧,是的——是我提出了这个问题!我正在试图找出实现这一点的最佳方法——我应该使用非阻塞I/O吗?getc()?我真的需要编写自己的readline()吗替换?示例代码在这里非常有用。回答很好!值得注意的是,这将把$keyword作为Perl regexp处理,如果这是您想要的,这是很好的。如果我们想把它作为文本字符串处理,那么匹配中的(\Q$keyword\E)将这样做,我的$keyword=quotemeta(shift)或die也会这样做。。。
#! /usr/bin/perl

use strict;
use warnings;

$| = 1; # autoflush on

my $keyword = shift or die "No pattern specified\n";
my $cache   = '';

while (read STDIN, my $ch, 1) {
    if ($ch eq '#') {
        $cache =~ s/($keyword)/\e[31m$1\e[0m/g;
        syswrite STDOUT, "$cache$ch";
        $cache = '';
    }
    else {
        $cache .= $ch;
    }
}