'호스트'에 해당되는 글 1건

  1. 2021.12.22 C# IP Addresses and Hostname - IP 주소 호스트 네임 확인
반응형

IP 주소와 호스트 네임을 확인 해 보자.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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);
            }
        }
    }
}
 

 

소스를 입력하고 빌드한다.

 

IP 주소 및 호스트 네임을 확인 할 수 있다.

 

반응형
Posted by J-sean
: