1

I am having an issue trying to determine if an address is an IP address or a hostname. Everything I found says to use a regular expression. I am not sure how to form the IF statement. Here is my code:

private void btnPingAddress_Click(object sender, EventArgs e)
{
    intByteSize = Convert.ToInt32(numericDataSize.Value);
    intNumberOfPings = Convert.ToInt32(numericPing.Value);
    strDnsAddress = cmbPingAddress.Text;
    //If address is IP address:
    if (strDnsAddress Contains ((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?")
    {
       txtPingResults.Text = "Pinging " + strIpAddress + " with " + intByteSize + " bytes of data:" + "\r\n";
    }
    // If address is hostname:
    else
    {
       strIpAddress = Convert.ToString(Dns.GetHostEntry(strDnsAddress));
       txtPingResults.Text = "Pinging " + strDnsAddress + "  [" + strIpAddress + "]  with " + intByteSize + " bytes of data:" + "\r\n";
    }           
    Ping ping = new Ping();
    PingReply reply = ping.Send(cmbPingAddress.Text);
    txtPingResults.Text = "Pinging " + cmbPingAddress.Text + "  [" + Convert.ToString(reply.Address) + "]  with " + intByteSize + " bytes of data:" + "\r\n";
    for (int i = 0; i < intNumberOfPings; i++)
    {
       txtPingResults.AppendText("Reply from "+Convert.ToString(reply.Address)+": Bytes="+Convert.ToString(intByteSize) +" Time="+Convert.ToString(reply.RoundtripTime) +"ms"+" TTL="+Convert.ToString(reply.Options.Ttl)+ "\r\n");
       cmbPingAddress.Items.Add(cmbPingAddress.Text);
    }
}

Any help will be greatly appreciated.

Omar
  • 16,329
  • 10
  • 48
  • 66
Tornado726
  • 352
  • 1
  • 7
  • 16

4 Answers4

7

I needed to do this recently for pulling apart WebAPI headers. Uri.CheckHostName is probably the easiest way to do it, and it includes ipv6 support:

var dns = Uri.CheckHostName("www.google.com"); //UriHostNameType.Dns
var ipv4 = Uri.CheckHostName("192.168.0.1");   //IPv4
var ipv6 = Uri.CheckHostName("2601:18f:780:308:d96d:6088:6f40:c5a8");//IPv6
dns = Uri.CheckHostName("Foo");    //Dns

The last one is tricky, but technically right. At the very least, you can rule out hostnames vs IP addresses.

rianjs
  • 7,767
  • 5
  • 24
  • 40
3

try:

ValidIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";

ValidHostnameRegex = "^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$";



if(Regex.IsMatch(strDnsAddress, ValidIpAddressRegex )) {
    // the string is an IP
}
else if(Regex.IsMatch(strDnsAddress,ValidHostnameRegex )){
    // the string is a host

 }
Habib Zare
  • 1,206
  • 8
  • 17
1

Using your regex:

if(Regex.IsMatch(strDnsAddress, "(2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?")) {
    // the string is an IP
}

Or you can use the regexes provided in this question (as habib zare suggested in his comment)

Community
  • 1
  • 1
BlackBear
  • 22,411
  • 10
  • 48
  • 86
0

I try to explain the concept in a very basic and understandable for beginners like myself!

NOTE: The argument of the function is defined as an optional argument (CheckHost(string host="127.0.0.0"))

public static void CheckHost(string host="127.0.0.1")
    {
        if (host == null || host.Length == 0)
        {
            Console.WriteLine("no host is defined!");
        }
        else if (Uri.CheckHostName(host).ToString() == "Dns")
        {
            Console.WriteLine("The "+host+" is a DNS hostname.");

        }
        else if (Uri.CheckHostName(host).ToString() == "IPv4")
        {
            Console.WriteLine("The " + host + " is an IP Address v4.");
        }
        else if (Uri.CheckHostName(host).ToString() == "IPv6")
        {
            Console.WriteLine("The " + host + " is an IP Address v6.");
        }
        else
        {
            Console.WriteLine("The " + host + " is unknown!");
        }
    }
Saffa Seraj
  • 77
  • 1
  • 8