Last active
October 29, 2016 15:08
-
-
Save gzxultra/9645d5ca9210404e632d8425a7ef0436 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import commands | |
import time | |
def get_current_ssh_user(): | |
output = commands.getoutput('users') | |
return output.split(' ') if output else None | |
def get_current_ssh_user_set(): | |
users = get_current_ssh_user() | |
return set(users) if users else set() | |
if __name__ == '__main__': | |
init_user_set = get_current_ssh_user_set() | |
print 'init_users are %s' % commands.getoutput('users') | |
# loop | |
current_user_set = init_user_set | |
while True: | |
new_user = get_current_ssh_user_set() - current_user_set.intersection(get_current_ssh_user_set()) | |
if new_user: | |
print 'new user %s login at %s' % (new_user, time.strftime('%Y-%m-%d %H:%M:%S')) | |
current_user_set = get_current_ssh_user_set() | |
new_logout = current_user_set - current_user_set.intersection(get_current_ssh_user_set()) | |
if new_logout: | |
print 'new user %s logout at %s' % (new_logout, time.strftime('%Y-%m-%d %H:%M:%S')) | |
current_user_set = get_current_ssh_user_set() | |
time.sleep(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment