Jump to content
Sign in to follow this  
Davarious

Email Script - IP Change Detector (working - need help optimizing)

Recommended Posts

Davarious

The I have used the code located at http://www.autoitscript.com/forum/index.ph...etSmtpMailCom()

What I have included is what I edited out of the code, because well - as my comments say I do not want to show people our password if there is an error etc. I also edited the first line because it seemed to ignore the variables for things such as SMTP port by declaring them - so the newbie in me removed them to be safe.

CODE
; Commented to Avoid a msg box that could display the compiled in password of our gmail account.

;If @error Then

; MsgBox(0, "Error sending message", "Error code:" & @error & " Rc:" & $rc)

;EndIf

;

Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Username = "", $s_Password = "",$IPPort="", $ssl="")

After I did that, I saved the file as SMTP-Auth.Au3 in the Autoit Include directory. I went and built the following program to install at our remote sites... (warning I comment a lot in my code! Call it newbie needing to know where he left off at the next shift...).

CODE
; This program will detect the IP address of the computer, and determine if it has changed since the last check.

; Using the email script from http://www.autoitscript.com/forum/index.ph...etSmtpMailCom()

#include <INET.au3>

#include <SMTP-Auth.au3>

#cs

These are the original referenced comments from the creator of the INetSmtpMailCom function

$s_SmtpServer = "MailServer" ; address for the smtp-server to use - REQUIRED

$s_FromName = "Name" ; name from who the email was sent

$s_FromAddress = "your@Email.Address.com" ; address from where the mail should come

$s_ToAddress = "your@Email.Address.com" ; destination address of the email - REQUIRED

$s_Subject = "Userinfo" ; subject from the email - can be anything you want it to be

$as_Body = "" ; the messagebody from the mail - can be left blank but then you get a blank mail

$s_AttachFiles = "" ; the file you want to attach- leave blank if not needed

$s_CcAddress = "CCadress1@test.com" ; address for cc - leave blank if not needed

$s_BccAddress = "BCCadress1@test.com" ; address for bcc - leave blank if not needed

$s_Username = "******" ; username for the account used from where the mail gets sent - Optional (Needed for eg GMail)

$s_Password = "********" ; password for the account used from where the mail gets sent - Optional (Needed for eg GMail)

$IPPort = 25 ; port used for sending the mail

$ssl = 0 ; enables/disables secure socket layer sending - put to 1 if using httpS

;~ $IPPort=465 ; GMAIL port used for sending the mail

;~ $ssl=1 ; GMAILenables/disables secure socket layer sending - put to 1 if using httpS

#ce

; Declare some variables we need to worry about, while not needed, it makes it easier to view what is happening, and understand all the values being used.

Dim $SMTPServer, $FromName, $FromAddy, $ToAddy, $Subject, $Body, $Attachment, $CCAddy, $BCCAddy, $SMTPUser, $SMTPPass, $SMTPPort, $SSL

; Some things are contstant and will not change, therefor we will set them now.

$SMTPServer = "*******" ; Future version will just encrypt this information in the INI file.

$SMTPUser = "*******" ; Future version will just encrypt this information in the INI file.

$SMTPPass = "**********" ; Future version will just encrypt this information in the INI file.

$SMTPPort = 465 ; This is gmail's smtp port.

$SSL = 1 ; I am using gmail and running on a remote network - therefor encryption is prefered.

$FromName = "Okuma Integrated Solutions" ; Hard coded to support what is coded in the SMTP Settings.

$FromAddy = "TRM@TSkinner.com" ; Hard coded to support what is coded in the SMTP Settings.

$ToAddy = IniRead("IPCheck.ini","CONF", "EMAIL", "TRM@TSkinner.com") ; We default to our group mailbox, unless otherwise indicated.

$CCAddy = "" ; The function asks for these, however we are not using them so they are declared blank here.

$BCCAddy = "" ; The function asks for these, however we are not using them so they are declared blank here.

; This leaves $Subject and $Body to be declared in the script.

; We are going to declare some simple variables to check for old and new addresses.

Dim $OldIP, $NewIP, $Change

$OldIP = IniRead("IPCheck.ini", "IP", "ADDY", "")

$NewIP = _GetIP()

$Client = IniRead("IPCheck.ini", "CONF", "CLIENT", "UNKNOWN")

If $NewIP = $OldIP Then

IniWrite("IPCheck.ini", "IP", "DATE", @MON & "-" & @MDAY & "-" & @YEAR)

$Change = 0

Else

IniWrite("IPCheck.ini", "IP", "ADDY", $NewIP)

IniWrite("IPCheck.ini", "IP", "DATE", @MON & "-" & @MDAY & "-" & @YEAR)

$Change = 1

EndIf

If $Change = 0 Then

Exit

ElseIf $Change = 1 Then

$Subject = "IP Address Changed @ " & $Client & " (" & $NewIP & ")"

$Body = "IP Address Changed @ " & $Client & " (" & $NewIP & ")"

_INetSmtpMailCom($SMTPServer, $FromName, $FromAddy, $ToAddy, $Subject, $Body, $Attachment, $CCAddy, $BCCAddy, $SMTPUser, $SMTPPass, $SMTPPort, $SSL)

EndIf

The INI File I am using looks like this:

CODE
[CONF]

EMAIL=Blah@Blah.com

CLIENT=Sample Customer

[iP]

ADDY=10.10.10.10

DATE=01-10-2008

(Date is used to let me know things are still being checked, if I ever remote in and open the file I will know when the check was last run (as this will be scheduled as a windows task)

When I test it - I get an email. It works flawlessly when I double click the file - however when I use the Tools>Beta Run from SCITE, I get the following error...

Keep in mind, it still sends the email. (To ensure I get an email each time, I just edit the IP in the INI file to make it different and thus the check sends me the results..

CODE
>"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /beta /ErrorStdOut /in "C:\Users\jbattley\Desktop\IP Change Notifier.au3" /autoit3dir "C:\Program Files\AutoIt3\beta" /UserParams

+>11:54:51 Starting AutoIt3Wrapper v.1.9.3

>Running AU3Check (1.54.9.0) from:C:\Program Files\AutoIt3

+>11:54:51 AU3Check ended.rc:0

>Running:(3.2.8.1):C:\Program Files\AutoIt3\autoit3.exe "C:\Users\jbattley\Desktop\IP Change Notifier.au3"

### COM Error ! Number: 80020009 ScriptLine: 89 Description:The transport failed to connect to the server.

+>11:55:00 AutoIT3.exe ended.rc:0

+>11:55:00 AutoIt3Wrapper Finished

>Exit code: 0 Time: 9.732

Share this post


Link to post
Share on other sites
DirtDBaK

If you want your IP to be static not dynamic then you should sign up for www.DynDNS.com they gave me a name for my ip...its free.

http://dbak.mine.nu

is the same as

http://65.31.39.43

I am not sure if you want a static server or client. Could you tell me which...

What I posted above is for a static server


[center][/center]

Share this post


Link to post
Share on other sites
Davarious

If you want your IP to be static not dynamic then you should sign up for www.DynDNS.com they gave me a name for my ip...its free.

http://dbak.mine.nu

is the same as

http://65.31.39.43

I am not sure if you want a static server or client. Could you tell me which...

What I posted above is for a static server

While DynDNS handles the changing addresses well of a dynamic host, it does not handle the volume of clients we have without needing a paid subscription, hence why a solution such as this seems to work well for me.

The script works, I just get the error I posted at the end. Between 6-9 seconds run time each time I do it is not awful considering it is relying on external services, but the error concerns me that I may get failures and not receive updates from a client.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.