users 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/bin/sh -e
  2. #
  3. # users: print the number of remote users on the machine
  4. #
  5. # Copyright (C) 2009 Raphaël Pinson.
  6. # Copyright (C) 2009 Canonical Ltd.
  7. # Copyright (C) 2011-2014 Dustin Kirkland
  8. #
  9. # Authors: Raphaël Pinson <raphink@ubuntu.com>
  10. # Dustin Kirkland <kirkland@byobu.org>
  11. #
  12. # This program is free software: you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License as published by
  14. # the Free Software Foundation, version 3 of the License.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License
  22. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. __users_detail() {
  24. ps -ef 2>/dev/null | grep "sshd:.*@" | grep -v grep
  25. }
  26. __users() {
  27. local count=0
  28. if [ "$USERS_DISTINCT" = "1" ]; then
  29. count=$(pgrep -fl 'sshd:.*@' | cut -f3 -d\ | cut -f1 -d@ | sort -u | wc -l)
  30. else
  31. # Note: we'd like to use pgrep -c, however, this isn't available in
  32. # busybox and some distro's pgrep (and it doesn't exit non-zero).
  33. count=$(pgrep -f "^sshd:.*@|^/usr/sbin/sshd -i" | wc -l) || return
  34. fi
  35. if [ $count -gt 0 ]; then
  36. color b w r; printf "%d" "$count"; color -; color w r; printf "##"; color --
  37. else
  38. rm -f "$BYOBU_RUN_DIR/status.$BYOBU_BACKEND/users"*
  39. fi
  40. }
  41. # vi: syntax=sh ts=4 noexpandtab