Introduction
I always come across situations where I will be outside home and may need to access my system remotely. So I can ask anybody at home to switch on the system through a call. Then I surely need my External IP Address to connect to my system where every body at home may not know to access the system and find the IP address through some websites. Then comes the need of an automated system which can send the IP Address to us without the need for any manual interaction. So I decide to develop a simple tool to send my IP address to me automatically every time the system starts up. IPMon is the tool designed in C# .NET which automatically checks for the IP Address and sends it through Email or SMS. This tool checks for the IP Address through the given link and sends it through SMS or Mail using the SMTP service.
Concept Behind It
As soon Windows starts up, the service named IPMonSvc
gets started and it runs the IPMon.exe from the installed path.
A method from MonitorService.cs:
protected override void OnStart(string[] args)
{
string Path = "";
using (RegistryKey Service = Registry.LocalMachine.OpenSubKey(
@"SYSTEM\CurrentControlSet\Services\IPMonitor"))
if (Service != null) Path = Service.GetValue("ImagePath").ToString();
Path = Path.Substring(0, Path.LastIndexOf('\\'));
if (Path.StartsWith("\"")) Path = Path.Substring(1);
Process.Start(Path + "\\IPMon.exe");
base.OnStart(args);
base.Stop();
}
The above code gets the path where the service is installed and appends the name of the application with the path and starts the application. Once our application is started, we don't have any need for the service, so calling base.OnStop()
as soon as the process is started will stop the current service whereas the running application (IPMon.exe) will continue to run.
A method from frmMain.cs - The main form which is always hidden but contains the notify icon.
string GetExternalIP()
{
string Return = string.Empty;
try
{
Stream ResponseStream = WebRequest.Create(
ApplicationVariables.CheckUrl).GetResponse().GetResponseStream();
byte[] IPByte = new byte[15];
ResponseStream.Read(IPByte, 0, 15);
Return = Encoding.ASCII.GetString(IPByte);
ResponseStream.Close();
ResponseStream = null;
}
catch { }
try
{
Return = Return.Trim();
if (Return.Split('.').Length != 4 || Convert.ToInt64(
Return.Replace(".", "")) == 0)
Return = "Unknown";
}
catch { Return = "Unknown"; }
return Return;
}
The above method is the major and important method of this project. This method creates a web request with the given URL (which must return only the IP Address as response) and returns the IP Address which is returned by the URL. In case no connection could be established or the server does not return a valid IP Address, then the method returns just a string "Unknown
".
This method is called by RecheckIP
method which will call the DoIntimate
method the first time the application is executed and in case if there are any changes in the IP last sent. The DoIntimate
method takes care of sending the mail or SMS according to the settings. All the settings are stored and retrieved from the ApplicationVariables
class which contains all the static
methods and properties.
A method from ApplicationVariables.cs:
internal static void ReadSettings(string Path)
{
SETTINGS_PATH = Path;
Settings = new XmlDocument();
if (File.Exists(Path))
{
Settings.Load(Path);
}
else { }
EmailSettings = Settings.DocumentElement.SelectSingleNode("SMTP");
}
This method reads the XML file from the application path and stores it in an XML document element within the class. This element is accessed by all the member variables to get corresponding values.
How To Use It?
First download and install the Setup from the link provided above, then restart your system. You will find a new icon in your system tray. Right click on the Tray Icon to display the context menu options as shown below:
In the above context menu, the usage of the options are as follows:
- Display IP Monitor - Enabling this option will display the small transparent window just above the Tray Icon which will display the IP Address of the system.
- Always On Top - Enabling this option will always display the transparent window at the top of other window.
- Recheck Periodically - Enabling this option will periodically recheck the IP Address and intimate in case of any changes.
- Notify On Changes - Enabling this option will send the IP Address through mail or SMS. You can stop sending the IP Address by just disabling this feature as soon as you login to your system.
- Check Now - This option will recheck the IP Address.
- Configurations - The submenus enable the configuration of the settings. All the changes made in step 1, 2,3 and 4 will be lost as soon as you exit the application or you shutdown. But the settings made using these menus will be persistent.
General Configuration
All the settings in this form are self explanatory and need not be explained in detail.
SMS Configuration
In the above window, type in the SMS URL which is used to send the SMS. Select the request method type. Currently I don't find any of the providers using the Post
methods and so it has been disabled. If your provider requires authenticated request, then enable the authentication and type in the user name and password. Then comes the request params which is sent to your provider. Enter all the param names and the corresponding values. In the message param, you can use the wild cards described below to mention the IP Address, etc.
Application Wild Cards
In application specific wild cards are used to replace those with the corresponding values as shown below:
- $MACHINENAME - Replaces with the name of the local machine
- $INTERNALIP - Replaces with the Internal IP Address (LAN IP)
- $EXTERNALIP - Replaces with the External IP (used outside the LAN)
- $DATE - Replaces with the system date in the specified format
Email Settings
The above window enables to send email notification using the SMTP service. All the fields in this window are general and self explanatory and so need not be explained in detail.
Note
- After the installation of the setup, the system needs to be restarted for the application to work properly.
- The setup link provided above also installs a service as the intimation of the IP must be sent as soon as the system is switched on.
- Manually installing the service may not work untill you enable user interaction. (So it is always better to install the application with the setup provided with the project)
- It also intimates whenever the IP gets changed and so in case the modem is reset, you can get the new IP instantly.
- The IP checking URL provided in the general settings page must return only the External IP. No parsing of the IP from the page is provided.
- All the settings are stored in a XML file without any encryption.
History
- 21st October, 2009: Initial post