Hallo zusammen,
besten Dank für die Anregungen. Ich habe das Script jetzt fast kompl. fertig.
Einziges Problem was ich noch habe ist eine Möglichkeit der Datumsberchnung.
Das Script müsste, so wie ich es jetzt geschrieben habe, täglich laufen damit es mit der Datumsberechnung funktioniert. Ich suche nach einer Lösung die es ermöglicht, dass man alles kleiner 30 Tage herausfindet und dann entsprechend die Infos Verschickt.
Hier das Script:
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
# ------------------------------------------------------------------
# Loaded Modules
# ------------------------------------------------------------------
use strict;
use warnings;
use Net::LDAP;
use Net::LDAP;
use Date::Manip;
use Email::Simple ();
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP ();
use Email::Simple::Creator ();
# ------------------------------------------------------------------
# smtp parameters
# ------------------------------------------------------------------
my $smtpserver = 'server';
my $smtpport = 25;
my $transport = Email::Sender::Transport::SMTP->new({
host => $smtpserver,
port => $smtpport,
});
my $sender_adr = "rumpel.stilzchen\@blablub.com";
# ------------------------------------------------------------------
# ldap parameters
# ------------------------------------------------------------------
my $ldap_srv = 'PDC';
my $ldap_usr = 'CN=bal.blub,OU=User,DC=foo,DC=com';
my $ldap_pwd = 'na so eins';
my $ldap_base_dir = 'dc=foo,dc=com';
# ------------------------------------------------------------------
# connect to ldap
# ------------------------------------------------------------------
my $ldap = Net::LDAP->new( $ldap_srv ) or die "$@";
my $mesg = $ldap->bind( $ldap_usr,
password => $ldap_pwd
);
# ------------------------------------------------------------------
# search LDAP database
# ------------------------------------------------------------------
$mesg = $ldap->search( base=>$ldap_base_dir,
filter => "(&(objectclass=*)(sn=*))");
#filter=>"(name=*)",
my @entries = $mesg->entries;
foreach my $entry (@entries) {
my $samACC = $entry->get_value('samAccountName');
my $name = $entry->get_value('name');
my $descr = $entry->get_value('description');
my $mail = $entry->get_value('mail');
my $expired = readable_date($entry->get_value('accountExpires'));
my $manager = $entry->get_value('manager');
&check_data($samACC, $name, $mail, $expired, $manager, $descr);
}
# ------------------------------------------------------------------
# Process data
# ------------------------------------------------------------------
sub check_data{
my $date = ParseDate("today");
my $newdate = DateCalc(ParseDate($_[3]), ParseDateDelta('- 2 days'));
if ($newdate eq $date) {
foreach my $contact ($_[4]){
my $email = Email::Simple->create(
header => [
To => $_[2],
From => $sender_adr,
Subject => 'Konto Abgelaufen - Bitte Verlängern',
],
body => "Sehr geehrte Damen und Herren\n Der Zugang fue Account $_[1] $_[5] laeuft am $_[3] ab\n",
);
sendmail($email, { transport => $transport });
}
}
else {
print "Alles OK";
}
}
# ------------------------------------------------------------------
# disconnect from ldap
# ------------------------------------------------------------------
$mesg = $ldap->unbind; # take down session
# ------------------------------------------------------------------
# convert none readable date/time format to readable format
# ------------------------------------------------------------------
sub readable_date {
my $wintime = shift;
my $unix_epoch = win_to_unix_epoch($wintime);
my ($year, $month, $day,) = (localtime $unix_epoch)[5,4,3,2,1,0];
$year += 1900;
$month += 1;
($month, $day) = map { sprintf '%02d', $_ } $month, $day;
my $LastLogin = join('-', $year, $month, $day);
}
sub win_to_unix_epoch {
# Actually hundreths of nanoseconds at this point...
my $nanoseconds = shift;
# Get seconds
my $seconds = $nanoseconds / 10_000_000;
# This magic number is the difference between Unix and Windows epoch.
my $unix_epoch = $seconds - 11644473600;
# Return the Unix epoch for use with localtime().
return $unix_epoch;
Sollte es noch Verbesserungsvorschläge geben, dann nur zu.
Besten Dank im Voraus