1
2
3
4
5
6
7
8
9
10
11
SamAccountName : U011004
LastLogonDate : 20.06.2016 15:01:10
ExpiryDate : 19.12.2016 13:58:05
PasswordLastSet : 20.06.2016 14:58:05
mail : J.W@dbx.com
SamAccountName : U011001
LastLogonDate : 25.05.2016 09:53:51
ExpiryDate : 23.11.2016 08:53:28
PasswordLastSet : 25.05.2016 09:53:28
mail : MM.AA@yxz.de
1
2
3
4
SamAccountName ExpiryDate PasswordLastSet mail
-------------- ---------- --------------- ----
U011004 19.12.2016 13:58:05 20.06.2016 14:58:05 J.W@dbx.com
U011001 23.11.2016 08:53:28 25.05.2016 09:53:28 MM.AA@xyz.de
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
# ArrayReferenz = get_datasets( FileHandle) sub get_datasets { my $handle = shift; my @datasets; # read blockwise; separate at double newline local $/ = "\n\n"; while ( my $dataset = <$handle> ) { # remove double newline from dataset chomp $dataset; # first split by newline, then at the first ':' # identifiers must not contain ':' themselves # store splitted fields in a new hash my %set = map { split m{\s*:\s*}, $_, 2 } split( m{\n}, $dataset ); # store reference to new hash in array push @datasets, \%set; } # return array reference return \@datasets; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@datasets = (
{
'SamAccountName' => 'U011004',
'ExpiryDate' => '19.12.2016 13:58:05',
'PasswordLastSet' => '20.06.2016 14:58:05',
'mail' => 'J.W@dbx.com',
'LastLogonDate' => '20.06.2016 15:01:10'
},
{
'SamAccountName' => 'U011001',
'ExpiryDate' => '23.11.2016 08:53:28',
'PasswordLastSet' => '25.05.2016 09:53:28',
'mail' => 'MM.AA@yxz.de',
'LastLogonDate' => '25.05.2016 09:53:51'
}
);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
... my @fields = qw( SamAccountName ExpiryDate PasswordLastSet mail ); my $format = "%-25s %-25s %-25s %-25s\n"; # header fields printf $format, @fields; # underline header fields (same length as header fields printf $format, map { '-' x length $_ } @fields; my $datasets = get_datasets( $handle ); for my $set ( @$datasets ) { my %h = %$set; printf $format, @h{@fields}; }
1
2
3
4
SamAccountName ExpiryDate PasswordLastSet mail
-------------- ---------- --------------- ----
U011004 19.12.2016 13:58:05 20.06.2016 14:58:05 J.W@dbx.com
U011001 23.11.2016 08:53:28 25.05.2016 09:53:28 MM.AA@yxz.de
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
use strict; use warnings; use Data::Dumper; my $result = {}; my $oid = ''; while(my $lin = <DATA> ){ chomp $lin; my ($att, $val) = split /\s+?:\s+?/, $lin or next; if( $att eq 'SamAccountName'){ $result->{$val} = {}; $oid = $val; } $result->{$oid}{$att} = $val; } print Dumper $result; __DATA__ SamAccountName : U011004 LastLogonDate : 20.06.2016 15:01:10 ExpiryDate : 19.12.2016 13:58:05 PasswordLastSet : 20.06.2016 14:58:05 mail : J.W@dbx.com SamAccountName : U011001 LastLogonDate : 25.05.2016 09:53:51 ExpiryDate : 23.11.2016 08:53:28 PasswordLastSet : 25.05.2016 09:53:28 mail : MM.AA@yxz.de
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
$VAR1 = { 'U011004' => { 'SamAccountName' => 'U011004', 'PasswordLastSet' => '20.06.2016 14:58:05', 'ExpiryDate' => '19.12.2016 13:58:05', 'mail' => 'J.W@dbx.com', 'LastLogonDate' => '20.06.2016 15:01:10' }, 'U011001' => { 'SamAccountName' => 'U011001', 'PasswordLastSet' => '25.05.2016 09:53:28', 'ExpiryDate' => '23.11.2016 08:53:28', 'mail' => 'MM.AA@yxz.de', 'LastLogonDate' => '25.05.2016 09:53:51' } };
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
use strict;
use warnings;
use Data::Dumper;
my $result = {};
local $/ = "\n\n";
while(my $block = <DATA> ){
chomp $block;
my %info = map{
split /\s*:\s*/, $_, 2
}split /\n/, $block;
my $oid = $info{SamAccountName};
$result->{$oid} = \%info;
}
print Dumper $result;
__DATA__
SamAccountName : U011004
LastLogonDate : 20.06.2016 15:01:10
ExpiryDate : 19.12.2016 13:58:05
PasswordLastSet : 20.06.2016 14:58:05
mail : J.W@dbx.com
SamAccountName : U011001
LastLogonDate : 25.05.2016 09:53:51
ExpiryDate : 23.11.2016 08:53:28
PasswordLastSet : 25.05.2016 09:53:28
mail : MM.AA@yxz.de
131269498665739707
23.12.2016 07:57:46
@{Name="ExpiryDate";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}
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
my $parm = "131130381022593331";
my $parm2 = "131121778351196949";
my @parm = ($parm, $parm2);
foreach my $wintime (@parm) {
my $unix_epoch = win_to_unix_epoch($wintime);
my ($year, $month, $day, $hour, $minute, $second) = (localtime $unix_epoch)[5,4,3,2,1,0];
$year += 1900;
$month += 1;
($month, $day, $hour, $minute, $second) = map { sprintf '%02d', $_ } $month, $day, $hour, $minute, $second;
my $LL= join('-', $day, $month, $year) . ' ' . join(':', $hour, $minute, $second);
print $LL."\n";
}
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;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#! /usr/bin/perl use strict; use warnings; my @array = qw( a b c ); print "@array\n"; for my $elem ( @array ) { # overwrite element in @array $elem = 1; } print "@array\n";
1
2
3
4
5
6
7
8
...
my @unix_times;
for my $wintime ( @times ) {
push @unix_times, calc_unix_time($wintime);
}
print join "\n", @unix_times, '';
2016-07-18T10:20:14 LinuxerIch hab's nicht verstanden. In was soll $LL denn aufgespalten werden?
2016-07-18T10:56:59 LinuxerIch hatte meinen Beitrag editiert; hast Du den Nachtrag schon gesehen?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
sub readable_wintime { my $wintime = shift; my $unix_epoch = win_to_unix_epoch($wintime); my ($year, $month, $day, $hour, $minute, $second) = (localtime $unix_epoch)[5,4,3,2,1,0]; $year += 1900; $month += 1; ($month, $day, $hour, $minute, $second) = map { sprintf '%02d', $_ } $month, $day, $hour, $minute, $second; my $LL= join('-', $day, $month, $year) . ' ' . join(':', $hour, $minute, $second); return $LL; } 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; }
1
2
$LastLogin = readable_wintime($LastLogin);
$PasswortSet = readable_wintime($PasswortSet);
1 2 3 4 5 6 7 8 9 10 11 12 13 14
sub readable_wintime { my $wintime = shift; my $unix_epoch = win_to_unix_epoch($wintime); my ($year, $month, $day, $hour, $minute, $second) = (localtime $unix_epoch)[5,4,3,2,1,0]; $year += 1900; $month += 1; ($month, $day, $hour, $minute, $second) = map { sprintf '%02d', $_ } $month, $day, $hour, $minute, $second; my $date = join('-', $day, $month, $year); my $time = join(':', $hour, $minute, $second); return $date, $time; }
1
2
my ( $login_date, $login_time ) = readable_wintime($LastLogin);
my ( $pwset_date, $pwset_time ) = readable_wintime($PasswortSet);
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
#!/usr/bin/perl -w
################################################################################
#
# AUTHOR : xyz
# CREATION DATE : 18 July 2016
#
# SHORT DESCRIPTION:
# find all AD/LDAP user and login credentials
# (c) RH 2016
#
# -----------------------------------------------------------------------------
# File name : search_ldap_user_v1_20160718.pl
# Location : ESB
# Last edited by : RH
# Last Checkin :
# Revision : 0.1
# -----------------------------------------------------------------------------
# History:
#
# 18.07.2016 first version
#
# -----------------------------------------------------------------------------
#
# Last change: 18.07.2016 RH
#
################################################################################
#
#
# ------------------------------------------------------------------
# Loaded Modules
# ------------------------------------------------------------------
use strict;
use warnings;
use Net::LDAP;
# ------------------------------------------------------------------
# ldap parameters
# ------------------------------------------------------------------
my $ldap_srv = 'server1.domain.com';
my $ldap_usr = 'CN=Username,OU=User,OU=ESB,OU=DE,DC=domain,DC=com';
my $ldap_pwd = 'geheim';
my $ldap_base_dir = 'dc=domain,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=user)(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 $lastLogon = readable_wintime($entry->get_value('lastLogon'));
my $pwdLastSet = readable_wintime($entry->get_value('pwdLastSet'));
my $mail = $entry->get_value('mail');
print "UserID:$samACC\t
User Name:$name\t
Beschreibung:$descr\t
Letzter Login:$lastLogon\t
Last_PWD_Set:$pwdLastSet\t
Email:$mail\n";
}
# ------------------------------------------------------------------
# disconnect from ldap
# ------------------------------------------------------------------
$mesg = $ldap->unbind; # take down session
# ------------------------------------------------------------------
# convert none readable date/time format to readable format
# ------------------------------------------------------------------
sub readable_wintime {
my $wintime = shift;
my $unix_epoch = win_to_unix_epoch($wintime);
my ($year, $month, $day, $hour, $minute, $second) = (localtime $unix_epoch)[5,4,3,2,1,0];
$year += 1900;
$month += 1;
($month, $day, $hour, $minute, $second) = map { sprintf '%02d', $_ } $month, $day, $hour, $minute, $second;
my $LastLogin = join('-', $day, $month, $year) . ' ' . join(':', $hour, $minute, $second);
}
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;
}
my $memberof = $entry->get_value('memberof');
perldoc Net::LDAP::EntryCode: (dl )1
2
3
4
5get_value ( ATTR, OPTIONS )
Get the values for the attribute ATTR. In a list context returns all values for the given attribute,
or the empty list if the attribute does not exist. In a scalar context returns the first value for
the attribute or undef if the attribute does not exist.
my $memberof = join ", ", $entry->get_value('memberof');
2016-07-20T09:36:16 LinuxerNaja, ist doch kein großes Hexenwerk?