-
Notifications
You must be signed in to change notification settings - Fork 0
/
phone
executable file
·115 lines (113 loc) · 3.91 KB
/
phone
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
#!/usr/bin/perl
use strict;
use threads;
use threads::shared;
# Basic argument checking
if ( $ARGV[0]!~/[A-z]{3,}/ ) {
die "You must provide the username or the user's first or last name as argument (at least 3 letters for limiting the result list)...\n"
}
# basic ldapsearch executable checking
my $ldapsearch="/usr/bin/ldapsearch";
unless ( -x $ldapsearch ) {
die "No $ldapsearch on the system. Please make sure to install the \"openldap-clients\" package.\n";
}
$|=1;
my ($searching_for):shared=$ARGV[0];
chomp $searching_for;
my $ldap="$ldapsearch -H ldap://ldap.corp.redhat.com -x -b";
# Try to quickly resolve the query using the argument as the userid
open LDAP, "$ldap \"uid=$searching_for,ou=Users,dc=redhat,dc=com\"|" or die "$!\n";
my $quicksearch_search_found_flag;
my $phone;
my $fullname;
while (my $line=<LDAP>) {
$quicksearch_search_found_flag=1 if ($line=~/^#.*?Users, redhat.com$/i); # the username was found
if ($line=~/^rhatPhoneExt:/) { # getting to this line means that the userid was found
$phone=&retrieve_phone($line);
}
if ($line=~/^cn:/) { # Look for full name
($fullname=$line)=~s/^cn:\s{1}(.*)/$1/;
chomp $fullname;
}
if ($phone=~/\d+/ && $fullname=~/\w+/) { # both the phone and the full name - have values
printf "\t%-25s %6s\n", $fullname,$phone;
exit 0;
}
}
if ($quicksearch_search_found_flag == 1) { # Found the username, but there's no telephone
print "The username \"$searching_for\" was found, but has no phone number in the system\n";
exit 0;
}
#getting here - means that no phone number was found for the argument as uid..
close LDAP;
my ($phone,$count,$thread_completed_flag):shared;
my $users;
my $printdot=threads->new(\&printdot);
my $main=threads->new(\&main);
$main->join;
$printdot->join;
sub main {
#Trying to look for matching first/last name
open LDAP, "$ldap \"ou=Users,dc=redhat,dc=com\"|" or die "$!\n";
my $username;
while (my $line=<LDAP>) {
next if $line=~/^\s+/; # omit empty lines
if ($line=~/^#.*?Users, redhat.com$/i) { # change the username every time you run into comment at the beginning of the line
($username=$line)=~s/^\#\s{1}(.*?),.*$/$1/; # reset the username
chomp $username;
}
&phone($username,$line);
}
$thread_completed_flag=1;
&display;
}
sub retrieve_phone {
my $line=shift;
if ($line=~/^rhatPhoneExt:/) { # getting to this line means that the userid was found
if ($line=~/^rhatPhoneExt:\s{1}.*?(\d{7})$/) { # deal with normal office numbers
($phone=$line)=~s/^rhatPhoneExt:\s{1}.*?(\d{7})$/$1/;
} else {
($phone=$line)=~s/^rhatPhoneExt:\s{1}(.*)$/$1/; # deal with numbers like 986-057-2439 - assume these numbers aren't internal
}
chomp $phone;
$phone;
}
}
sub display() {
print "\n";
if ($count==0) {
print "No matches for $searching_for\n";
} else {
for (keys %$users) {
if (defined $users->{$_}{'fullname'}) {
printf "\t%-25s %6s\n", $users->{$_}{'fullname'},$users->{$_}{'phone'};
}
}
}
}
sub phone ($$) {
my ($username,$line)=@_;
my $fullname;
if (($line=~/^cn:/) && ($username=~/\w+/)) {
($fullname=$line)=~s/^cn:\s{1}(.*)/$1/;
chomp $fullname;
my ($first_name,$last_name)= split /\s/,$fullname;
if ($first_name =~/^$searching_for/i or $last_name =~ /^$searching_for/i) {
$count++;
$users->{$username}{'fullname'}=$fullname;
$users->{$username}{'phone'}="Not specified"; #Initiate the phone to Not specified - will be replaced by number if found
}
}
elsif ($line=~/^rhatPhoneExt:/ && exists $users->{$username}{'fullname'}) {
$phone=&retrieve_phone($line);
$users->{$username}{'phone'}=$phone;
}
}
sub printdot {
print "No such username: \"$searching_for\". Will try to find first or last name that begins with \"$searching_for\".\nSearching";
while (!$thread_completed_flag) { # print dots as long as the searching wasn't finished
print ".";
sleep 1;
}
return;
}