[PATCH] Feature request: exit netstat(1) after user specified outputs

LI Xin delphij at delphij.net
Wed Feb 28 16:45:01 UTC 2007


Hi,

If no one objects this change, I will commit it?

LI Xin wrote:
> 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,
> 
> 
> ------------------------------------------------------------------------
> 
> Index: if.c
> ===================================================================
> 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 = 0;
> +	if (qflag && (qval-- == 0))
> +		exit(0);
>  	if (line == 21)
>  		goto banner;
>  	else
> Index: main.c
> ===================================================================
> 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[])
>  
>  	af = AF_UNSPEC;
>  
> -	while ((ch = getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:rSstuWw:z")) != -1)
> +	while ((ch = getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:q:rSstuWw:z")) != -1)
>  		switch(ch) {
>  		case 'A':
>  			Aflag = 1;
> @@ -400,6 +402,10 @@ main(int argc, char *argv[])
>  			}
>  			pflag = 1;
>  			break;
> +		case 'q':
> +			qval = atoi(optarg);
> +			qflag = 1;
> +			break;
>  		case 'r':
>  			rflag = 1;
>  			break;
> Index: netstat.1
> ===================================================================
> 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
> ===================================================================
> 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 */

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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 249 bytes
Desc: OpenPGP digital signature
Url : http://lists.freebsd.org/pipermail/freebsd-net/attachments/20070228/a7a71ad5/signature.pgp


More information about the freebsd-net mailing list