How to print IP address of your Router or your workstation using .NET

A

Anonieko Ramos

I found this code from ElreyRonald. It is a console but
works by referencing another website.




//--------------------------------------------------------------
// A console application to print IP Address of the current
// workstation (or its router's IP )
//
// Two things are demonstrated here:
// (1) How to get a web page html and
// (2) How to use regular expression to search within a string.
//
// Written by ElreyRonald V. [[email protected]] 2004
//--------------------------------------------------------------


using System;
using System.Text.RegularExpressions;
using System.Text;
using System.Net;
using System.IO;


namespace Network.Utilities
{

class GetIp
{
[STAThread]
static void Main(string[] args)
{
// use a website which displays your IP
string urlString = "http://www.getmyipaddress.com";

// Fetch the html text of the website.
string htmlText = GetPageHtml(urlString);

// Search for the IP within that page.
string regExpr = @".*>(\d+.\d+.\d+.\d+)</*";
Regex rex = new Regex( regExpr, RegexOptions.None);
if (rex.Match( htmlText).Success )
{
string ip = rex.Match( htmlText).Result("$1").Trim();
Console.WriteLine(ip);
}

}

//------------------------------------------------------
// Get the HTML text of a give URL
//------------------------------------------------------
private static String GetPageHtml(string url)
{
StringBuilder sb = new StringBuilder();

try
{

WebRequest myRequest = WebRequest.Create(url);

// Return the response.
WebResponse myHttpWebResponse = myRequest.GetResponse();

// Gets the stream associated with the response.
Stream receiveStream =
myHttpWebResponse.GetResponseStream();
Encoding encode =
System.Text.Encoding.GetEncoding("utf-8");

// Pipes the stream to a higher level stream reader with
the required encoding format.
StreamReader readStream = new StreamReader( receiveStream,
encode );

Char[] read = new Char[256];

// Reads 256 characters at a time.
int count = readStream.Read( read, 0, 256 );
while (count > 0)
{
// Dumps the 256 characters on a string and displays
the string to the console.
String str = new String(read, 0, count);
//Console.Write(str);
sb.Append(str);
count = readStream.Read(read, 0, 256);
}

// Releases the resources of the response.
myHttpWebResponse.Close();

// Releases the resources of the Stream.
readStream.Close();

}
catch ( Exception )
{
return "";
}

String strHtml = sb.ToString();
return strHtml.ToString();
}
}
}
 
R

Ram Dash

I got this from http://www.dotnet247.com/247reference/msgs/1/7651.aspx. Here
is it:

using System;

using System.Net;

namespace ConsoleApplication1

{

/// <summary>

/// Summary description for Class1.

/// </summary>

class Class1

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

string host;

host = Dns.GetHostName();

Console.WriteLine(host);

IPHostEntry i = Dns.GetHostByName(host);

foreach (IPAddress a in i.AddressList)

{

Console.WriteLine(a);

}

Console.ReadLine();

}

}

}

Anonieko Ramos said:
I found this code from ElreyRonald. It is a console but
works by referencing another website.




//--------------------------------------------------------------
// A console application to print IP Address of the current
// workstation (or its router's IP )
//
// Two things are demonstrated here:
// (1) How to get a web page html and
// (2) How to use regular expression to search within a string.
//
// Written by ElreyRonald V. [[email protected]] 2004
//--------------------------------------------------------------


using System;
using System.Text.RegularExpressions;
using System.Text;
using System.Net;
using System.IO;


namespace Network.Utilities
{

class GetIp
{
[STAThread]
static void Main(string[] args)
{
// use a website which displays your IP
string urlString = "http://www.getmyipaddress.com";

// Fetch the html text of the website.
string htmlText = GetPageHtml(urlString);

// Search for the IP within that page.
string regExpr = @".*>(\d+.\d+.\d+.\d+)</*";
Regex rex = new Regex( regExpr, RegexOptions.None);
if (rex.Match( htmlText).Success )
{
string ip = rex.Match( htmlText).Result("$1").Trim();
Console.WriteLine(ip);
}

}

//------------------------------------------------------
// Get the HTML text of a give URL
//------------------------------------------------------
private static String GetPageHtml(string url)
{
StringBuilder sb = new StringBuilder();

try
{

WebRequest myRequest = WebRequest.Create(url);

// Return the response.
WebResponse myHttpWebResponse = myRequest.GetResponse();

// Gets the stream associated with the response.
Stream receiveStream =
myHttpWebResponse.GetResponseStream();
Encoding encode =
System.Text.Encoding.GetEncoding("utf-8");

// Pipes the stream to a higher level stream reader with
the required encoding format.
StreamReader readStream = new StreamReader( receiveStream,
encode );

Char[] read = new Char[256];

// Reads 256 characters at a time.
int count = readStream.Read( read, 0, 256 );
while (count > 0)
{
// Dumps the 256 characters on a string and displays
the string to the console.
String str = new String(read, 0, count);
//Console.Write(str);
sb.Append(str);
count = readStream.Read(read, 0, 256);
}

// Releases the resources of the response.
myHttpWebResponse.Close();

// Releases the resources of the Stream.
readStream.Close();

}
catch ( Exception )
{
return "";
}

String strHtml = sb.ToString();
return strHtml.ToString();
}
}
}
 
A

Anonieko Ramos

Ram,
If you are in an intranet, this program only shows your
intranet IP address (e.g. 192.168.xxx.xxx ). I tried
both programs. The solution by ElreyRonald will correctly
print the ROUTER address ( bec. it is the IP as 'seen from outside')




Ram Dash said:
I got this from http://www.dotnet247.com/247reference/msgs/1/7651.aspx. Here
is it:

using System;

using System.Net;

namespace ConsoleApplication1

{

/// <summary>

/// Summary description for Class1.

/// </summary>

class Class1

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

string host;

host = Dns.GetHostName();

Console.WriteLine(host);

IPHostEntry i = Dns.GetHostByName(host);

foreach (IPAddress a in i.AddressList)

{

Console.WriteLine(a);

}

Console.ReadLine();

}

}

}

Anonieko Ramos said:
I found this code from ElreyRonald. It is a console but
works by referencing another website.




//--------------------------------------------------------------
// A console application to print IP Address of the current
// workstation (or its router's IP )
//
// Two things are demonstrated here:
// (1) How to get a web page html and
// (2) How to use regular expression to search within a string.
//
// Written by ElreyRonald V. [[email protected]] 2004
//--------------------------------------------------------------


using System;
using System.Text.RegularExpressions;
using System.Text;
using System.Net;
using System.IO;


namespace Network.Utilities
{

class GetIp
{
[STAThread]
static void Main(string[] args)
{
// use a website which displays your IP
string urlString = "http://www.getmyipaddress.com";

// Fetch the html text of the website.
string htmlText = GetPageHtml(urlString);

// Search for the IP within that page.
string regExpr = @".*>(\d+.\d+.\d+.\d+)</*";
Regex rex = new Regex( regExpr, RegexOptions.None);
if (rex.Match( htmlText).Success )
{
string ip = rex.Match( htmlText).Result("$1").Trim();
Console.WriteLine(ip);
}

}

//------------------------------------------------------
// Get the HTML text of a give URL
//------------------------------------------------------
private static String GetPageHtml(string url)
{
StringBuilder sb = new StringBuilder();

try
{

WebRequest myRequest = WebRequest.Create(url);

// Return the response.
WebResponse myHttpWebResponse = myRequest.GetResponse();

// Gets the stream associated with the response.
Stream receiveStream =
myHttpWebResponse.GetResponseStream();
Encoding encode =
System.Text.Encoding.GetEncoding("utf-8");

// Pipes the stream to a higher level stream reader with
the required encoding format.
StreamReader readStream = new StreamReader( receiveStream,
encode );

Char[] read = new Char[256];

// Reads 256 characters at a time.
int count = readStream.Read( read, 0, 256 );
while (count > 0)
{
// Dumps the 256 characters on a string and displays
the string to the console.
String str = new String(read, 0, count);
//Console.Write(str);
sb.Append(str);
count = readStream.Read(read, 0, 256);
}

// Releases the resources of the response.
myHttpWebResponse.Close();

// Releases the resources of the Stream.
readStream.Close();

}
catch ( Exception )
{
return "";
}

String strHtml = sb.ToString();
return strHtml.ToString();
}
}
}
 
2

2G

Hi,

It will depend on your isp. I'm using Telenet (Belgium) so for me,
retrieving my routers ip thru a webpage will not work because the webpage
will display the ip address of the proxy from my isp.
I'm also searching for a solution to retrieve my routers public ip but
haven't found one yet.

Grtz

Anonieko Ramos said:
Ram,
If you are in an intranet, this program only shows your
intranet IP address (e.g. 192.168.xxx.xxx ). I tried
both programs. The solution by ElreyRonald will correctly
print the ROUTER address ( bec. it is the IP as 'seen from outside')




"Ram Dash" <[email protected]> wrote in message
I got this from http://www.dotnet247.com/247reference/msgs/1/7651.aspx. Here
is it:

using System;

using System.Net;

namespace ConsoleApplication1

{

/// <summary>

/// Summary description for Class1.

/// </summary>

class Class1

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

string host;

host = Dns.GetHostName();

Console.WriteLine(host);

IPHostEntry i = Dns.GetHostByName(host);

foreach (IPAddress a in i.AddressList)

{

Console.WriteLine(a);

}

Console.ReadLine();

}

}

}

Anonieko Ramos said:
I found this code from ElreyRonald. It is a console but
works by referencing another website.




//--------------------------------------------------------------
// A console application to print IP Address of the current
// workstation (or its router's IP )
//
// Two things are demonstrated here:
// (1) How to get a web page html and
// (2) How to use regular expression to search within a string.
//
// Written by ElreyRonald V. [[email protected]] 2004
//--------------------------------------------------------------


using System;
using System.Text.RegularExpressions;
using System.Text;
using System.Net;
using System.IO;


namespace Network.Utilities
{

class GetIp
{
[STAThread]
static void Main(string[] args)
{
// use a website which displays your IP
string urlString = "http://www.getmyipaddress.com";

// Fetch the html text of the website.
string htmlText = GetPageHtml(urlString);

// Search for the IP within that page.
string regExpr = @".*>(\d+.\d+.\d+.\d+)</*";
Regex rex = new Regex( regExpr, RegexOptions.None);
if (rex.Match( htmlText).Success )
{
string ip = rex.Match( htmlText).Result("$1").Trim();
Console.WriteLine(ip);
}

}

//------------------------------------------------------
// Get the HTML text of a give URL
//------------------------------------------------------
private static String GetPageHtml(string url)
{
StringBuilder sb = new StringBuilder();

try
{

WebRequest myRequest = WebRequest.Create(url);

// Return the response.
WebResponse myHttpWebResponse = myRequest.GetResponse();

// Gets the stream associated with the response.
Stream receiveStream =
myHttpWebResponse.GetResponseStream();
Encoding encode =
System.Text.Encoding.GetEncoding("utf-8");

// Pipes the stream to a higher level stream reader with
the required encoding format.
StreamReader readStream = new StreamReader( receiveStream,
encode );

Char[] read = new Char[256];

// Reads 256 characters at a time.
int count = readStream.Read( read, 0, 256 );
while (count > 0)
{
// Dumps the 256 characters on a string and displays
the string to the console.
String str = new String(read, 0, count);
//Console.Write(str);
sb.Append(str);
count = readStream.Read(read, 0, 256);
}

// Releases the resources of the response.
myHttpWebResponse.Close();

// Releases the resources of the Stream.
readStream.Close();

}
catch ( Exception )
{
return "";
}

String strHtml = sb.ToString();
return strHtml.ToString();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top