Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

VBScript for Sending IP Information to an E-mail Address

0.00/5 (No votes)
6 Oct 2007 1  
VBScript for sending IP information to an e-mail address

Introduction

At home, I have a PPPoE connection and the IP address is changing almost every time. I am usually in the need to connect to my home computer from external locations, but I cannot setup a fixed remote desktop connection since I don't have a fixed IP. Also, I need the connection to be dialled automatically when the computer starts, without the need of phone guidance to the other person.

This article contains two simple scripts that:

  1. Dial the connection, and 
  2. Send the IP information to an e-mail account for use

Using the Code

The VB script uses CDO for e-mail transport and WMI for IP retrieval. Since the script is commented and explanations of CDO and WMI methods are easy to find, here it is:

'
' send the IP address via Gmail 
'
Sub SendIPInfo()

  On Error Resume Next

  Dim iMsg, iConf, Flds

  Set iMsg = CreateObject("CDO.Message")
  Set iConf = CreateObject("CDO.Configuration")
  Set Flds = iConf.Fields

  schema = "http://schemas.microsoft.com/cdo/configuration/"

  Flds.Item(schema & "sendusing") = 2
  Flds.Item(schema & "smtpserver") = "smtp.gmail.com" 
  Flds.Item(schema & "smtpserverport") = 465
  Flds.Item(schema & "smtpauthenticate") = 1
  Flds.Item(schema & "sendusername") = "your-gmail-username@gmail.com"
  Flds.Item(schema & "sendpassword") = "your-gmail-password"
  Flds.Item(schema & "smtpusessl") = 1
  Flds.Update

  ' message body
  strDate = CStr(Date()) & " " & CStr(Time())
  strBody = "---===---<br/>IP info YOURCOMPUTER " & strDate & " <br/><br/>"

  ' read adapters info
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  Set colNicConfigs = objWMIService.ExecQuery _
   ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
  For Each objNicConfig In colNicConfigs
    strDescription = objNicConfig.Description
    IPAddress = objNicConfig.IPAddress
    strIP = ""
    For Each s in IPAddress
      if strIP = "" then
        strIP = s
      else
        strIP = strIP & ";" & s
      end if
    Next
    strBody = strBody & "<br/>" & _ 
      "Description:" & strDescription & "<br/>" & _ 
      "IP: " & strIP & "<br/>"
  Next
  strBody = strBody & "---===---" 

  With iMsg
    .To = "Sender Name <your-gmail-username@gmail.com>"
    .From = "Receiver Name <your-gmail-username@gmail.com>"
    .Subject = "IP info YOURCOMPUTER on: " & strDate
    .HTMLBody = strBody 
    .Sender = "Sender Name"
    .Organization = "Your Organisation"
    .ReplyTo = "your-gmail-username@gmail.com"
 
    Set .Configuration = iConf

    .Send 
  End With 
 
  ' release interfaces
  Set iMsg = nothing
  Set iConf = nothing
  Set Flds = nothing
End Sub  

SendIPInfo  

Just replace YOURCOMPUTER with your computer name, and your-gmail-username and your-gmail-password with the appropriate GMail identification.
Save the content of the script (or the attached ipsend.zip) on a location of choice (I am using C:\etc) and also secure the folder to grant access only to you (since the file contains the e-mail user name and password).

* * *

The other thing is how to automatically dialup the PPPoE connection at startup.

For this, it is enough to create a batch file, such as:

@echo off
if %1 EQU connect goto l_connect
if %1 EQU disconnect goto l_disconnect
goto l_unknown

:l_connect
rasdial CONN_NAME connUser connPassword
goto l_end

:l_disconnect
rasdial CONN_NAME /disconnect
goto l_end

:l_unknown
@echo Unknown option %1
goto l_end

:l_end

Replace the CONN_NAME with the name of connection as is listed in Network Connections, as well as connUser and connPassword with the identification from ISP. Again, save the file in a secure location (I'm using C:\etc\inet.bat) so it cannot be read or written except your account.

One more step is needed in order to make it automatically executed: execute gpedit.msc, go to Computer Configuration/Windows Settings/Scripts and double click the Startup. In Startup properties, click Add... and set the Script Name to C:\etc\inet.bat and Script Parameters to connect. (If you also need to disconnect on shutdown for some reason, double click Shutdown and set disconnect instead of connect on Script Parameters).

After adding the autodial script inet.bat, just repeat the same steps for adding the upper script, ipsend.vbs (after editing it). AFAIK, the startup scripts are executed sequentially so first will be the connection dialled and then the email will be sent. (For convenience, both .bat and the .vbs file can be called from a single windows-startup.bat file that does the dial and then the email job).

History

  • 6th October, 2007: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here