using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace CS
{
using static System.Console;
class Program
{
static void Main(string[] args)
{
// 인터넷 url로 IP 주소 확인
string url = "www.naver.com";
IPHostEntry ipHostEntry = Dns.GetHostEntry(url);
WriteLine("■ IP addresses of " + url);
foreach (IPAddress add in ipHostEntry.AddressList)
{
WriteLine(add.ToString());
}
// 호스트 네임으로 IP 주소 확인
WriteLine();
string hostName = Dns.GetHostName();
ipHostEntry = Dns.GetHostEntry(hostName);
WriteLine("■ All IP addresses of " + ipHostEntry.HostName);
// WriteLine(hostName);
foreach (IPAddress address in ipHostEntry.AddressList)
{
WriteLine(address.ToString());
}
WriteLine();
WriteLine("■ All IP6 addresses of " + ipHostEntry.HostName);
foreach (IPAddress address in ipHostEntry.AddressList)
{
if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
WriteLine(address.ToString());
}
WriteLine();
WriteLine("■ All IP4 addresses of " + ipHostEntry.HostName);
foreach (IPAddress address in ipHostEntry.AddressList)
{
if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
WriteLine(address.ToString());
}
WriteLine();
WriteLine("■ All IP4 addresses of " + ipHostEntry.HostName);
foreach (IPAddress address in ipHostEntry.AddressList)
{
if (address.ToString().Split('.').Count() == 4)
WriteLine(address.ToString());
}
// IP 주소로 호스트 네임 확인
WriteLine();
IPAddress ipAddress = IPAddress.Parse(ipHostEntry.AddressList[0].ToString());
// IPAddress ipAddress = IPAddress.Parse("192.168.0.100"); // My internal IP address
ipHostEntry = Dns.GetHostEntry(ipAddress);
Console.WriteLine("■ Host name: " + ipHostEntry.HostName);
// 웹사이트를 이용한 외부 IP 주소 확인
WriteLine();
try
{
string externalIP = new WebClient().DownloadString("http://ipinfo.io/ip").Trim();
// or http://icanhazip.com
WriteLine("■ External IP address: " + externalIP);
}
catch (Exception exc)
{
WriteLine(exc.Message);
}
}
}
}