OBTER IP / MASCARA / GATEWAY E SERVIDOR DNS

HELDER 15/01/2010 20:05:21
#332090
Viva

Como obter o IP, mascara da sub-rede, gateway e servidor de dns ?
(No vb 2008 e ou 2010)


O IP já consegui, agora falta o resto
Imports System
Imports System.Net
Imports System.Net.Dns

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sas As String = Dns.GetHostName()


DoGetHostAddresses(sas)

End Sub
Public Sub DoGetHostAddresses(ByVal hostName As [String])

Dim ips As IPAddress()

Dim ns
ips = Dns.GetHostAddresses(hostName)

TextBox1.Text = ips(ips.Length - 1).ToString


End Sub
End Class
MARCELOSN 15/01/2010 20:18:41
#332091


Boa noite


tem este link para auxilio, veja se ajuda
--------------------------------------------------------------------------------
Getting IP Details and Pinging to the Gateway
http://www.codeproject.com/KB/IP/IP_Details_and_Ping.aspx
--------------------------------------------------------------------------------


Using the code
The code is handy and can be incorporated in any existing Code.
Collapse

//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;
using System.Diagnostics;

namespace Netmon
{
public partial class Netmon : Form
{
public Netmon()
{
InitializeComponent();
}

private void Start_Click(object sender, EventArgs e)
{

{
richTextBox2.Text = [Ô]
[Ô];
//Gateway IP
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
if (addresses.Count > 0)
{
foreach (GatewayIPAddressInformation address in addresses)
{
richTextBox2.Text += [Ô]Gateway Address | [Ô] + address.Address.ToString() + [Ô]
[Ô];

// Pinging
IPAddress addr = address.Address;
Ping pingSender = new Ping();
PingOptions options = new PingOptions();

// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment = true;

// Create a buffer of 32 bytes of data to be transmitted.
string data = [Ô]aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[Ô];
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
int count = 0; float Success = 0; float loop = 0;
for (int i = 0; i < 25; )
{
PingReply reply = pingSender.Send(addr, timeout, buffer, options);

{
//richTextBox2.Text += [Ô]
[Ô] + [Ô]Address: [Ô] + reply.Address.ToString() + [Ô]    [Ô];
try
{
richTextBox2.Text += [Ô]
[Ô] + [Ô]RoundTrip time: [Ô] + reply.RoundtripTime + [Ô] ms[Ô] + [Ô]    [Ô];
}
catch
{ richTextBox2.Text += [Ô]
[Ô] + [Ô]RoundTrip time: [Ô] + [Ô] [Ô] + [Ô] ms[Ô] + [Ô]    [Ô]; }

try
{
richTextBox2.Text += [Ô]Time to live(TTL): [Ô] + reply.Options.Ttl + [Ô]    [Ô];
}
catch
{ richTextBox2.Text += [Ô]Time to live(TTL): [Ô] + [Ô] [Ô] + [Ô]    [Ô]; }
//richTextBox2.Text += [Ô]Don[ô]t fragment: [Ô] + reply.Options.DontFragment + [Ô]    [Ô];
try
{
richTextBox2.Text += [Ô]Status : [Ô] + reply.Status + [Ô]    [Ô];
}
catch
{ richTextBox2.Text += [Ô]Status : [Ô] + reply.Status + [Ô]    [Ô]; }
count += Convert.ToInt32(reply.RoundtripTime);
if (reply.Status == IPStatus.Success)
{ Success += 1; }
}
{ i++; }
loop += 1;
richTextBox2.Show();

} richTextBox2.Text += [Ô]
[Ô] + [Ô]Avg. Roundtime : [Ô] + count / loop+[Ô] ms[Ô];
float AvgSuccess = ((Success / loop) * 100);
richTextBox2.Text += [Ô]
[Ô] + [Ô]Avg. Success (%) : [Ô] + AvgSuccess + [Ô]%[Ô];
}

}
}
}

}


private void Netmon_Load(object sender, EventArgs e)
{
{ // Initialize Variables
richTextBox1.Text = [Ô][Ô];
string iphostname = [Ô][Ô];
try
{
iphostname = Dns.GetHostName(); // Resolving Host name
IPHostEntry ipentry = Dns.GetHostEntry(iphostname);
IPAddress[] addr = ipentry.AddressList;// Resolving IP Addresses
for (int i = 0; i < addr.Length; i++)
{
try
{ richTextBox1.Text += [Ô]IP Address | [Ô] + Convert.ToString(addr[i]) + [Ô]
[Ô]; }
catch
{
richTextBox1.Text += [Ô]IP Address | [Ô] + [Ô]
[Ô];
}
}

richTextBox1.Text += [Ô]Hostname | [Ô] + ipentry.HostName + [Ô]
[Ô];
}
catch
{
richTextBox1.Text += [Ô]Hostname | [Ô] + [Ô]
[Ô];
}
// More Details
try
{// Getting DNS Addresses
for (int i = 0; i < 3;i++ )
richTextBox1.Text += [Ô]DNS Address | [Ô] + System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[i].GetIPProperties().DnsAddresses[0].ToString() + [Ô]
[Ô];
}
catch
{
richTextBox1.Text += [Ô]DNS Address | [Ô] + [Ô]
[Ô];
}
try
{// Getting Network Type Description
for (int i = 0; i < 2; i++)
richTextBox1.Text += [Ô]Description | [Ô] + System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[i].Description.ToString() + [Ô]
[Ô];
}
catch
{
richTextBox1.Text += [Ô]Description | [Ô] + [Ô]
[Ô];
}
try
{// Getting the Details of Connection
for (int i = 0; i < 2; i++)
richTextBox1.Text += [Ô]Name | [Ô] + System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[i].Name.ToString() + [Ô]
[Ô];
}
catch
{
richTextBox1.Text += [Ô]Name | [Ô] + [Ô]
[Ô];
}
try
{// Operational Status
for (int i = 0; i < 2; i++)
richTextBox1.Text += [Ô]Operation Status | [Ô] + System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[i].OperationalStatus.ToString() + [Ô]
[Ô];
}
catch
{
richTextBox1.Text += [Ô]Operation Status | [Ô] + [Ô]
[Ô];
}
try
{// Getting DHCP Details
for (int i = 0; i < 3; i++)
richTextBox1.Text += [Ô]DHCP | [Ô] + System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[i].GetIPProperties().DhcpServerAddresses[0].ToString() + [Ô]
[Ô];
}
catch
{
richTextBox1.Text += [Ô]DHCP | [Ô] + [Ô]
[Ô];
}
try
{// Getting DNS Details
for (int i = 0; i < 3; i++)
richTextBox1.Text += [Ô]DNS Suffix | [Ô] + System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[i].GetIPProperties().DnsSuffix.ToString() + [Ô]
[Ô];
}
catch
{
richTextBox1.Text += [Ô]DNS Suffix | [Ô] + [Ô]
[Ô];
}
try
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
if (addresses.Count > 0)
{
foreach (GatewayIPAddressInformation address in addresses)
{
richTextBox1.Text += [Ô]Gateway Address | [Ô] + address.Address.ToString() + [Ô]
[Ô];
}

}
}
}
catch
{
richTextBox1.Text += [Ô]Gateway Address | [Ô] + [Ô]
[Ô];
}


}
}

}
}
// END

Tópico encerrado , respostas não são mais permitidas