6

I would like to switch between hostnames and hex IP addresses, and vice versa. I have installed syslinux-utils on Debian Stretch, which provides gethostip:

gethostip -x google.com 
D83ACD2E

How can I switch D83ACD2E back to hostname?

In older version of Debian Wheezy, I can use the commands getaddrinfo' and 'getnameinfo

# getaddrinfo google.com
D83ACD4E
# getnameinfo D83ACD4E  
mil04s25-in-f14.1e100.net

I was unable to find these tools in Debian Stretch. Were these tools replaced by others?

Martin Vegter
  • 69
  • 66
  • 195
  • 326
  • If you want an answer from the official source, find out which package provides that tool in wheezy and install the same package in stretch. If it doesn't work/not the same the contact the package maintainer, they can answer the question. – apocalysque Sep 12 '18 at 22:38

2 Answers2

14

You could hexify D83ACD2E, pack it into a (Network byte order!) 32-bit integer, then print the (unsigned!) character components of that integer joined by dots. (This is also possible if somewhat more verbose in assembly.)

$ perl -e 'printf "%v*d\n", ".", pack "N", hex shift' D83ACD2E
216.58.205.46
$ 

With fewer complications the decimal flag to gethostip gives that value directly, which then can be fed to host or nslookup or getent hosts

$ gethostip -d google.com
172.217.3.206
$ host `gethostip -d google.com`
206.3.217.172.in-addr.arpa domain name pointer sea15s12-in-f206.1e100.net.
206.3.217.172.in-addr.arpa domain name pointer sea15s12-in-f14.1e100.net.
$ getent hosts `gethostip -d google.com`
172.217.3.206   sea15s12-in-f206.1e100.net
$ 

that's the DNS PTR record associated with the given IP address, which may or may not be set, or may or may not be the hostname you are looking for.

Or if you search around with apt-file

$ sudo apt-file search getaddrinfo | grep 'getaddrinfo$'
gnulib: /usr/share/gnulib/modules/getaddrinfo
libruli-bin: /usr/bin/ruli-getaddrinfo
libsocket-getaddrinfo-perl: /usr/bin/socket_getaddrinfo
$ sudo apt-file search getnameinfo | grep 'getnameinfo$'
libsocket-getaddrinfo-perl: /usr/bin/socket_getnameinfo
$ sudo apt-get install libsocket-getaddrinfo-perl
...

but that version does not appear to support your notation:

$ socket_getnameinfo D83ACD4E
Unrecognised address or port format - Name or service not known
$ 

but does if the conventional 0x for hex prefix is used

$ socket_getnameinfo 0xD83ACD4E
Resolved address '0xD83ACD4E'

  mil04s25-in-f78.1e100.net
$ 

(according to the man page Debian did rename the program, which I now recall LeoNerd mentioning on IRC a while ago...)

If you're dead set on accepting D83ACD4E this can be done with the above hex to numify that value, packing it, and punching that blindly through Socket module functions. But this really should be a script with error checking, input validation, tests, etc

$ perl -MSocket=:addrinfo,pack_sockaddr_in \
  -E '($e,$h)=getnameinfo pack_sockaddr_in(0, pack("N", hex shift));' \
  -E 'say $h' D83ACD2E
mil04s24-in-f46.1e100.net
$ 
thrig
  • 34,333
  • 3
  • 63
  • 84
  • So `/usr/share/gnulib/modules/getaddrinfo` is not executable/the one ? –  Sep 15 '18 at 22:52
7

You may be able to use glibc's getent here:

$ getent ahostsv4 0xD83ACD2E | { read ip rest && getent hosts "$ip"; }
216.58.205.46   mil04s24-in-f46.1e100.net

Another perl approach:

$ perl -MSocket -le '($n)=gethostbyaddr(inet_aton("0xD83ACD2E"), AF_INET); print $n'
mil04s24-in-f46.1e100.net
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501