forked from HariSekhon/DevOps-Python-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
welcome.py
executable file
·159 lines (146 loc) · 5.26 KB
/
welcome.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2009-12-09 19:58:14 +0000 (Wed, 09 Dec 2009)
#
# https://github.com/harisekhon/pytools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback
# to help improve or steer this or other code I publish
#
# https://www.linkedin.com/in/harisekhon
#
"""
Prints a slick welcome message with last login time
Tested on Mac OS X and Linux
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# from __future__ import unicode_literals
import getpass
import os
import random
import re
import string
import sys
import time
libdir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'pylib'))
sys.path.append(libdir)
try:
# pylint: disable=wrong-import-position
from harisekhon.utils import ERRORS, isUser, which, printerr
from harisekhon import CLI
except ImportError as _:
print('module import failed: %s' % _, file=sys.stderr)
print("Did you remember to build the project by running 'make'?", file=sys.stderr)
print("Alternatively perhaps you tried to copy this program out without it's adjacent libraries?", file=sys.stderr)
sys.exit(4)
__author__ = 'Hari Sekhon'
__version__ = '2.0'
class Welcome(CLI):
def __init__(self):
# Python 2.x
super(Welcome, self).__init__()
# Python 3.x
# super().__init__()
self.quick = False
def case_user(self, user): # pylint: disable=no-self-use
if user == 'root':
user = user.upper()
elif len(user) < 4 or re.search(r'\d', user):
# probably not a real name
pass
else:
user = user.title()
return user
def construct_msg(self): # pylint: disable=no-self-use
# user = os.getenv('USER', '').strip()
user = getpass.getuser()
if not isUser(user):
# print("invalid user '%s' determined from environment variable $USER, failed regex validation" % user)
print("invalid user '%s' returned by getpass.getuser(), failed regex validation" % user)
sys.exit(ERRORS['CRITICAL'])
user = self.case_user(user)
msg = 'Welcome %s - ' % user
last = ''
if which("last"):
_ = os.popen('last -100')
_.readline()
re_skip = re.compile(r'^(?:reboot|wtmp)|^\s*$')
last = ''
for line in _:
last = line.rstrip('\n')
if re_skip.match(last):
last = ''
continue
break
_.close()
else:
printerr("WARNING: 'last' command not found, will not be able to get last login information")
if last:
msg += 'last login was '
last_user = re.sub(r'\s+.*$', '', last)
if last_user == 'root':
last_user = 'ROOT'
# strip up to "Day Mon NN" ie "%a %b %e ..."
(last, num_replacements) = re.subn(r'.*(\w{3}\s+\w{3}\s+\d+)', r'\g<1>', last)
if not num_replacements:
print('failed to find the date format in the last log')
sys.exit(ERRORS['CRITICAL'])
last = re.sub(' *$', '', last)
if last_user == 'ROOT':
msg += 'ROOT'
elif last_user.lower() == user.lower():
msg += 'by you'
else:
msg += 'by %s' % last_user
msg += ' => %s' % last
else:
msg += 'no last login information available!'
return msg
def print_welcome(self): # pylint: disable=no-self-use
msg = self.construct_msg()
if self.quick:
print(msg)
return
try:
charmap = list(string.uppercase + string.lowercase + '@#$%^&*()')
# print '',
# print('', end='')
for i in range(0, len(msg)):
char = msg[i]
# print '',
print(' ', end='')
j = 0
while 1:
if j > 3:
random_char = char
else:
random_char = random.choice(charmap)
# going from print statement to func requires one less backspace otherwise it scrolls backwards
# print '\b\b%s' % random_char,
print('\b%s' % random_char, end='')
sys.stdout.flush()
if char == random_char:
break
j += 1
time.sleep(0.0085)
print()
except KeyboardInterrupt:
# print('\b\b\b\b%s' % msg[i:])
print('\b\b\b%s' % msg[i:])
def add_options(self):
self.add_opt('-q', '--quick', action='store_true', default=False,
help='Print instantly without fancy scrolling effect, saves 2-3 seconds ' +\
'(you can also Control-C to make output complete instantly)')
def run(self):
self.quick = self.get_opt('quick')
if self.args:
self.usage()
self.print_welcome()
if __name__ == '__main__':
Welcome().main()