Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 18 Feb 2007 21:54:16 +0800
From:      LI Xin <delphij@delphij.net>
To:        freebsd-net@freebsd.org
Subject:   [PATCH] Feature request: exit netstat(1) after user specified outputs
Message-ID:  <45D85A88.4040402@delphij.net>

next in thread | raw e-mail | index | archive | help
This is an OpenPGP/MIME signed message (RFC 2440 and 3156)
--------------enig1579E7B082DB1FE636B8A914
Content-Type: multipart/mixed; boundary="------------090407050304000506020605"

This is a multi-part message in MIME format.
--------------090407050304000506020605
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hi,

This is a patch that I have backported to -CURRENT, which is originally
made against RELENG_6_2.

I have received several feature requests from our local community which
they wanted to have a top(1)'s '-d' alike feature for netstat, so they
will be able to use netstat(1) as a handy brick when building monitoring
scripts.

So here is the patchset that adds a '-q' option, which accepts a
numerical argument meaning how many outputs are going out before we exit.=


Comments?  (I think 'q' is arguable, but netstat(1) has too many options =
:-)

Cheers,
--=20
Xin LI <delphij@delphij.net>	http://www.delphij.net/
FreeBSD - The Power to Serve!

--------------090407050304000506020605
Content-Type: text/plain; x-mac-type="0"; x-mac-creator="0";
	name="patch-netstat"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline;
 filename="patch-netstat"

Index: if.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/ncvs/src/usr.bin/netstat/if.c,v
retrieving revision 1.66
diff -u -p -r1.66 if.c
--- if.c	8 Nov 2006 15:15:05 -0000	1.66
+++ if.c	18 Feb 2007 13:37:32 -0000
@@ -676,6 +676,8 @@ loop:
 	(void)alarm(interval1);
 	line++;
 	first =3D 0;
+	if (qflag && (qval-- =3D=3D 0))
+		exit(0);
 	if (line =3D=3D 21)
 		goto banner;
 	else
Index: main.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/ncvs/src/usr.bin/netstat/main.c,v
retrieving revision 1.83
diff -u -p -r1.83 main.c
--- main.c	28 Jul 2006 16:16:40 -0000	1.83
+++ main.c	18 Feb 2007 13:29:11 -0000
@@ -295,6 +295,8 @@ int	mflag;		/* show memory stats */
 int	numeric_addr;	/* show addresses numerically */
 int	numeric_port;	/* show ports numerically */
 static int pflag;	/* show given protocol */
+int	qflag;		/* whether to quit after specified outputs */
+int	qval;		/* how much outputs before we exit */
 int	rflag;		/* show routing tables (or routing stats) */
 int	sflag;		/* show protocol statistics */
 int	tflag;		/* show i/f watchdog timers */
@@ -316,7 +318,7 @@ main(int argc, char *argv[])
=20
 	af =3D AF_UNSPEC;
=20
-	while ((ch =3D getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:rSstuWw:z")) !=
=3D -1)
+	while ((ch =3D getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:q:rSstuWw:z"))=
 !=3D -1)
 		switch(ch) {
 		case 'A':
 			Aflag =3D 1;
@@ -400,6 +402,10 @@ main(int argc, char *argv[])
 			}
 			pflag =3D 1;
 			break;
+		case 'q':
+			qval =3D atoi(optarg);
+			qflag =3D 1;
+			break;
 		case 'r':
 			rflag =3D 1;
 			break;
Index: netstat.1
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/ncvs/src/usr.bin/netstat/netstat.1,v
retrieving revision 1.57
diff -u -p -r1.57 netstat.1
--- netstat.1	29 Sep 2006 15:20:46 -0000	1.57
+++ netstat.1	18 Feb 2007 13:40:17 -0000
@@ -130,6 +130,7 @@ is also present, print interface names u
 .Nm
 .Fl w Ar wait
 .Op Fl I Ar interface
+.Op Fl q Ar howmany
 .Op Fl d
 .Op Fl M Ar core
 .Op Fl N Ar system
@@ -143,6 +144,11 @@ traffic on all configured network interf
 or a single
 .Ar interface .
 If
+.Fl q
+is also present, exit after
+.Ar howmany
+outputs.
+If
 .Fl d
 is also present, show the number of dropped packets.
 .It Xo
Index: netstat.h
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/ncvs/src/usr.bin/netstat/netstat.h,v
retrieving revision 1.48
diff -u -p -r1.48 netstat.h
--- netstat.h	28 Jul 2006 16:09:18 -0000	1.48
+++ netstat.h	18 Feb 2007 13:19:41 -0000
@@ -47,6 +47,8 @@ extern int	Lflag;	/* show size of listen
 extern int	mflag;	/* show memory stats */
 extern int	numeric_addr;	/* show addresses numerically */
 extern int	numeric_port;	/* show ports numerically */
+extern int	qflag;	/* whether to quit after specified outputs */
+extern int	qval;	/* how much outputs before we exit */
 extern int	rflag;	/* show routing tables (or routing stats) */
 extern int	sflag;	/* show protocol statistics */
 extern int	tflag;	/* show i/f watchdog timers */

--------------090407050304000506020605--

--------------enig1579E7B082DB1FE636B8A914
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF2FqIOfuToMruuMARA7PNAJ91skYOhGG/ogn9vUbZ3jtXZCatZgCdFWAi
hiBKc/YafSMUMyepEfdNQUA=
=1oWX
-----END PGP SIGNATURE-----

--------------enig1579E7B082DB1FE636B8A914--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?45D85A88.4040402>