File: report_user_activity.py

package info (click to toggle)
python-duo-client 5.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 768 kB
  • sloc: python: 7,105; sh: 6; makefile: 4
file content (42 lines) | stat: -rwxr-xr-x 1,212 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
import sys
from datetime import datetime, timezone

import duo_client

argv_iter = iter(sys.argv[1:])


def get_next_input(prompt):
    """Collect user input from terminal and return it."""
    try:
        return next(argv_iter)
    except StopIteration:
        return input(prompt)


def human_time(time: int) -> str:
    """Translate unix time into human readable string"""
    if time is None:
        date_str = 'Never'
    else:
        date_str = datetime.fromtimestamp(time, timezone.utc).strftime("%Y-%m-%m %H:%M:%S")
    return date_str


# Configuration and information about objects to create.
admin_api = duo_client.Admin(
        ikey=get_next_input('Admin API integration key ("DI..."): '),
        skey=get_next_input('integration secret key: '),
        host=get_next_input('API hostname ("api-....duosecurity.com"): '), )

# Retrieve user info from API:
users = admin_api.get_users()

print(f'{"Username":^30} {"Last Login":^20} {"User Enrolled"}')
print(f'{"=" * 30} {"=" * 20} {"=" * 15}')
for user in users:
    line_out = f"{user['username']:30} "
    line_out += f"{human_time(user['last_login']):20} "
    line_out += f" {user['is_enrolled']} "
    print(line_out)