python version of rbusers. list logged in users
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

116 righe
3.8 KiB

  1. #!/usr/bin/env python3
  2. """ Print currently logged in users"""
  3. import os
  4. import pwd
  5. import sys
  6. import utmp
  7. USERS = utmp.UtmpRecord()
  8. LOGGED_USERS = {}
  9. # two format strings, to take a/c of users with >10 sessions
  10. FORMAT_STRING_NORM = '%s%s \033[;0m\033[;032m(\033[;033m%d\033[;032m) '
  11. FORMAT_STRING_10 = '%s%s\033[;0m\033[;032m(\033[;033m%d\033[;032m) '
  12. # set colours if we have an encoding
  13. if sys.stdout.encoding is not None:
  14. DEFAULT_COLOUR = '\033[;0m'
  15. WHITE_TEXT_ESCAPE = '\033[;035m'
  16. WHITE_BACK_ESCAPE = '\033[;45m'
  17. RED_BACK_ESCAPE = '\033[;41m'
  18. CYAN_BACK_ESCAPE = '\033[;46m'
  19. GREEN_BACK_ESCAPE = '\033[;42m'
  20. MAGENTA_BACK_ESCAPE = '\033[;44m'
  21. YELLOW_BACK_ESCAPE = '\033[;43m'
  22. GROUPS = {100: '\033[;31m', 107: '\033[;36m',
  23. 108: '\033[;32m', 102: '\033[;33m',
  24. 101: '\033[;34m', 103: '\033[;0m'}
  25. TITLE_MESSAGE = '\033[;31mTotal \033[;0mNumber \033[;33mof \033[;34mUsers\
  26. \033[;35monline\033[;0m:'
  27. else:
  28. DEFAULT_COLOUR = ''
  29. WHITE_TEXT_ESCAPE = ''
  30. WHITE_BACK_ESCAPE = ''
  31. RED_BACK_ESCAPE = ''
  32. CYAN_BACK_ESCAPE = ''
  33. GREEN_BACK_ESCAPE = ''
  34. MAGENTA_BACK_ESCAPE = ''
  35. YELLOW_BACK_ESCAPE = ''
  36. GROUPS = {100: '', 107: '', 108: '', 102: '', 101: '', 103: ''}
  37. TITLE_MESSAGE = 'Total Number of Users Online:'
  38. def main():
  39. """ main function """
  40. # need to deal with .friends
  41. try:
  42. friends_file = open(os.path.expanduser('~/.friends'), 'r')
  43. friends = [i.rstrip() for i in friends_file.readlines()]
  44. except IOError:
  45. pass
  46. # need a dict of users + times logged in
  47. for user in USERS:
  48. if user.ut_type == 7:
  49. seven = user.ut_user
  50. try:
  51. LOGGED_USERS[seven][0] = LOGGED_USERS[seven][0] + 1
  52. except KeyError:
  53. try:
  54. group = pwd.getpwnam(seven)[3]
  55. LOGGED_USERS[seven] = [1, ]
  56. LOGGED_USERS[seven].append(
  57. GROUPS.get(group, DEFAULT_COLOUR))
  58. try:
  59. if seven in friends and group != 100:
  60. LOGGED_USERS[n][1] = white_text_escape
  61. except NameError:
  62. pass
  63. except KeyError:
  64. pass
  65. # Alan wants sorted users, so sorted users he shall get
  66. list_users = list(LOGGED_USERS.keys())
  67. list_users.sort()
  68. # printing stuff
  69. print_users(list_users)
  70. def print_users(list_users):
  71. """ Pretty Print"""
  72. print('%s%s%s' %
  73. (' ', TITLE_MESSAGE, len(list_users)))
  74. print('%s%s%s%s%s%s%s%s%s%s%s%s%s' %
  75. (' ',
  76. WHITE_BACK_ESCAPE, ' ', DEFAULT_COLOUR, ' friends ',
  77. RED_BACK_ESCAPE, ' ', DEFAULT_COLOUR, ' committee ',
  78. CYAN_BACK_ESCAPE, ' ', DEFAULT_COLOUR, ' associate'))
  79. print('%s%s%s%s%s%s%s%s%s%s%s%s%s' %
  80. (' ',
  81. MAGENTA_BACK_ESCAPE, ' ', DEFAULT_COLOUR, ' society ',
  82. YELLOW_BACK_ESCAPE, ' ', DEFAULT_COLOUR, ' club ',
  83. GREEN_BACK_ESCAPE, ' ', DEFAULT_COLOUR, ' guest'))
  84. print()
  85. print(' ', end=' ')
  86. # go through and print the users.
  87. # we only want 5 users for a line which is what iter is for
  88. itera = 0
  89. for user in list_users:
  90. itera = itera + 1
  91. temp_user = (LOGGED_USERS[user][1], user.ljust(8)[:8],
  92. LOGGED_USERS[user][0])
  93. if LOGGED_USERS[user][0] < 10:
  94. print(FORMAT_STRING_NORM % temp_user, end=' ')
  95. else:
  96. print(FORMAT_STRING_10 % temp_user, end=' ')
  97. if itera >= 5:
  98. itera = 0
  99. print()
  100. print(' ', end=' ')
  101. # reset to default colour for stupid terms
  102. print(DEFAULT_COLOUR)
  103. if __name__ == "__main__":
  104. main()