~siggi-bjarnason/siggivbscript/vbscript

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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
Option Explicit
Dim inFileObj, strLine, fso, strOut, strParts, inFileName, outFileObj, userOutFileName, bSendMail, Subject, i, DictKeys, strName, outstr, perms, path, tmpstr2
Dim ProfileNum, Profile, ProfilePermDict, ProfileNameDict, ProfileSettingsDict, groupOutFileName, UserDict, SettingsArray(2), UserArray(4), tmpstr, EmailBodyFileName

'Const MailServerName = "smtphost.redmond.corp.microsoft.com" 
Const MailServerName = "satsmtpa01"
Const SMTPTimeout = 10
Const FromAddress = "harrydup@microsoft.com"
Const ToAddress = "linnco@microsoft.com;netpro@microsoft.com;gnspm@microsoft.com"
Const CCAddress = ""
Const BCCAddress = "harrydup@microsoft.com "
Const cdoSendUsingPort = 2
Const cdoNTLM = 2

Subject = "TACACS Audit report for "  & FormatDateTime(Now,vbLongDate)

Set ProfilePermDict = CreateObject("Scripting.Dictionary")
Set ProfileNameDict = CreateObject("Scripting.Dictionary")
Set ProfileSettingsDict = CreateObject("Scripting.Dictionary")
Set UserDict = CreateObject("Scripting.Dictionary")
Set fso = CreateObject("Scripting.FileSystemObject")

If WScript.Arguments.Count <> 2 Then 
  WScript.Echo "Usage: TACACSReport inFileName, EmailBodyFileName"
  WScript.Quit
End If

inFileName = WScript.Arguments(0)
EmailBodyFileName = WScript.Arguments(1)

If fso.FileExists(inFileName) = False Then
	wscript.echo "File " & infilename & " does not exists."
	wscript.quit
End If

If fso.FileExists(EmailBodyFileName) = False Then
	wscript.echo "File " & EmailBodyFileName & " does not exists."
	wscript.quit
End If

strparts = split(infilename,"\")
For i =0 to UBound(strparts) - 1
	path = path & strparts(i) & "\"
Next 

Useroutfilename = path & "TACACSUsers.csv"
Groupoutfilename = path & "TACACSGroup.txt"

Set inFileObj = fso.opentextfile(inFileName)


While not infileobj.atendofstream
	strLine = Trim(inFileObj.readline)
	If strline = "##--- Values End" Then
		If Left(strname,10) = "###profile" Then
			ProfileSettingsDict.add profilenum, SettingsArray
		End If 
		If strname <> "" and Left(strname,3) <> "###" and strname <> "CSMonRad" and strname <> "CSMonTac" and strname <> "system" Then
			userdict.add strname,userarray
		End If 	
		strName = ""
		ProfileNum = ""
		SettingsArray(0) = ""
		SettingsArray(1) = ""
		SettingsArray(2) = ""
		userarray(0) = ""
		userarray(1) = ""
		userarray(2) = ""
		userarray(3) = ""
		userarray(4) = ""
	End If 
	If strline <> "" Then
		strparts = split(strline,vbtab)
		If UBound(strparts) > 2 Then
			If strparts(0) = "App01" and Left(strparts(1),7) = "PROFILE" Then
				ProfileNum = CInt(Right(strparts(1),Len(strparts(1))-7))
				Profile = strparts(3)
				ProfilePermDict.add ProfileNum, Profile
			End If 
			If strparts(0) = "App03" and Left(strparts(1),10) = "PROFMAP-T-" Then
				ProfileNum = CInt(Right(strparts(1),Len(strparts(1))-10))
				Profile = strparts(3)
				ProfileNameDict.add ProfileNum, Profile
			End If 
		End If 
		If UBound(strparts) > 0 Then 
			If strparts(0) = "Name          :" Then 
				strName = strparts(1)
				If Left(strparts(1),10) = "###profile" Then ProfileNum = CInt(Right(strparts(1),Len(strparts(1))-10))
			End If	
			If strparts(0) = "App01" and strparts(1) = "Filters\NAS\records" Then
				tmpstr = ""
				For i = 3 to UBound(strparts)
					tmpstr = tmpstr & strParts(i) & vbtab
				Next 
				SettingsArray(0) = Left(tmpstr,Len(tmpstr)-1)
			End If 
			If strparts(0) = "App01" and strparts(1) = "Filters\NAS\enabled" Then
				SettingsArray(1) = strparts(3)
			End If 
			If strparts(0) = "App01" and strparts(1) = "Filters\NAS\option" Then
				SettingsArray(2) = strparts(3)
			End If 
			If strname <> "" and Left(strname,3) <> "###" Then
				If strparts(0) = "Profile       :"  Then
					profilenum = CLng(strparts(1))
					userarray(0) = profilenum
				End If 
				If strparts(1) = "USER_DEFINED_FIELD_0" and strparts(0) = "App00" Then
					userarray(1) = strparts(3)
				End If 
				If strparts(1) = "USER_DEFINED_FIELD_1" and strparts(0) = "App00" Then
					userarray(2) = strparts(3)
				End If 
				If strparts(1) = "USER_DEFINED_FIELD_2" and strparts(0) = "App00" Then
					userarray(3) = strparts(3)
				End If 								
				If strparts(1) = "USER_DEFINED_FIELD_3" and strparts(0) = "App00" Then
					userarray(4) = strparts(3)
				End If 								
			End If 
		End If 		
	End If 
Wend 
inFileObj.close

'generate users report
Set outFileObj = fso.createtextfile(userOutFileName)
DictKeys = userdict.Keys
outstr = "username, group, real name, property, UTS, approver, HWGroup"
'wscript.echo outstr
outfileobj.writeline outstr
For i = 0 to userdict.count - 1 
	tmpstr = userdict.item(DictKeys(i))
	tmpstr2 = ProfileSettingsDict.item(tmpstr(0))
	outstr = DictKeys(i) & ", " & ProfileNameDict.item(tmpstr(0)) & ", " & tmpstr(1) & ", " & tmpstr(2) & ", " & tmpstr(3) & ", " & tmpstr(4) & ", " & tmpstr2(0)
	'wscript.echo outstr
	outfileobj.writeline outstr
Next
outFileObj.close


'generate users report
Set outFileObj = fso.createtextfile(groupOutFileName)
DictKeys = ProfileSettingsDict.Keys

For i = 0 to ProfileSettingsDict.count - 1 
	tmpstr = ProfileSettingsDict.item(DictKeys(i))
	If tmpstr(1) <> "" Then
		outstr = String(60,"-") & vbcrlf & "USER GROUP NAME: " & ProfileNameDict.item(DictKeys(i)) & vbcrlf 
		outstr = outstr & "Associated Hardware Group: " & tmpstr(0) & vbcrlf
		outstr = outstr & "Enable: " & tmpstr(1)  & vbcrlf
		outstr = outstr & "Option:" & tmpstr(2) & vbcrlf
		outstr = outstr & "Commands: "
		perms = ProfilePermDict.item(dictkeys(i))
		perms = replace(perms,"{", vbcrlf & vbtab)
		perms = replace(perms,"}","")
		perms = replace(perms,"default", vbcrlf & "default")
		perms = replace(perms,"cmd", vbcrlf & "cmd")
		perms = replace(perms,"  ", vbcrlf & vbtab & vbtab)
		While InStr(perms,vbcrlf&vbtab&vbcrlf) > 0 
		perms = replace(perms,vbcrlf&vbtab&vbcrlf,vbcrlf)
		Wend
		While InStr(perms,vbcrlf&vbtab&vbtab&vbcrlf) > 0 
		perms = replace(perms,vbcrlf&vbtab&vbtab&vbcrlf,vbcrlf)
		Wend 
		'outstr = outstr & Right(perms,Len(perms)-2)
		outstr = outstr & perms
		'wscript.echo outstr
		outfileobj.writeline outstr
	End If		
Next
ouTfileobj.writeline String(60,"-")
'wscript.echo String(60,"-")
outFileObj.close

Set inFileObj = fso.opentextfile(EmailBodyFileName)
tmpstr = infileobj.readall
inFileObj.close

Set inFileObj = nothing
Set outFileObj = nothing
Set fso = nothing

wscript.echo "DONE PARSING!!!"

'mysendmail subject,tmpstr
'wscript.echo "Mail sent"


Sub MySendMail(StrSubject,msg)
	Dim iMsg,iConf,Flds
	
	Set iMsg = CreateObject("CDO.Message") 
	Set iConf = CreateObject("CDO.Configuration") 
	Set Flds = iConf.Fields 
	
	With Flds 
	  .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort 
	  .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = MailServerName 
	  .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = SMTPTimeout
	  .item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")= cdoNTLM
	  .Update 
	End With 
	
	With iMsg 
	  Set .Configuration = iConf 
	      .To       = ToAddress
	      .CC	    = CCAddress
	      .bcc      = BCCAddress
	      .From     = FromAddress 
	      .Subject  = StrSubject 
	      .textbody = Msg
	      .AddAttachment(Useroutfilename)
	      .AddAttachment(Groupoutfilename)
	      .Send          
	End With
End Sub