#131312 - 2004-12-17 11:42 PM
Re: MX Record lookup help
|
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11624
Loc: CA
|
Okay, here is a Sample Script for demonstration purposes ONLY! It is not and should not be considered useful for any Production Environment. It is only a concept of how something like this could be used.
I've only posted the code here for those that "might" have been interested in the final script usage.
Currently KiXtart does not natively support a valid method of scripting a TELNET session.
1. Script runs MXRecordLookup to find list of all available SMTP mail servers 2. Script gets the current IP of the workstation during logon. 3. Script gets Antivirus information [in real life would only send mail if some condition like old definitions found were met] 4. Script checks if highest priority server is available, if not it checks for the next one in the list. 5. Once first available server is found it sends the e-mail via BLAT to that server
Code:
Break On Dim $SO $SO=SetOption('Explicit','On') $SO=SetOption('NoVarsInStrings','On') $SO=SetOption('WrapAtEOL','On') $SO=SetConsole('hide') Dim $CurrentIP,$NAVInfo,$MailerID,$MailerSubject,$MailerBody,$MailerAlert,$Record,$counter,$MailServer Global $MailerServer $Record=MXRecordLookup('somesmtpmailserver.com','somednsserver.com') $CurrentIP=GetCurrentIP() $NAVInfo=GetNavAntiVirusInfo() $MailerID='"'+Join(Split(@Fullname,','),'')+'<'+@UserID+'@@somecompany.com>'+'"' $MailerSubject='"KAM: Old Definitions "' $MailerBody='"Please check antivirus definitions ' + @CRLF + 'Definitions dated: ' + $NAVInfo[1] + @CRLF + 'AV Engine: ' + $NAVInfo[0] + @CRLF + 'Computer Name: ' + @WKSTA + @CRLF + 'Current IP: ' + $CurrentIP + @CRLF + 'NIC address: ' + @Address + @CRLF + 'Domain: ' + @DOMAIN + @CRLF + 'User ID: ' + @USERID + @CRLF + 'User Name: ' + @Fullname +'"' While $counter<= UBound($Record) And $MailerServer="" If MailServerUp($Record[$counter]) $MailerServer=$Record[$counter] $MailerAlert=BlatMailerAlert('some.administrator@@somecompany.com',$MailerID,$MailerSubject,$MailerBody) EndIf $counter=$counter+1 Loop If $MailerServer="" ? 'Unable to connect to any server' EndIf Function BlatMailerAlert($Recipient,$Sender,$Subject,$Body,optional $Attachment) Dim $SMTPServer,$MailerLine $SMTPServer=$MailerServer $MailerLine=@LDrive+'\blat.exe -' + ' -to ' + $Recipient + ' -f ' + $Sender + ' -subject ' + $Subject + ' -body ' + $Body + ' -server ' + $SMTPServer + ' -q ' Shell $MailerLine EndFunction Function MXRecordLookup($ComputerToFind,optional $Server) Dim $mxrecords[0],$MX,$Line,$Counter,$record,$c $MX=WshPipe("nslookup -q=mx " + $ComputerToFind + " " + $Server,1) For Each $Line in $MX If InStr($Line,"MX preference") ReDim PreServe $mxrecords[$Counter] $mxrecords[$Counter]="" + Trim(Split(Split($Line,",")[0],"=")[1]) + ":" + Trim(Split(Split($line,",")[1],"=")[1]) $Counter = $Counter + 1 EndIf Next $MXRecords=QS($mxrecords) For each $record in $mxrecords $MXRecords[$c]=split($record,":")[1] $c=$c+1 Next $MXRecordLookup=$MXRecords EndFunction Function QS($a) DIM $b[32],$c[32],$d,$e,$f,$g,$h,$i,$j,$k,$l $b[0]=0 $c[0]=UBOUND($a) $d=0 While $d >=0 $e=$b[$d] $f=$c[$d] While $e < $f $h=$e+($f-$e)/2 $k=$a[$e] $A[$e]=$A[$h] $A[$h]=$k $i=$e+1 $j=$f $l=0 Do While ($i<$j) AND $A[$e] > $A[$i] $i=$i+1 Loop While ($j>=$i) AND $A[$j] > $A[$e] $j=$j-1 Loop IF $i>=$j $l=1 ELSE $k=$A[$i] $A[$i]=$A[$j] $A[$j]=$k $j=$j-1 $i=$i+1 ENDIF Until $l=1 $k=$a[$e] $a[$e]=$a[$j] $a[$j]=$k $g=$j If $g-$e <= $f - $g If $g+1 < $f $b[$d]=$g+1 $c[$d]=$f $d=$d+1 Endif $f=$g-1 Else If $g-1 > $e $b[$d]=$e $c[$d]=$g-1 $d=$d+1 Endif $e=$g+1 Endif Loop $d=$d-1 Loop $qs=$a EndFunction Function WshPipe($ShellCMD, OPTIONAL $NoEcho) Dim $oExec, $Output $oExec = CreateObject("WScript.Shell").Exec($ShellCMD) If Not VarType($oExec)=9 $WshPipe="WScript.Shell Exec Unsupported" Exit 10 EndIf $Output = $oExec.StdOut.ReadAll + $oExec.StdErr.ReadAll If Not $NoEcho $Output Endif $WshPipe=Split(Join(Split($Output,CHR(13)),''),CHR(10)) Exit($oExec.ExitCode) EndFunction Function MailServerUp($mailserver) Dim $output,$,$fh,$line,$path $output='%temp%\output.txt' If Exist($output) Del $output EndIf Run '%comspec% /c telnet -f ' + $output + ' ' + $mailserver + ' 25' Sleep 2 $=EndProc('telnet.exe') Sleep 1 $fh=FreeFileHandle() If Open($fh,$output)=0 $line=ReadLine($fh) While @ERROR=0 If Left($line,3)=220 $MailServerUp=1 EndIf $line=ReadLine($fh) Loop $=Close($fh) EndIf If Exist($output) Del $output EndIf EndFunction Function EndProc($proc, optional $strComputer) DIM $Process If $strComputer='' $strComputer='.' EndIf For Each $Process In GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $strComputer + "\root\cimv2").ExecQuery("Select * from Win32_Process where Name= " +'"'+$Proc+'"') $Process=$Process.Terminate Next EndFunction Function GetCurrentIP() Dim $TempFile, $Line, $cf $TempFile = '%TEMP%\PING.TXT' If Exist($TempFile) Del $TempFile EndIf Shell '%COMSPEC% /C PING -n 1 ' + @WKSTA + '>'+$TempFile If open(1, $TempFile) = 0 $Line = ReadLine(1) While @ERROR = 0 If InStr($Line,@WKSTA) $Line = SubStr($Line,InStr($Line,"[")+1) $GetCurrentIP = SubStr($Line,1,InStr($Line,"]")-1) EndIf $Line = ReadLine(1) Loop $cf = Close(1) EndIf If Exist($TempFile) Del $TempFile EndIf EndFunction Function GetNavAntiVirusInfo(optional $wksta,optional $DateCheck,optional $WarnOldDef,optional $WarnNoNav) Dim $Defdate,$CurDef,$NAVHome,$NavExecutable,$NavParent,$OldDefs,$AlertOldDef,$NAVPath Dim $ClientType,$ReadClientType,$RegPath,$FilePath,$CurWksta,$NAVInfoArray[9] Dim $AlertNoNAV,$NoNavWarn,$NoNavMsg,$Notify $CurWksta = @WKSTA If $wksta = "" $wksta = @WKSTA EndIf If $wksta = $CurWksta $RegPath = "" Else $RegPath = '\\'+$wksta+'\' EndIf If $wksta = $CurWksta $FilePath = "" Else $FilePath = '\\'+$wksta+'\c$' EndIf If $DateCheck = "" $DateCheck = 14 EndIf $ReadClientType = ReadValue($RegPath+'HKLM\SOFTWARE\INTEL\LANDesk\VirusProtect6\CurrentVersion', 'ClientType') $Defdate = Left(Right(ReadValue($RegPath+'HKLM\SOFTWARE\Symantec\SharedDefs','NAVCORP_70'),12),8) $Defdate = Left($Defdate,4) + '/' + SubStr($Defdate,5,2) + '/' + Right($Defdate,2) $CurDef = DateCalc(@DATE, $Defdate) $NoNavWarn = ExistKey($RegPath+'HKLM\SOFTWARE\INTEL\LANDesk\VirusProtect6\CurrentVersion\Quarantine') $NoNavMsg = 'Unable to locate the Symantec/Norton AntiVirus program on this system. '+'[ '+$wksta+' ]' $NoNavMsg = $NoNavMsg+@CRLF+'Please contact the Helpdesk to have the program installed Or checked For problems ' $NoNavMsg = $NoNavMsg+'with a previous installation.' $NoNavMsg = $NoNavMsg+@CRLF+@CRLF+'Helpdesk x555-1212' If $NoNavWarn <> 0 If $WarnNoNav <> 1 $AlertNoNAV = MessageBox($NoNavMsg,'AntiVirus Program Update', 16,) EndIf EndIf $NAVHome = ReadValue($RegPath+'HKLM\software\INTEL\LANDesk\VirusProtect6\CurrentVersion\', 'Home Directory') $NAVPath = $NAVHome If $wksta <> $CurWksta $NAVPath = SubStr($NAVHome,3,) $NAVPath = $FilePath+$NAVPath EndIf $NavExecutable = GetFileVersion($NAVPath +'\vpc32.exe','ProductVersion') $NavParent = ReadValue($RegPath+'HKLM\SOFTWARE\Intel\LANDesk\VirusProtect6\CurrentVersion','Parent') Select Case $ReadClientType = 1 $ClientType = 'Connected Client' Case $ReadClientType = 2 $ClientType = 'Stand-alone Client' Case $ReadClientType = 4 $ClientType = 'Server' If $NavParent = "" $ClientType = 'Primary Server' EndIf Case $ReadClientType = "" $ClientType = 'Unknown' EndSelect $OldDefs = 'Warning: Your ANTI-VIRUS definition files are dated ' $OldDefs = $OldDefs +$Defdate+' which makes them '+$CurDef +' days old.' $OldDefs = $OldDefs +@CRLF+@CRLF+'Your system '+'[ '+$wksta+' ]'+' is vulnerable to recent worms Or viruses.' $OldDefs = $OldDefs +@CRLF+@CRLF+'Please contact the Helpdesk to help determine why your system ' $OldDefs = $OldDefs +'is Not automatically updating.' $OldDefs = $OldDefs +@CRLF+@CRLF+'Helpdesk x555-1212 ' $OldDefs = $OldDefs +@CRLF+@CRLF+'Antivirus Engine version: '+$NavExecutable $OldDefs = $OldDefs +@CRLF+'Antivirus Definition date: '+$Defdate $OldDefs = $OldDefs +@CRLF+'Age of Definition Files: '+$CurDef $OldDefs = $OldDefs +@CRLF+'Antivirus Parent Server: '+$NavParent $OldDefs = $OldDefs +@CRLF+'Antivirus Client Type: '+$ClientType $OldDefs = $OldDefs +@CRLF+'Antivirus Installation Folder: '+$NAVHome If $CurDef > $DateCheck $Notify = 2 If $WarnOldDef <> 1 $AlertOldDef = MessageBox($OldDefs,'AntiVirus - Old Definitions', 48,) EndIf EndIf $NAVInfoArray[0]=$NavExecutable $NAVInfoArray[1]=$Defdate $NAVInfoArray[2]=$NavParent $NAVInfoArray[3]=$ClientType $NAVInfoArray[4]=$NAVHome $NAVInfoArray[5]=$wksta $NAVInfoArray[6]=$NoNavWarn $NAVInfoArray[7]=$Notify $NAVInfoArray[8]=$CurDef $GetNavAntiVirusInfo=$NAVInfoArray EndFunction Function DateCalc($date1, $DateOrMod) Dim $_intDate1, $_intYear1, $_intMonth1, $_intDay1 Dim $_intDate2, $_intYear2, $_intMonth2, $_intDay2 If InStr ($date1,'/') <> 5 Or InStrRev ($date1,'/') <> 8 Or Len ($date1) <> 10 Exit (1) EndIf $_intYear1 = Val(SubStr($date1,1,4)) $_intMonth1 = Val(SubStr($date1,InStr($date1,'/')+1,2)) $_intDay1 = Val(SubStr($date1,InStrRev($date1,'/')+1,2)) If $_intMonth1 < 3 $_intMonth1 = $_intMonth1 + 12 $_intYear1 = $_intYear1 - 1 EndIf $_intDate1 = $_intDay1 + ( 153 * $_intMonth1 - 457 ) / 5 + 365 * $_intYear1 + $_intYear1 / 4 - $_intYear1 / 100 + $_intYear1 / 400 - 306 Select Case VarType($DateOrMod) = 3 $_intDate2 = $_intDate1 + $DateOrMod If InStr($_intDate2,'-') $_intDate2 = Val(SubStr($_intDate2,2,Len($_intDate2)-1)) EndIf $_intYear2 = ( 100 * ( ( ( 100*($_intDate2+306)-25)/3652425) - ( ((100*($_intDate2+306)-25)/3652425)/4) ) + (100*($_intDate2+306)-25) ) / 36525 $_intMonth2 = ( 5 * ( ( ( 100*($_intDate2+306)-25)/3652425) - ( ((100*($_intDate2+306)-25)/3652425)/4) + ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4 ) + 456 ) / 153 $_intDay2 = ( ( ( 100*($_intDate2+306)-25)/3652425) - ( ((100*($_intDate2+306)-25)/3652425)/4) + ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4 ) - ( 153 * $_intMonth2 - 457 ) / 5 If $_intMonth2 > 12 $_intYear2 = $_intYear2 + 1 $_intMonth2 = $_intMonth2 - 12 EndIf If Len($_intYear2 ) < 4 $_ = Execute("For $i=1 to 4-Len($$_intYear2) $$_intYear2 = '0' + $$_intYear2 Next") EndIf If Len($_intMonth2) < 2 $_intMonth2 = "0$_intMonth2" EndIf If Len($_intDay2 ) < 2 $_intDay2 = "0$_intDay2" EndIf $DateCalc = '$_intYear2/$_intMonth2/$_intDay2' Case VarType($DateOrMod) = 8 If InStr ($DateOrMod,'/') <> 5 Or InStrRev ($DateOrMod,'/') <> 8 Or Len ($DateOrMod) <> 10 Exit (1) EndIf $_intYear2 = Val(SubStr($DateOrMod,1,4)) $_intMonth2 = Val(SubStr($DateOrMod,InStr($DateOrMod,'/')+1,2)) $_intDay2 = Val(SubStr($DateOrMod,InStrRev($DateOrMod,'/')+1,2)) If $_intMonth2 < 3 $_intMonth2 = $_intMonth2 + 12 $_intYear2 = $_intYear2 - 1 EndIf $_intDate2 = $_intDay2 + ( 153 * $_intMonth2 - 457 ) / 5 + 365 * $_intYear2 + $_intYear2 / 4 - $_intYear2 / 100 + $_intYear2 / 400 - 306
$DateCalc = $_intDate1 - $_intDate2 ;comment the Next line If you wish to return negative results also !!! If InStr($DateCalc,'-') $DateCalc = Val(SubStr($DateCalc,2,Len($DateCalc)-1)) EndIf Case 1 Exit (1) EndSelect EndFunction
|
Top
|
|
|
|
#131323 - 2004-12-20 05:42 PM
Re: MX Record lookup help
|
Chris S.
MM club member
Registered: 2002-03-18
Posts: 2368
Loc: Earth
|
Quote:
I think Ruud should come-out with a .net version of Kix (or a version of Kix that can be used with .net (subtle diff)), keep the legacy support ... then all this talk about what should and shouldn't go into Kix will be a mute point ... EVERYTHING will be available from Kix.
Love your idea, Shawn.
Pet peeve time. It's moot not mute. Other than that, I think you're a genius.
|
Top
|
|
|
|
#131325 - 2004-12-20 08:49 PM
Re: MX Record lookup help
|
Co
MM club member
Registered: 2000-11-20
Posts: 1341
Loc: NL
|
|
Top
|
|
|
|
#131326 - 2004-12-20 09:33 PM
Re: MX Record lookup help
|
Co
MM club member
Registered: 2000-11-20
Posts: 1341
Loc: NL
|
|
Top
|
|
|
|
#131329 - 2004-12-21 12:55 AM
Re: MX Record lookup help
|
Co
MM club member
Registered: 2000-11-20
Posts: 1341
Loc: NL
|
|
Top
|
|
|
|
Moderator: Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 920 anonymous users online.
|
|
|