Skip to content

Commit

Permalink
Merge pull request #149 from dilinger/examples
Browse files Browse the repository at this point in the history
Update perl scripts for the past few decades of progress
  • Loading branch information
masatake committed Jan 26, 2021
2 parents c097e42 + 48c2727 commit 70c0078
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 346 deletions.
27 changes: 9 additions & 18 deletions scripts/00MANIFEST
Original file line number Diff line number Diff line change
@@ -1,51 +1,42 @@
The scripts in this subdirectory give examples of using lsof's
field output.

big_brother.perl5 Perl 5 script, contributed by Lionel Cons
big_brother.pl Perl 5 script, contributed by Lionel Cons
<Lionel.Cons@cern.ch>, that watches for new
network connections.

count_pf.perl Perl 4 or 5 script that runs lsof in repeat
mode, gathering process, file, TCP, and UDP
counts

count_pf.perl5 Perl 5 script that runs lsof in repeat mode,
count_pf.pl Perl 5 script that runs lsof in repeat mode,
gathering process, file, TCP, and UDP counts

This script uses NUL terminated lsof field
output.

identd.perl5 Perl 5 script, contributed by Kapil Chowksey
identd.pl Perl 5 script, contributed by Kapil Chowksey
<kchowksey@hss.hns.com> that implements an
identd server. (Thanks, Kapil!)

idrlogin.perl Perl 4 script that identifies the shell and
network source address of users who have logged
on from remote locations via rlogin, ssh, or
telnet

idrlogin.perl5 Perl 5 script that identifies the shell and
idrlogin.pl Perl 5 script that identifies the shell and
network source address of users who have logged
on from remote locations via rlogin, ssh, or
telnet

list_NULf.perl5 Perl 5 script that prints lsof's NUL terminated
list_NULf.pl Perl 5 script that prints lsof's NUL terminated
field output

list_fields.awk AWK script that prints lsof's field output

list_fields.perl Perl 4 or 5 script that prints lsof's field
list_fields.pl Perl 4 or 5 script that prints lsof's field
output

shared.perl5 Perl 5 script that uses +ffn output to produce
shared.pl Perl 5 script that uses +ffn output to produce
a list of file descriptors or files shared by
processes.

sort_res.perl5 Perl 5 script, contributed by Fabian Frederick
sort_res.pl Perl 5 script, contributed by Fabian Frederick
<fabian.frederick@gmx.fr>, to display top resource
usage.

watch_a_file.perl Perl 4 or 5 script that watches the use of a
watch_a_file.pl Perl 4 or 5 script that watches the use of a
named file

xusers.awk an AWK (actually NAWK) script, written by
Expand Down
16 changes: 2 additions & 14 deletions scripts/00README
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ If you want to do field output post-processing in a C program, take
a look at the test suite C library in ../tests/LTlib.c. You may
be able to adapt it to your needs.

The scripts are written in AWK, Perl 4 (4.036), and Perl 5 (5.001e
through 5.006). AWK scripts have a suffix of ``.awk''; Perl 4
(which will work under Perl 5) scripts have a ``.perl4'' suffix;
and Perl 5 scripts, ``.perl''.

Supply AWK scripts to your AWK interpreter with its -f option. Supply
lsof field output via a pipe -- e.g.,

Expand All @@ -28,20 +23,13 @@ specify the location of Perl -- i.e., the first line begins with
``#!'' and the path to the Perl interpreter follows. If your system
supports the command interpreter feature, but your Perl interpreters
have different paths to them, just change the interpreter lines in
the scripts. These scripts assume:

Path to: Is:
======= ==

Perl 4 /usr/local/bin/perl4

Perl 5 /usr/local/bin/perl
the scripts. These scripts assume Perl lives at /usr/bin/perl.

If your system doesn't support the command interpreter feature,
you'll have to supply the scripts to your Perl interpreter on its
command line -- e.g.,

lsof -F | /<path_to_your_perl_4> list_fields.perl
lsof -F | /<path_to_your_perl> list_fields.pl

The Perl scripts attempt to establish a path to lsof, putting their
result in the $LSOF variable. Assuming you'll run them from the
Expand Down
4 changes: 2 additions & 2 deletions scripts/big_brother.perl5 → scripts/big_brother.pl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/local/bin/perl -w
#!/usr/bin/perl -w
#+##############################################################################
# #
# File: big_brother.perl #
# File: big_brother.pl #
# #
# Description: check the network sockets with lsof to detect new connections #
# #
Expand Down
68 changes: 0 additions & 68 deletions scripts/count_pf.perl

This file was deleted.

4 changes: 2 additions & 2 deletions scripts/count_pf.perl5 → scripts/count_pf.pl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/local/bin/perl
#!/usr/bin/perl
#
# count_pf.perl5 -- run lsof in repeat mode and count processes and
# count_pf.pl -- run lsof in repeat mode and count processes and
# files

sub interrupt { print "\n"; exit 0; }
Expand Down
35 changes: 17 additions & 18 deletions scripts/identd.perl5 → scripts/identd.pl
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
#!/usr/local/bin/perl
#!/usr/bin/perl
###################################################################
# identd.perl5 : An implementation of RFC 1413 Ident Server
# identd.pl : An implementation of RFC 1413 Ident Server
# using Vic Abell's lsof.
#
# - Started from inetd with 'nowait' option. This entry in
# /etc/inetd.conf will suffice :
#
# ident stream tcp nowait root /usr/local/bin/identd.perl5 -t200
# ident stream tcp nowait root /usr/local/bin/identd.pl -t200
#
# - Multiple instances of the server are not a performance penalty
# since they shall use lsof's cacheing mechanism. (compare with
# Peter Eriksson's pidentd)
# - assumes 'lsof' binary in /usr/local/sbin
# - Command line arguments :
# -t TIMEOUT Number of seconds to wait for a query before aborting.
# Default is 120.
#
# Kapil Chowksey <kchowksey@hss.hns.com>
# Nicholas Bamber <nicholas@periapt.co.uk>
###################################################################

use strict;
use Socket;
require 'getopts.pl';
use Getopt::Long;

# Set path to lsof.

my $LSOF;
if (($LSOF = &isexec("../lsof")) eq "") { # Try .. first
if (($LSOF = &isexec("lsof")) eq "") { # Then try . and $PATH
print "can't execute $LSOF\n"; exit 1
Expand All @@ -34,18 +35,16 @@
close(STDERR);
open(STDERR, ">/dev/null");

$Timeout = "120";
my $Timeout = "120";

&Getopts('t:');
if ($opt_t) {
$Timeout = $opt_t;
}
GetOptions('timeout=i'=>\$Timeout);

($port, $iaddr) = sockaddr_in(getpeername(STDIN));
$peer_addr = inet_ntoa($iaddr);
my ($port, $iaddr) = sockaddr_in(getpeername(STDIN));
my $peer_addr = inet_ntoa($iaddr);
my $query;

# read ident-query from socket (STDIN) with a timeout.
$timeout = int($Timeout);
my $timeout = int($Timeout);
eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm $timeout;
Expand All @@ -61,8 +60,8 @@
# remove all white-spaces from query
$query =~ s/\s//g;

$serv_port = "";
$cli_port = "";
my $serv_port = "";
my $cli_port = "";
($serv_port,$cli_port) = split(/,/,$query);

if ($serv_port =~ /^[0-9]+$/) {
Expand All @@ -87,8 +86,8 @@

open(LSOFP,"$LSOF -nPDi -T -FLn -iTCP@".$peer_addr.":".$cli_port."|");

$user = "UNKNOWN";
while ($a_line = <LSOFP>) {
my $user = "UNKNOWN";
while (my $a_line = <LSOFP>) {
# extract user name.
if ($a_line =~ /^L.*/) {
($user) = ($a_line =~ /^L(.*)/);
Expand Down

0 comments on commit 70c0078

Please sign in to comment.