Home > Net >  What is the proper way to get FQDN of current host?
What is the proper way to get FQDN of current host?

Time:08-08

For some time I was using code:

hostname = Socket.gethostbyname(Socket.gethostname).first 

taken from here.

But, as the comment said - it's deprecated, and now both rubocop and ruby itself are complaining:

=$ ./z.rb 
./z.rb:5: warning: Socket.gethostbyname is deprecated; use Addrinfo.getaddrinfo instead.

=$ rubocop z.rb
...
z.rb:5:13: W: Lint/DeprecatedClassMethods: Socket.gethostbyname is deprecated in favor of Addrinfo#getaddrinfo.
puts Socket.gethostbyname(Socket.gethostname).first
            ^^^^^^^^^^^^^

The problem is that I don't know how to use getaddrinfo to get the same information. Anyone could show an example?

For now, I figured I'll just use:

`hostname --fqdn`

which works, as I only run it on Linux, but it's not really nice approach.

CodePudding user response:

Got help in some other way, but figured others might benefit too.

Current way to do it is:

Addrinfo.getaddrinfo(Socket.gethostname, nil).first.getnameinfo.first
  • Related