-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathwelcome.pl
executable file
·124 lines (112 loc) · 3.3 KB
/
welcome.pl
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
#!/usr/bin/perl -T
#
# Author: Hari Sekhon
# Date: 2009-12-10 17:57:49 +0000 (Thu, 10 Dec 2009)
#
# https://github.com/HariSekhon/DevOps-Perl-tools
#
# 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
#
$DESCRIPTION = "Prints a slick welcome message with last login time
Tested on Mac OS X and Linux";
$VERSION = "2.1.1";
use strict;
use warnings;
BEGIN {
use File::Basename;
use lib dirname(__FILE__) . "/lib";
}
use HariSekhonUtils;
use Time::HiRes 'sleep';
autoflush();
my $quick;
%options = (
"q|quick" => [ \$quick, "Print instantly without fancy scrolling effect, saves 2-3 seconds (you can also Control-C to make output complete instantly)" ],
);
get_options();
set_timeout(20);
# not set on Alpine Linux in Docker
my $user = $ENV{"USER"};
unless($user){
vlog2 "\$USER not populated, trying whoami";
$user = `whoami`;
}
unless($user){
vlog2 "whoami failed, trying 'id' command";
$user = `id`;
$user =~ s/.*?\(([^\)]+?)\).*/$1/;
}
unless($user){
vlog2 "couldn't determine username, falling back to generic user salutation";
$user = "user";
}
$user = isUser(trim($user)) || die "invalid user '$user' determined from environment variable \$USER\n";
if($user eq "root"){
$user = uc $user;
} elsif(length($user) < 4 or $user =~ /\d/ or $user eq "user"){
# probably not a person's name, don't capitalize
} else {
$user = ucfirst lc $user;
}
my $last_login;
if(which("last", 0)){
my @output = cmd("last -100", 1);
for(my $i=1; $i < $#output; $i++){
$output[$i] =~ /^(?:reboot|wtmp)|^\s*$/ and next;
$last_login = $output[$i];
last;
}
} else {
print STDERR "WARNING: 'last' command not found, will not be able to get last login information\n";
}
my $msg = "Welcome $user - ";
if($last_login){
( my $last_user = $last_login ) =~ s/\s+.*$//;
$last_user = isUser($last_user) || die "invalid user '$last_user' determined from last log";
$last_user = uc $last_user if $last_user eq "root";
# strip up to "Day Mon NN" ie "%a %b %e ..."
$last_login =~ s/.*(\w{3}\s+\w{3}\s+\d+)/$1/ or die "failed to find the date format in the last log";
$last_login =~ s/ *$//;
$last_login =~ /^[\w\s\:\(\)-]+$/ or die "last login '$last_login' failed to match expected format";
$msg .= "last login was ";
if($last_user eq "ROOT"){
$msg .= "ROOT";
} elsif(lc $last_user eq lc $user){
$msg .= "by you";
} else {
$msg .= "by $last_user";
}
$msg .= " => $last_login";
} else {
$msg .= "no last login information available!";
}
my $ESC = "\033";
print "${ESC}[s";
$SIG{'INT'} = sub { print "${ESC}[u$msg\n"; exit 1; };
if($quick){
print "$msg\n";
exit 0;
}
my @charmap = ("A".."Z", "a".."z", 0..9, split('', '@#$%^&*()'));
my $random_char;
foreach my $char (split("", $msg)) {
print " ";
my $j = 0;
while(1){
if ($j > 3) {
$random_char = $char;
} else {
$random_char = $charmap[int(rand(@charmap))];
}
print "\b$random_char";
last if ("$random_char" eq "$char");
$j += 1;
sleep 0.0085;
}
}
print "\n";