0

If Powershell Core on Linux, using dotnet, can return the FQDN, then how does mono, also using dotnet, get the same result?

Here's powershell finding the local system FQDN:

thufir@dur:~/powershell$ 
thufir@dur:~/powershell$ pwsh
PowerShell v6.0.1
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS /home/thufir/powershell> 
PS /home/thufir/powershell> [System.Net.Dns]::GetHostByName((hostname)).HostName                                        
dur.bounceme.net
PS /home/thufir/powershell> 

and mono:

thufir@dur:~$ 
thufir@dur:~$ mono Projects/console/console/bin/Debug/console.exe
Hello Mono World
dur
dur

localhost
localhost
localhost
dur
done
thufir@dur:~$ 


code:

using System;
using System.Net;
using System.Net.Sockets;
using System.Globalization;
using System.Text;

namespace console
{
    class MainClass
    {
        public static void Main(string[] args)
        {



        Console.WriteLine ("Hello Mono World");

            String name;

            name = Environment.MachineName;
            Console.WriteLine(name);

             name = System.Net.Dns.GetHostName();
            Console.WriteLine(name);

             name = System.Environment.GetEnvironmentVariable("COMPUTERNAME");
            Console.WriteLine(name);

            name =System.Net.Dns.GetHostEntry("localhost").HostName;
            Console.WriteLine(name);

            name = Dns.GetHostEntry("LocalHost").HostName;
            Console.WriteLine(name);

            // var ipProperties = IPGlobalProperties.GetIPGlobalProperties();
            //   name = string.Format("{0}.{1}", ipProperties.HostName, ipProperties.DomainName);
            //  Console.WriteLine(name);

            name = Dns.GetHostEntry("LocalHost").HostName;
            Console.WriteLine(name);

            name = System.Net.Dns.GetHostEntry(Environment.MachineName).HostName;
            Console.WriteLine(name);


            Console.WriteLine("done");

        }
    }
}

thanks to jjw for pointing me in the right direction.

This is tantalizingly close, because dur, which is how the prompt is shown, is listed. Just not the whole name. Also:

thufir@dur:~$ 
thufir@dur:~$ hostname
dur
thufir@dur:~$ 
thufir@dur:~$ hostname --fqdn
dur.bounceme.net
thufir@dur:~$ 
thufir@dur:~$ cat /etc/hosts
127.0.0.1   localhost
127.0.1.1   dur.bounceme.net    dur

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
thufir@dur:~$ 
thufir@dur:~$ cat /etc/hostname 
dur
thufir@dur:~$ 

Looking for the whole enchilada here, not just the prepend of dur but dur.bounceme.net if possible. Because Powershell Core can find the FQDN shouldn't mono be able to do the same?

Thufir
  • 7,241
  • 18
  • 103
  • 236
  • The system may not have a FQDN. It may only be a hostname. *`localhost`* is a hostname; *`localhost.localdomain.`* is a FQDN. – jww Feb 18 '18 at 05:28
  • @jww it has a FQDN. Even localhost.localdomain would be output. currently, no output at all. – Thufir Feb 18 '18 at 05:29
  • Maybe you can try Mono's [Howto IpInfobyDns](http://www.mono-project.com/archived/howto_ipinfobydns/). It looks like the Mono sample is using a static method `Dns.GetHostName()`. `hostname --fqdn` is different. It is a command written in C against OS API's (not a language runtime) and I am not sure how it behaves nowadays. I know how it is supposed to behave, but that may not happen. – jww Feb 18 '18 at 05:36
  • 1
    There is also [Get FQDN in C# running on Mono](https://stackoverflow.com/q/17114536/608639), [How do I get the local machine name in C#?](https://stackoverflow.com/q/662282/608639), [How to get full host name in C#?](https://stackoverflow.com/q/37087949/608639), etc on Stack Overflow. The second one seems like it should help: `System.Net.Dns.GetHostEntry("").HostName;`. It might be a good idea to perform a quick search: [Mono+hostname+site:stackoverflow.com](https://www.google.com/search?q=Mono+retrieve+hostname+site:stackoverflow.com). – jww Feb 18 '18 at 05:44

0 Answers0