-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadcsv.pl
159 lines (136 loc) · 4.7 KB
/
adcsv.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
#****************************************************************************************
#* ADCSV.PL *
#*======================================================================================*
#* Author : joe@joeware.net *
#* Version: V01.00.00 *
#* Modification History: *
#* V01.00.00 2004.12.08 joe Original Version *
#*--------------------------------------------------------------------------------------*
#* This reads an ADFIND dump and CSVs it. *
#*--------------------------------------------------------------------------------------*
#* Notes: *
#****************************************************************************************
#****************************************************************************************
#****************************************************************************************
#* Definitions: *
#*--------------------------------------------------------------------------------------*
#* $TRUE : Define True for testing. *
#* $FALSE : Define False for testing. *
#* $YES : Define Yes for testing. *
#* $NO : Define No for testing. *
#* $SCRIPTPATH : Path to script. *
#****************************************************************************************
$TRUE=1;
$FALSE=0;
$YES=1;
$NO=0;
($SCRIPTPATH)=($0=~/(^.*)\\.*$/);
$csvdelim=";";
$mvdelim=";";
#
# Display header
#
print "\nADCSV V01.00.00pl Joe Richards (joe\@joeware.net) December 2004\n\n";
$update=0;
$help=0;
$infile="";
$outfile="";
map {
if (/\/infile:(.+)/i) {$infile=$1};
if (/\/outfile:(.+)/i) {$outfile=$1};
if (/\/csvdelim:(.+)/i) {$csvdelim=$1};
if (/\/mvdelim:(.+)/i) {$mvdelim=$1};
if (/\/(help|h|\?)/i) {$help=1};
} @ARGV;
if ($help) {DisplayUsage()};
if (!$infile) {DisplayUsage()};
if (!$outfile) {$outfile=$infile.".txt"};
#
#
# Extract attribs and insert into a hash
#
#
$dncnt=0;
$valcnt=0;
%attribs=();
print "Extracting fields from input file $infile...\n";
open IFH,"<$infile" or die("ERR: Couldn't open infile ($infile):$!\n");
foreach $this (<IFH>)
{
$dncnt++ if $this=~/^dn:/;
next unless $this=~/^>(.+?): /;
$attribs{$1}=1;
$valcnt++;
}
@attriblist=sort keys %attribs;
$attribcnt=@attriblist;
#map {print "$_\n"} @attriblist;
print "DN Count: $dncnt\n";
print "Unique Attribute Count: $attribcnt\n";
print "Values Count: $valcnt\n";
#
#
# Extract objects and slap them into CSV format output
#
#
print "Parsing out objects and writing file $outfile\n";
open OFH,">$outfile" or die("ERR: Couldn't open outfile ($outfile):$!\n");
OutputHeader(\@attriblist);
$curdn="";
%obj=();
map {$obj{$_}=""} @attriblist;
seek(IFH,0,0);
foreach $this (<IFH>)
{
next unless $this=~/^(dn:|>)/;
if ($this=~/^dn:(.+)/)
{
print ".";
$newdn=$1;
if ($curdn)
{ # Have an object in storage
OutputObj($curdn,\%obj);
%obj=();
map {$obj{$_}=""} @attriblist;
}
$curdn=$newdn;
next;
}
chomp $this;
($attrib,$value)=($this=~/^>(.+?): (.+)$/);
if ($obj{$attrib}=~/\S/)
{ # multivalue - think quick...
$obj{$attrib}.=$mvdelim.$value;
}
else {$obj{$attrib}=$value};
}
if ($newdn) {OutputObj($curdn,\%obj)};
close IFH;
close OFH;
print "\n\nThe command completed successfully.\n\n";
exit;
sub OutputHeader
{
my $h=shift;
print OFH "DN".$csvdelim;
map {print OFH "$_".$csvdelim} @$h;
print OFH "\n";
}
sub OutputObj
{
my $dn=shift;
my $a=shift;
print OFH "\"$dn\"$csvdelim";
map {print OFH "\"$$a{$_}\"$csvdelim"} sort keys %$a;
print OFH "\n";
}
sub DisplayUsage
{
print " Usage: adcsv /infile:input_file [switches]\n\n";
print " [switches]\n";
print " outfile xxxx File to output CSV to\n";
print " csvdelim x Delimiter to use for separation of attributes (;)\n";
print " mvdelim x Delimiter to use for separation of MV attribs (;)\n";
print "\n\n";
exit;
}