File: grep6.pl

package info (click to toggle)
thc-ipv6 3.6-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,116 kB
  • sloc: ansic: 52,618; sh: 529; makefile: 77; perl: 34
file content (43 lines) | stat: -rwxr-xr-x 912 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/perl
# basic code by Eric Vyncke
use Socket;
use Socket6;

$fd = STDIN;
$option = shift;
$count = 0;
$ln = 0;

if ($option eq "" || $option eq "-h") {
  print "Syntax: grep6.pl [-n] ipv6-address [logfile]\n";
  print "Option: -n print with line count\n";
  exit(0);
}

if ($option eq "-n") {
  $count = 1;
  $option = shift;
}

my (@words, $word, $binary_address, $address) ;
$address = inet_pton AF_INET6, $option ;
if (! $address) { die "Wrong IPv6 address passed as argument" ; }

$option2 = shift;
if ($option2 ne "") {
  open $fd, "< $option2"		or die "$option2";
}

## go through the file one line at a time
while (my $line = <$fd>) {
  $ln++;
  @words = split /[ ,"'.\\\t\n\r\(\)\[\]]/, $line ;
  foreach $word (@words) {
    $binary_address = inet_pton AF_INET6, $word ;
    if ($binary_address eq $address) {
      print "$ln: "	if ($count == 1);
      print $line ;
      next ;
    }
  }
}