tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

netstat -finet,inet6



I'm tired of typing "netstat -nfinet;netstat -nfinet6".  Any objections to
the following patch that makes "netstat -faf1,af2..." work?

--chris
? netstat.core
Index: main.c
===================================================================
RCS file: /cvsroot/src/usr.bin/netstat/main.c,v
retrieving revision 1.86
diff -u -r1.86 main.c
--- main.c      19 Jun 2013 21:12:03 -0000      1.86
+++ main.c      14 Oct 2013 10:40:31 -0000
@@ -395,6 +395,7 @@
        struct protox *tp;      /* for printing cblocks & stats */
        int ch;
        char *cp;
+       char *afname, *afnames;
        u_long pcbaddr;
 
        if (prog_init) {
@@ -407,6 +408,7 @@
        (void)setegid(getgid());
        tp = NULL;
        af = AF_UNSPEC;
+       afnames = NULL;
        pcbaddr = 0;
 
        while ((ch = getopt(argc, argv,
@@ -428,24 +430,7 @@
                        dflag = 1;
                        break;
                case 'f':
-                       if (strcmp(optarg, "inet") == 0)
-                               af = AF_INET;
-                       else if (strcmp(optarg, "inet6") == 0)
-                               af = AF_INET6;
-                       else if (strcmp(optarg, "arp") == 0)
-                               af = AF_ARP;
-                       else if (strcmp(optarg, "pfkey") == 0)
-                               af = PF_KEY;
-                       else if (strcmp(optarg, "unix") == 0
-                           || strcmp(optarg, "local") == 0)
-                               af = AF_LOCAL;
-                       else if (strcmp(optarg, "atalk") == 0)
-                               af = AF_APPLETALK;
-                       else if (strcmp(optarg, "mpls") == 0)
-                               af = AF_MPLS;
-                       else
-                               errx(1, "%s: unknown address family",
-                                   optarg);
+                       afnames = optarg;
                        break;
 #ifndef SMALL
                case 'g':
@@ -603,91 +588,114 @@
         */
        sethostent(1);
        setnetent(1);
-       if (iflag) {
-               if (af != AF_UNSPEC)
-                       goto protostat;
-
-               intpr(interval, nl[N_IFNET].n_value, NULL);
-               exit(0);
-       }
-       if (rflag) {
-               if (sflag)
-                       rt_stats(use_sysctl ? 0 : nl[N_RTSTAT].n_value);
+       while ((afname = strsep(&afnames, ",")) != NULL) {
+               if (strcmp(afname, "inet") == 0)
+                       af = AF_INET;
+               else if (strcmp(afname, "inet6") == 0)
+                       af = AF_INET6;
+               else if (strcmp(afname, "arp") == 0)
+                       af = AF_ARP;
+               else if (strcmp(afname, "pfkey") == 0)
+                       af = PF_KEY;
+               else if (strcmp(afname, "unix") == 0
+                   || strcmp(afname, "local") == 0)
+                       af = AF_LOCAL;
+               else if (strcmp(afname, "atalk") == 0)
+                       af = AF_APPLETALK;
+               else if (strcmp(afname, "mpls") == 0)
+                       af = AF_MPLS;
                else {
-                       if (!use_sysctl)
-                               err(1, "-r is not supported "
-                                   "for post-mortem analysis.");
-                       p_rttables(af);
+                       warnx("%s: unknown address family",
+                           afname);
+                       break;
+               }
+
+               if (iflag) {
+                       if (af != AF_UNSPEC)
+                               goto protostat;
+
+                       intpr(interval, nl[N_IFNET].n_value, NULL);
+                       break;
+               }
+               if (rflag) {
+                       if (sflag)
+                               rt_stats(use_sysctl ? 0 : nl[N_RTSTAT].n_value);
+                       else {
+                               if (!use_sysctl)
+                                       err(1, "-r is not supported "
+                                           "for post-mortem analysis.");
+                               p_rttables(af);
+                       }
+                       break;
                }
-               exit(0);
-       }
 #ifndef SMALL
-       if (gflag) {
-               if (sflag) {
-                       if (af == AF_INET || af == AF_UNSPEC)
-                               mrt_stats(nl[N_MRTPROTO].n_value,
-                                         nl[N_MRTSTAT].n_value);
+               if (gflag) {
+                       if (sflag) {
+                               if (af == AF_INET || af == AF_UNSPEC)
+                                       mrt_stats(nl[N_MRTPROTO].n_value,
+                                                 nl[N_MRTSTAT].n_value);
 #ifdef INET6
-                       if (af == AF_INET6 || af == AF_UNSPEC)
-                               mrt6_stats(nl[N_MRT6PROTO].n_value,
-                                          nl[N_MRT6STAT].n_value);
+                               if (af == AF_INET6 || af == AF_UNSPEC)
+                                       mrt6_stats(nl[N_MRT6PROTO].n_value,
+                                                  nl[N_MRT6STAT].n_value);
 #endif
-               }
-               else {
-                       if (af == AF_INET || af == AF_UNSPEC)
-                               mroutepr(nl[N_MRTPROTO].n_value,
-                                        nl[N_MFCHASHTBL].n_value,
-                                        nl[N_MFCHASH].n_value,
-                                        nl[N_VIFTABLE].n_value);
+                       }
+                       else {
+                               if (af == AF_INET || af == AF_UNSPEC)
+                                       mroutepr(nl[N_MRTPROTO].n_value,
+                                                nl[N_MFCHASHTBL].n_value,
+                                                nl[N_MFCHASH].n_value,
+                                                nl[N_VIFTABLE].n_value);
 #ifdef INET6
-                       if (af == AF_INET6 || af == AF_UNSPEC)
-                               mroute6pr(nl[N_MRT6PROTO].n_value,
-                                         nl[N_MF6CTABLE].n_value,
-                                         nl[N_MIF6TABLE].n_value);
+                               if (af == AF_INET6 || af == AF_UNSPEC)
+                                       mroute6pr(nl[N_MRT6PROTO].n_value,
+                                                 nl[N_MF6CTABLE].n_value,
+                                                 nl[N_MIF6TABLE].n_value);
 #endif
+                       }
+                       break;
                }
-               exit(0);
-       }
 #endif
-  protostat:
-       if (af == AF_INET || af == AF_UNSPEC) {
-               setprotoent(1);
-               setservent(1);
-               /* ugh, this is O(MN) ... why do we do this? */
-               while ((p = getprotoent()) != NULL) {
+         protostat:
+               if (af == AF_INET || af == AF_UNSPEC) {
+                       setprotoent(1);
+                       setservent(1);
+                       /* ugh, this is O(MN) ... why do we do this? */
+                       while ((p = getprotoent()) != NULL) {
+                               for (tp = protox; tp->pr_name; tp++)
+                                       if (strcmp(tp->pr_name, p->p_name) == 0)
+                                               break;
+                               if (tp->pr_name == 0 || tp->pr_wanted == 0)
+                                       continue;
+                               printproto(tp, p->p_name);
+                               tp->pr_wanted = 0;
+                       }
+                       endprotoent();
                        for (tp = protox; tp->pr_name; tp++)
-                               if (strcmp(tp->pr_name, p->p_name) == 0)
-                                       break;
-                       if (tp->pr_name == 0 || tp->pr_wanted == 0)
-                               continue;
-                       printproto(tp, p->p_name);
-                       tp->pr_wanted = 0;
-               }
-               endprotoent();
-               for (tp = protox; tp->pr_name; tp++)
-                       if (tp->pr_wanted)
-                               printproto(tp, tp->pr_name);
-       }
+                               if (tp->pr_wanted)
+                                       printproto(tp, tp->pr_name);
+               }
 #ifdef INET6
-       if (af == AF_INET6 || af == AF_UNSPEC)
-               for (tp = ip6protox; tp->pr_name; tp++)
-                       printproto(tp, tp->pr_name);
+               if (af == AF_INET6 || af == AF_UNSPEC)
+                       for (tp = ip6protox; tp->pr_name; tp++)
+                               printproto(tp, tp->pr_name);
 #endif
-       if (af == AF_ARP || af == AF_UNSPEC)
-               for (tp = arpprotox; tp->pr_name; tp++)
-                       printproto(tp, tp->pr_name);
+               if (af == AF_ARP || af == AF_UNSPEC)
+                       for (tp = arpprotox; tp->pr_name; tp++)
+                               printproto(tp, tp->pr_name);
 #ifdef IPSEC
-       if (af == PF_KEY || af == AF_UNSPEC)
-               for (tp = pfkeyprotox; tp->pr_name; tp++)
-                       printproto(tp, tp->pr_name);
+               if (af == PF_KEY || af == AF_UNSPEC)
+                       for (tp = pfkeyprotox; tp->pr_name; tp++)
+                               printproto(tp, tp->pr_name);
 #endif
 #ifndef SMALL
-       if (af == AF_APPLETALK || af == AF_UNSPEC)
-               for (tp = atalkprotox; tp->pr_name; tp++)
-                       printproto(tp, tp->pr_name);
-       if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag)
-               unixpr(nl[N_UNIXSW].n_value);
+               if (af == AF_APPLETALK || af == AF_UNSPEC)
+                       for (tp = atalkprotox; tp->pr_name; tp++)
+                               printproto(tp, tp->pr_name);
+               if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag)
+                       unixpr(nl[N_UNIXSW].n_value);
 #endif
+       }
        exit(0);
 }
 
Index: netstat.1
===================================================================
RCS file: /cvsroot/src/usr.bin/netstat/netstat.1,v
retrieving revision 1.66
diff -u -r1.66 netstat.1
--- netstat.1   1 Mar 2013 18:26:11 -0000       1.66
+++ netstat.1   14 Oct 2013 10:40:31 -0000
@@ -36,14 +36,15 @@
 .Nm netstat
 .Nd show network status
 .Sh SYNOPSIS
+.ds address_family Fl f Ar address_family Ns Op , Ns Ar family ...
 .Nm
 .Op Fl Aan
-.Op Fl f Ar address_family
+.Op \*[address_family]
 .Op Fl M Ar core
 .Op Fl N Ar system
 .Nm
 .Op Fl bdghiLlmnqrSsTtv
-.Op Fl f Ar address_family
+.Op \*[address_family]
 .Op Fl M Ar core
 .Op Fl N Ar system
 .Nm
@@ -67,7 +68,7 @@
 .Op Fl p Ar protocol
 .Nm
 .Op Fl is
-.Op Fl f Ar address_family
+.Op \*[address_family]
 .Op Fl I Ar Interface
 .Nm
 .Op Fl s
@@ -127,10 +128,10 @@
 .Fl i
 or an interval, as described below),
 show the number of dropped packets.
-.It Fl f Ar address_family
+.It \*[address_family]
 Limit statistics or address control block reports to those
 of the specified
-.Ar address_family  .
+.Ar address_families  .
 The following address families
 are recognized:
 .Ar inet ,


Home | Main Index | Thread Index | Old Index