2012-09-19 11:20:16 -07:00
|
|
|
#!/usr/bin/env python
|
2013-05-09 15:39:30 -07:00
|
|
|
|
2013-08-22 23:36:57 -07:00
|
|
|
# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
|
2012-09-19 11:20:16 -07:00
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
|
|
|
"""
|
|
|
|
A clone of 'who' command; print information about users who are
|
|
|
|
currently logged in.
|
2014-12-23 10:45:15 -08:00
|
|
|
|
|
|
|
$ python examples/who.py
|
|
|
|
giampaolo tty7 2014-02-23 17:25 (:0)
|
|
|
|
giampaolo pts/7 2014-02-24 18:25 (:192.168.1.56)
|
|
|
|
giampaolo pts/8 2014-02-24 18:25 (:0)
|
|
|
|
giampaolo pts/9 2014-02-27 01:32 (:0)
|
2012-09-19 11:20:16 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
import psutil
|
|
|
|
from psutil._compat import print_
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2014-12-23 10:45:15 -08:00
|
|
|
users = psutil.users()
|
2012-09-19 11:20:16 -07:00
|
|
|
for user in users:
|
2014-12-23 10:45:15 -08:00
|
|
|
print_("%-15s %-15s %s (%s)" % (
|
|
|
|
user.name,
|
|
|
|
user.terminal or '-',
|
|
|
|
datetime.fromtimestamp(user.started).strftime("%Y-%m-%d %H:%M"),
|
|
|
|
user.host))
|
2012-09-19 11:20:16 -07:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|