Script-friendly (parseble) ps(1) output?

Patrick Tracanelli eksffa at freebsdbrasil.com.br
Thu Oct 30 17:43:41 PDT 2008


Eduardo Meyer escreveu:
[...]

> Now my next problem, do the sabe with sockstat....
> 
> %sockstat | head -4
> USER     COMMAND    PID   FD PROTO  LOCAL ADDRESS         FOREIGN ADDRESS
> freebsdsupportsshd   57255 3  tcp4   172.16.0.225:22       172.16.0.69:63583
> freebsdsupportsshd   57255 4  stream -> ??
> root     sshd       57253 3  tcp4   172.16.0.225:22       172.16.0.69:63583
> 
> So I have the USER and COMMAND column "merged" in a single string,
> because username is large. Again, space can not be used as a
> separator, but hopefully only the patterns with "->" will have space
> if not a column separator, easier to parse.
> 
> But sockstat have no formatting option to be issued like -o from
> ps(1). Measing in the very first sockstat output, I dont know what to
> do... :(

You wont be able to parse it. I believe sockstat was written when 
username was limited to 8 chars. Therefore, it will count until the 9th 
position to print another space.

I dont know if patching is an option for you, but you can truncate the 
username printing up to 8 chars. This is the huge patch which will make 
your output look better and get fully parseable:

--- sockstat.c.orig     2007-06-16 17:24:55.000000000 -0300
+++ sockstat.c  2008-10-30 21:57:05.000000000 -0200
@@ -586,7 +586,7 @@
                 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
                         pos += xprintf("%lu", (u_long)xf->xf_uid);
                 else
-                       pos += xprintf("%s", pwd->pw_name);
+                       pos += xprintf("%.8s", pwd->pw_name);
                 while (pos < 9)
                         pos += xprintf(" ");
                 pos += xprintf("%.10s", getprocname(xf->xf_pid));




-- 
Patrick Tracanelli

FreeBSD Brasil LTDA.
Tel.: (31) 3516-0800
316601 at sip.freebsdbrasil.com.br
http://www.freebsdbrasil.com.br
"Long live Hanin Elias, Kim Deal!"



More information about the freebsd-stable mailing list