-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexten.pl
168 lines (142 loc) · 4.66 KB
/
exten.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
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
160
161
162
163
164
165
166
167
168
#!/usr/bin/perl
# exten.pl v1.1
#
# Script to generate extensions 'extensions_custom.conf' file,
# from Active Directory (LADP) on groups in OU=ADGroupsSearchBase
# which groups contains 'description' attribute
#
# Using:
# 1. Print users to STDOUT:
# exten.pl
#
# 2. Print users to file:
# exten.pl exten_custom.conf
use strict;
use warnings;
use Net::LDAP;
use Lingua::Translit;
######################
### BEGIN SETTINGS ###
######################
my $debug = 0;
my $warning = 1;
#name of Domain
my $AD="domain";
#Domain name in format AD
#for example mydomain.ru
my $ADDC="DC=domain";
# user in Active directory
# example: "CN=asterisk,CN=Users,$ADDC"
my $ADUserBind="CN=asterisk,CN=Users,$ADDC";
my $ADpass="p@s$w0rd";
# base search Groups tree example "OU=Users,$ADDC"
my $ADGroupsSearchBase = "OU=asterisk,OU=Groups,OU=Organisation,$ADDC";
# base search Users tree example "OU=Users,$ADDC"
my $ADUsersSearchBase = "OU=Organisation,$ADDC";
# default email to send voicemail if email user not set
my $defaultEmail = '[email protected]';
# Field in active directory where telephone number, display name, phone stored ...
# "telephonenumber", "displayname", "mail", ...
my $ADfieldTelephone = "telephonenumber";
my $ADfieldMember = "member";
my $ADfieldMemberOf = "memberof";
my $ADfieldInfo = "info";
my $ADfieldDescription = "description";
my $ADfieldMail = "mail";
#######################
### END OF SETTINGS ###
#######################
my $ldap;
# get array DNS names of AD controllers
my @adControllers = `dig -t srv _ldap._tcp.$AD | grep -v '^;\\|^\$' | grep SRV | awk '{print \$8}'`;
# try connect to AD controllers
foreach my $controller (@adControllers){
$controller =~ s/\n//;
#INITIALIZING
$ldap = Net::LDAP->new ( $controller ) or next;
print STDERR "Connected to AD controller: $controller\n" if $debug > 0;
last;
}
die "$@" unless $ldap;
my $mesg = $ldap->bind ( dn=>$ADUserBind, password =>$ADpass);
#PROCESSING - Displaying SEARCH Results
# Accessing the data as if in a structure
# i.e. Using the "as_struct" method
my $ldapGroups = LDAPsearch (
$ADGroupsSearchBase,
"$ADfieldDescription=*",
[ $ADfieldMember, $ADfieldDescription ]
)->as_struct;
# translit RUS module.
# GOST 7.79 RUS, reversible, GOST 7.79:2000 (table B), Cyrillic to Latin, Russian
my $tr = new Lingua::Translit("GOST 7.79 RUS");
my $hash = ();
# process each group in $ADGroupsSearchBase with phone
while ( my ($distinguishedName, $groupAttrs) = each(%$ldapGroups) ) {
print STDERR "Processing GROUP: [$distinguishedName]\n" if $debug > 1;
my $attrMembers = $groupAttrs->{ $ADfieldMember } or next;
my $desc = $groupAttrs->{ $ADfieldDescription } or next;
my $groupNumber = "@$desc";
print STDERR "MEMBERS: @$attrMembers\nDESC: $groupNumber (Count=$#$attrMembers+1)" if $debug > 1;
# process members in current group
foreach my $member (@$attrMembers) {
my $ldapMember = LDAPsearch(
$ADUsersSearchBase,
"$ADfieldTelephone=*",
[ $ADfieldTelephone ]
) -> as_struct;
my $memberAttrs = $ldapMember->{$member};
my $memberPhone = $memberAttrs->{$ADfieldTelephone}[0] or next;
print STDERR "\nMEMBER: $member" if $debug > 1;
print STDERR "\tPHONE:$memberPhone" if $debug > 1;
if ($hash -> {$groupNumber}){
my $a = $hash -> {$groupNumber};
push @$a, $memberPhone;
} else {
$hash -> {$groupNumber} = [$memberPhone];
}
}
print STDERR "\n\n" if $debug > 1;
} # End of that groups in $ADGroupsSearchBase
my @out;
while ( my ($groupPhone, $userPhones) = each (%$hash) ) {
print STDERR "GROUP: $groupPhone\t PHONES: @$userPhones\n" if $debug > 1;
#foreach my $userPhone (@$userPhones) {
push (@out, "exten => $groupPhone,1,Dial(sip/" . join('&sip/', @$userPhones) . ")\n");
}
# print to file
if (@ARGV){
open FILE, "> $ARGV[0]" or die "Error create file '$ARGV[0]': $!";
print STDOUT "Printing to file '$ARGV[0]'";
print FILE @out;
close FILE;
print STDOUT " ...done!\n";
}
# print to STDOUT
else{
print @out;
}
exit 0;
#OPERATION - Generating a SEARCH
# $base, $searchString, $attrsArray
sub LDAPsearch
{
my ($base, $searchString, $attrs) = @_;
my $ret = $ldap->search ( base => $base,
scope => "sub",
filter => $searchString,
attrs => $attrs
);
LDAPerror("LDAPsearch", $ret) && die if( $ret->code );
return $ret;
}
sub LDAPerror
{
my ($from, $mesg) = @_;
my $err = "[$from] - error"
."\nCode: " . $mesg->code
."\nError: " . $mesg->error . " (" . $mesg->error_name . ")"
."\nDescripton: " . $mesg->error_desc . ". " . $mesg->error_text;
print STDERR $err if $warning;
#print STDERR "\nServer error: " . $mesg->server_error if $debug;
}