home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / lsof / examples / xusers.awk < prev   
Encoding:
AWK Script  |  1997-03-10  |  3.9 KB  |  138 lines

  1. #!/usr/bin/awk -f
  2. ################################################################
  3. #  
  4. #  Program Name  :  xusers
  5. #  Date Created  :  02-27-97
  6. #  Author        :  Dan A. Mercer
  7. #  Email         :  damercer@mmm.com
  8. #                :
  9. #  Description   : Print list of users and applications signed on
  10. #                : X workstations
  11. ################################################################
  12. # standard help message
  13. function help(hlpmsg) {
  14. basename = ARGV[0]
  15. sub(/.*\//,"",basename)
  16. printf "Format:  %s [o=[hi]] [s=cdlp] [pattern]\n", basename
  17. print  "Print list of users and applications signed on X workstations"
  18. print  "NOTE: applicationname is truncated to 9 chars"
  19. print  "Arguments:"
  20. print  "           o=[h|i]      - Options"
  21. print  "              h         - help - print this message"
  22. print  "              i         - case insensitive pattern search"
  23. print  "           s=[c|d|l|p]  - Sort Options"
  24. print  "              c         - sort by command"
  25. print  "              d         - sort by display name"
  26. print  "              l         - sort by login name"
  27. print  "              p         - sort by pid"
  28. print  "           pattern      - regex pattern to search commands against"
  29.  
  30. if (length(hlpmsg)) print hlpmsg
  31. exit
  32. }
  33. BEGIN {
  34. # process command line
  35. for (i=1;i<ARGC;i++) {
  36.    if (ARGV[i] ~ /^o=/) {
  37.       if (options)
  38.          help("duplicate option string")
  39.       options = ARGV[i]
  40.       sub(/^o=/,"",options)
  41.       if (options !~ /^[hi]$/)
  42.          help("Invalid options " options)
  43.       if ("h" == options)
  44.          help("")
  45.       else
  46.          igncase = 1
  47.       }
  48.    else if (ARGV[i] ~ /^s=/) {
  49.       if (sortorder)
  50.          help("duplicate sort order string")
  51.       sortorder = ARGV[i]
  52.       sub(/^s=/,"",sortorder)
  53.       if (sortorder !~ /^[cdlp]$/)
  54.          help("Invalid sort order: '" sortorder "'")
  55.       if ("p" == sortorder) {
  56.          sort = "sort -kn2"
  57.          }
  58.       else if ("c" == sortorder) {
  59.          # the 'b' option means ignore leading blanks
  60.          sort = "sort -kb3"
  61.          }
  62.       else if ("l" == sortorder) {
  63.          sort = "sort -kb1"
  64.          }
  65.       else {
  66.          sort = "sort -kb4"
  67.          }
  68.       }
  69.    else {
  70.       if (pattern)
  71.          help("duplicate pattern string")
  72.       pattern = ARGV[i]
  73.       }
  74.    }
  75.  
  76. # default is to sort by pid
  77. sort = (sort) ? sort : "sort -kn2"
  78.  
  79. # check for igncase
  80. if (pattern && igncase)
  81.    pattern = tolower(pattern)
  82.  
  83. # set default pattern
  84. pattern = (pattern) ? pattern : ".*"
  85.  
  86. cmd = "lsof -FpLcn  -awP -iTCP:6000"
  87. #            ||||| ||||  |
  88. #            ||||| ||||  X servers use port 6000
  89. #            ||||| |||don't list port names
  90. #            ||||| ||suppress warning messages
  91. #            ||||| |and all conditions
  92. #            ||||| |options
  93. #            |||||
  94. #            ||||Internet addresses
  95. #            |||command name
  96. #            ||login name
  97. #            |process id
  98. #            Format string
  99. # Output consists of one record per pid,  followed by newline
  100. # delimited fields for command, Login name, and network address
  101. # The pid is preceded by a 'p',  command by a 'c',
  102. # Login name by an L, and network connection by an 'n'.  There may
  103. # be multiple 'n' entries (for instance for vuewm)
  104.  
  105. while ((cmd | getline field) > 0) {
  106.    type = substr(field,1,1)
  107.    sub("^.","",field)
  108.    if ("p" == type) {
  109.       # always output first
  110.       pid = field
  111.       PID[pid] = ++ct
  112.       }
  113.    else if ("c" == type) {
  114.       # always output second
  115.       XAPPL[pid] = field
  116.       }
  117.    else if ("L" == type) {
  118.       # always output fourth
  119.       USER[pid] = field
  120.       }
  121.    else if ("n" == type) {
  122.       # may be multiple instances - we just use the last
  123.       gsub(".*->|:6000","",field)
  124.       DPY[pid] = field
  125.       }
  126.    }
  127. close(cmd)
  128.  
  129. printf "%8s  %5s  %-9s  %s\n","USER","PID","COMMAND","DISPLAY"
  130. for (pid in PID) {
  131.    if (((igncase) ? tolower(XAPPL[pid]) : XAPPL[pid]) ~ pattern)
  132.       printf "%8s  %5d  %-9s  %s\n", USER[pid],pid,XAPPL[pid],DPY[pid] | sort
  133.    }
  134.  
  135. close(sort)
  136. exit
  137. }
  138.