Friday, December 01, 2006

Exchange SMTP Log file DNS Test tool Powershell script

This powershell script is a combination of some of my past scripts (in particular the DNS util script) wrapped up in a nice little GUI with buttons to make it easy to use on a daily basis. What this script does is allows you to open a SMTP log file (or if you don’t want to read the whole log file just a certain number of lines from a file) and it will then parse that log file into a DataGrid on a Winform. You can then select a line in the datagrid and use one of the Buttons provided to perform different DNS test on that log file entry. The tests it can do are

Reverse DNS query on the Source Mail server IP Address (from the log file)

MX lookup of the parsed domain name from a RCPT or MAIL SMTP command

SPF lookup of the parsed domain name from a RCPT or MAIL SMTP command

A Record lookup of the parsed domain name from a RCPT or MAIL SMTP command

RBL lookup of Source Mail Server IP Address (Using SORB’s or any other RBL you like). To configure which RBL server to use you need to modify the following line in the RBLLookup() function.

$RBLService = ""

Helo Banner Check does a connection to the Mail server source IP address and reports on the banner. To do this it creates a connection on Port 25 of the source mail server IP address then reads the response and then issues a Quit to end the connection.

Whois Lookup of the Source Mail Server IP Address using (or any other whois server you want to configure). This will only return results for IP’s and domains this whois server is authorative for or it will return a referral to the whois server you should be using.

I’ve created two versions of the script the second version does Geolocationing of the Source Mail server IP address and gives the option to resolve every IP address in the log file to a geographical location or just do single queries on the location of certain ip addresses. This is based on my previous post of Geolocationing Message tracking logs .The reason for creating two versions is that the second script is quire complex and blows out to the size to around 800 lines so I wanted to have a simple script for anyone who doesn’t care about geolocatiion. Personally I think it’s really useful if you’re trying to diagnose something with a SMTP log file.

The parsing section of the script uses the get-content commandlet to read the log file you select this commandlet allows you to pass in the number of lines you want it to read from a file or pass -1 and it will read everything. The one thing I wasn’t 100 % sure about is the file locking operation of this commandlet. If it does lock the file when its retrieving the content then this could be a bad thing if you where to open a live log file. If anyone knows the answer to this one please let me know I haven’t come across any issues thus far.

To use the script just run it from powershell and you should see it build and popup a Winfrom. Use the select file button which will popup a file browser to allow you to select the log file you want to use. To use the geolocation version please see my other post about where you need to obtain the download from the iptocountry.csv file and where to place it to make the script work correctly. If you’re using the Geolocation version and you want the ipaddress resolved to a county location make sure you select the Show Cnty check box.

I’ve put a download of this script here the whole script is a bit too large to post but here are some highlights.

function openLog{
$exFileName = new-object System.Windows.Forms.openFileDialog
$fnFileNamelableBox.Text = $exFileName.FileName
$tcountline = -1
if ($rbVeiwAllOnlyRadioButton.Checked -eq $true){$tcountline = $lnLogfileLineNum.value}
get-content $exFileName.FileName -totalCount $tcountline | %{
$linarr = $_.split(" ")
$lfDate = ""
$lfTime = ""
$lfSourceIP = ""
$lfHostName = ""
$lfDestIP = ""
$lfSMTPVerb = ""
$lfCommandText = ""
if ($linarr[0].substring(0, 1) -ne "#"){
if ($linarr.Length -gt 0){$lfDate = $linarr[0]}
if ($linarr.Length -gt 1){$lfTime = $linarr[1]}
if ($linarr.Length -gt 2){$lfSourceIP= $linarr[2]}
if ($linarr.Length -gt 3){$lfHostName = $linarr[3]}
if ($linarr.Length -gt 6){$lfDestIP = $linarr[6]}
if ($linarr.Length -gt 8){$lfSMTPVerb = $linarr[8]}
if ($linarr.Length -gt 10){$lfCommandText = $linarr[10]}

$dgDataGrid.DataSource = $logTable

Compile-Csharp $code
$form = new-object System.Windows.Forms.form
$form.Text = "SMTP Log Test Tool"
$Dataset = New-Object System.Data.DataSet
$logTable = New-Object System.Data.DataTable
$logTable.TableName = "SMTPLogs"

# Content
$cmClickMenu = new-object System.Windows.Forms.ContextMenuStrip

# Add Open Log file Button

$olButton = new-object System.Windows.Forms.Button
$olButton.Location = new-object System.Drawing.Size(20,19)
$olButton.Size = new-object System.Drawing.Size(75,23)
$olButton.Text = "Select file"

# Add Reverse DNS Lookup Button

$rdnsbutton = new-object System.Windows.Forms.Button
$rdnsbutton.Location = new-object System.Drawing.Size(500,19)
$rdnsbutton.Size = new-object System.Drawing.Size(85,23)
$rdnsbutton.Text = "Reverse DNS"

# Add MX Lookup Button

$mxbutton = new-object System.Windows.Forms.Button
$mxbutton.Location = new-object System.Drawing.Size(500,44)
$mxbutton.Size = new-object System.Drawing.Size(85,23)
$mxbutton.Text = "MX Lookup"

# Add SPF Lookup Button

$spfbutton = new-object System.Windows.Forms.Button
$spfbutton.Location = new-object System.Drawing.Size(590,19)
$spfbutton.Size = new-object System.Drawing.Size(85,23)
$spfbutton.Text = "SPF Lookup"

# Add A Lookup Button

$Abutton = new-object System.Windows.Forms.Button
$Abutton.Location = new-object System.Drawing.Size(590,44)
$Abutton.Size = new-object System.Drawing.Size(85,23)
$Abutton.Text = "A Rec Lookup"

# Add RBL Single Lookup Button

$RBLButton = new-object System.Windows.Forms.Button
$RBLButton.Location = new-object System.Drawing.Size(680,19)
$RBLButton.Size = new-object System.Drawing.Size(85,23)
$RBLButton.Text = "RBL Lookup"

# Add HELO chk Button

$HelochkButton = new-object System.Windows.Forms.Button
$HelochkButton.Location = new-object System.Drawing.Size(680,44)
$HelochkButton.Size = new-object System.Drawing.Size(85,23)
$HelochkButton.Text = "HELO Banner Check"

# Add WhoIS chk Button

$Whois = new-object System.Windows.Forms.Button
$Whois.Location = new-object System.Drawing.Size(770,19)
$Whois.Size = new-object System.Drawing.Size(85,23)
$Whois.Text = "Whois Check"

# Add FileName Lable
$fnFileNamelableBox = new-object System.Windows.Forms.Label
$fnFileNamelableBox.Location = new-object System.Drawing.Size(110,25)
$fnFileNamelableBox.forecolor = "MenuHighlight"
$fnFileNamelableBox.size = new-object System.Drawing.Size(200,20)

# Add Veiw RadioButtons
$rbVeiwAllRadioButton = new-object System.Windows.Forms.RadioButton
$rbVeiwAllRadioButton.Location = new-object System.Drawing.Size(310,19)
$rbVeiwAllRadioButton.size = new-object System.Drawing.Size(69,17)
$rbVeiwAllRadioButton.Checked = $true
$rbVeiwAllRadioButton.Text = "View All"
$rbVeiwAllRadioButton.Add_Click({if ($rbVeiwAllRadioButton.Checked -eq $true){$lnLogfileLineNum.Enabled = $false}})

$rbVeiwAllOnlyRadioButton = new-object System.Windows.Forms.RadioButton
$rbVeiwAllOnlyRadioButton.Location = new-object System.Drawing.Size(310,42)
$rbVeiwAllOnlyRadioButton.size = new-object System.Drawing.Size(89,17)
$rbVeiwAllOnlyRadioButton.Text = "View Only #"
$rbVeiwAllOnlyRadioButton.Add_Click({if ($rbVeiwAllOnlyRadioButton.Checked -eq $true){$lnLogfileLineNum.Enabled = $true}})

# Add Numeric log line number
$lnLogfileLineNum = new-object System.Windows.Forms.numericUpDown
$lnLogfileLineNum.Location = new-object System.Drawing.Size(401,39)
$lnLogfileLineNum.Size = new-object System.Drawing.Size(69,20)
$lnLogfileLineNum.Enabled = $false
$lnLogfileLineNum.Maximum = 10000000000

# File setting Group Box

$OfGbox = new-object System.Windows.Forms.GroupBox
$OfGbox.Location = new-object System.Drawing.Size(12,0)
$OfGbox.Size = new-object System.Drawing.Size(464,75)
$OfGbox.Text = "Log File Settings"

# Add DataGrid View

$dgDataGrid = new-object
$dgDataGrid.AllowSorting = $True
$dgDataGrid.Location = new-object System.Drawing.Size(12,81)
$dgDataGrid.size = new-object System.Drawing.Size(1024,750)

$form.topmost = $true