Click here to Skip to main content
15,884,388 members
Articles / Web Development / ASP.NET

IP Logger

Rate me:
Please Sign up or sign in to vote.
3.31/5 (8 votes)
15 Feb 2009CPOL1 min read 40.6K   287   31   3
This program keeps track of your changing IP number.

Introduction

My Internet service provider gives me a static IP number but constantly changes it. This makes it difficult to remotely access my home PC from work. This simple program is designed to solve the problem of keeping track of your changing IP number.

Using the Code

This program consists of two components:

  1. An ASP page (IpLogger.aspx) that logs your current IP into a SQL database. This page will also show your current IP. The page has to be deployed to a publicly accessible server. The code behind IpLogger.aspx.vb page has a function called GetConnectionString() that points to the database where the IP log information is stored. The database can be created using this file: Schema.sql.
  2. A Windows script file. The script will access the ASP page periodically. It has to be scheduled with Windows Scheduler on your home PC. The script file uses the following URL to save the IP: http://<myservername>/MyServer/IpLogger.aspx?server=server1&save=1.

To view your current IP address and the history, please open this page in your browser: http://<myservername>/MyServer/IpLogger.aspx?server=server1.

Database Schema

SQL
CREATE DATABASE IpLogger
GO

USE IpLogger
GO

CREATE TABLE [dbo].[IpLog] (
    [LogId] [int] IDENTITY (1, 1) NOT NULL PRIMARY KEY  CLUSTERED,
    [IpAddress] [varchar] (50) NOT NULL ,
    [ServerName] [varchar] (50) NULL ,
    [AccessTime] [datetime] NOT NULL DEFAULT (getdate())
) ON [PRIMARY]
GO

Code Behind IpLogger.aspx.vb Page

VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
	Handles Me.Load

    If Request.QueryString("save") <> "" Then
        SaveIP(Request.QueryString("server"))
        Response.Write("OK")
        Response.End()
    ElseIf Request.QueryString("delete") <> "" Then
        DeleteServer(Request.QueryString("server"))
        Response.Write("OK")
        Response.End()
    End If

    Dim sSql As String = "SELECT IpAddress, MIN(AccessTime) AS _
			EffectiveOn FROM IpLog " & _
                         " WHERE ServerName = '" & 
			Request.QueryString("server") & "'" & _
                         " GROUP BY IpAddress ORDER BY IpAddress DESC"
    GridView1.DataSource = GetDataReader(sSql)
    GridView1.DataBind()

End Sub

Private Sub DeleteServer(ByVal sServerName As String)
    Dim sSql As String = "DELETE FROM IpLog WHERE ServerName = '" & sServerName & "'"
    ExecuteCommand(sSql)
End Sub

Private Sub SaveIP(ByVal sServerName As String)
    Dim sId As String = Request.ServerVariables("remote_addr")
    Dim sSql As String = "INSERT INTO IpLog _
	(IpAddress, ServerName) VALUES ('" & sId & "', '" & sServerName & "')"
    ExecuteCommand(sSql)
End Sub

Private Function GetDataReader(ByVal sSql As String) As OleDbDataReader
    Dim sConnection As String = GetConnectionString() 
    Dim cn As New OleDbConnection(sConnection)
    Dim cmd As New OleDbCommand(sSql, cn)
    cn.Open()
    Try
        Return cmd.ExecuteReader(Data.CommandBehavior.CloseConnection)
    Catch ex As Exception
        Throw New Exception(ex.Message & "

SQL: " & Replace(sSql, vbCrLf, "
"))
    End Try
End Function

Private Sub ExecuteCommand(ByVal sSql As String)
    Dim cn As New OleDbConnection(GetConnectionString())
    Dim cmd As New OleDbCommand(sSql, cn)
    cn.Open()

    Try
        cmd.ExecuteNonQuery()
    Catch ex As Exception
        Throw New Exception(ex.Message & "; SQL: " & sSql)
    End Try

    cn.Close()
End Sub

Private Function GetConnectionString() As String
    Return "Provider=SQLOLEDB.1;Password=xxxxx;_
	Persist Security Info=True;User ID=sa;Initial Catalog=IpLogger;Data Source=."
End Function

Windows Script File: LogIP.vbs

VB.NET
'To View IP History in browser http://MyServer/IpLogger/Default.aspx?server=server1

'To Save IP
GetUrlData "http://MyServer/IpLogger/Default.aspx?server=server1&save=1"

Function GetUrlData(sUrl)
    Dim oHttp
    Set oHttp = CreateObject("MSXML2.ServerXMLHTTP")
    oHttp.setTimeouts 0, 0, 0, 0
    
    oHttp.Open "GET", sUrl, False
    oHttp.send
    GetUrlData = oHttp.responseText
    Set oHttp = Nothing
End Function

History

  • 15th February, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Igor is a business intelligence consultant working in Tampa, Florida. He has a BS in Finance from University of South Carolina and Masters in Information Management System from University of South Florida. He also has following professional certifications: MCSD, MCDBA, MCAD.

Comments and Discussions

 
GeneralDNS/DHCP Pin
thund3rstruck16-Feb-09 5:45
thund3rstruck16-Feb-09 5:45 
GeneralRe: DNS/DHCP Pin
Aoi Karasu 13-Nov-09 2:04
professional Aoi Karasu 13-Nov-09 2:04 
"My Internet service provider gives me a static IP number but constantly changes it."

That's the infamous third type of IP Address assignation: quasi-dynamic IP... or perhaps quasi-static IP is the better name - your choice Laugh | :laugh:
GeneralDynDNS Pin
marco_br16-Feb-09 5:25
marco_br16-Feb-09 5:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.