11
\$\begingroup\$

Challenge

In this simple challenge, you have to print the public IP address of google.com.

Rules

  • You have to output either the IPv6 or IPv4 address, and do it by somehow using internet.

  • Output does not strictly need to be the IP address, your answer will be valid if the correct IP address is somewhere in the output, like Hello - google.com - 142.250.67.78 is a valid output.

  • You can use DNS queries and any kind of web APIs or web scraping.

  • Standard loopholes apply, except usage of URL shorteners, wanna see how creative you can get ;-)

  • This is , so the shortest answer wins!!

\$\endgroup\$
10
  • 1
    \$\begingroup\$ Welcome to Code Golf! As it is, this seems like a reasonably well-written challenge. However, in future, I'd recommend posting in the Sandbox to get feedback first \$\endgroup\$
    – pxeger
    Feb 17, 2021 at 10:18
  • 1
    \$\begingroup\$ Also, google.com has quite a lot of IP addresses. Presumably we should output just one of them \$\endgroup\$
    – pxeger
    Feb 17, 2021 at 10:19
  • 2
    \$\begingroup\$ "standard loopholes apply, except usage of URL shorteners" - does this mean we can or we can't use URL shorteners? \$\endgroup\$
    – pxeger
    Feb 17, 2021 at 10:48
  • 3
    \$\begingroup\$ your answer will be valid if the correct IP address is somewhere in the output This means you can print all IP adresses and call it done. Quite boring. Also, somehow usig the Internet is too vague \$\endgroup\$
    – Luis Mendo
    Feb 17, 2021 at 18:09
  • 2
    \$\begingroup\$ I've voted to close because there is still confusion over how many IP addresses may appear in the output. OP's comment above ('yes only one of them') seems to imply that there must only be one, but there is a contradiction with the second rule ('your answer will be valid if the correct IP address is somewhere in the output') that needs to be resolved. \$\endgroup\$
    – Dingus
    Feb 17, 2021 at 23:39

23 Answers 23

13
\$\begingroup\$

Zsh, 40 bytes

echo {0..255}.{0..255}.{0..255}.{0..255}

Try it online!

Since the output only needs to contain the IP address, we can just enumerate all of them! This probably isn't the shortest option, but it's funny!

\$\endgroup\$
4
  • \$\begingroup\$ Dosen't work when I click TIO link ;-( \$\endgroup\$
    – user101036
    Feb 17, 2021 at 10:22
  • 11
    \$\begingroup\$ and do it by somehow using internet. \$\endgroup\$
    – user100947
    Feb 17, 2021 at 10:23
  • 1
    \$\begingroup\$ (I just came up with the same idea... (besides this is not in the standard loophole, but yes I agree that the output format is a little too liberal)) @2x-1 That's not an issue, just add a ping to a.com. \$\endgroup\$
    – DELETE_ME
    Feb 17, 2021 at 10:32
  • 1
    \$\begingroup\$ Perhaps "the output should not contain more than 10^6 IP addresses". \$\endgroup\$
    – DELETE_ME
    Feb 17, 2021 at 10:33
7
\$\begingroup\$

Windows Batch, 15 bytes

ping google.com

Example run

C:\>ping google.com

Pinging google.com [8.7.198.46] with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 8.7.198.46:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
\$\endgroup\$
2
  • 2
    \$\begingroup\$ "ping gogle.com" will shave you off another byte! \$\endgroup\$
    – Chris B
    Feb 17, 2021 at 18:38
  • 2
    \$\begingroup\$ @ChrisB: "ping goo.gl" will shave you off 3 other bytes! \$\endgroup\$ Feb 17, 2021 at 19:31
6
\$\begingroup\$

Bash, 14 bytes

Requires dnsutils.

dig google.com
\$\endgroup\$
12
  • 1
    \$\begingroup\$ dig goo.gl saves 4 \$\endgroup\$ Feb 17, 2021 at 19:33
  • 2
    \$\begingroup\$ "except usage of URL shorteners" seems to imply you cannot use goo.gl \$\endgroup\$ Feb 17, 2021 at 19:56
  • 1
    \$\begingroup\$ @ijustlovemath I think that is given in the OP as an exception to the standard loopholes - i.e. "You may use URL shorteners even if that is a standard loophole, all other standard loopholes apply". \$\endgroup\$ Feb 17, 2021 at 20:04
  • \$\begingroup\$ Well, if shortened URLs are ok: dig g.co \$\endgroup\$
    – spuck
    Feb 17, 2021 at 21:26
  • 1
    \$\begingroup\$ Except both goo.gl and g.co point to different IP addresses than google.com, so neither actually works. \$\endgroup\$
    – Neinstein
    Feb 17, 2021 at 22:16
5
\$\begingroup\$

Java 8, 47 bytes

v->java.net.InetAddress.getByName("google.com")

(Don't) try it online (local run outputs google.com/142.250.179.206).

Explanation:

v->                          // Method with empty unused parameter & InetAddress return
  java.net.InetAddress       //  Create and return an InetAddress-instance
    .getByName("google.com") //  for hostname "google.com"

When this InetAddress-instance is printed, it will use its InetAddress.html#toString() method by default, which prints in the format hostname / literal IP address.

\$\endgroup\$
1
  • \$\begingroup\$ Also works for Kotlin, I think. \$\endgroup\$
    – user
    Feb 17, 2021 at 14:34
3
\$\begingroup\$

Powershell 5, 26 bytes

resolve-dnsname google.com

luckily, powershell has a CMDlet for this!

Somehow this does not work on TIO, but on powershell 5

Name                                           Type   TTL   Section    IPAddress
----                                           ----   ---   -------    ---------
google.com                                     AAAA   149   Answer     2404:6800:4007:805::200e
google.com                                     A      273   Answer     142.250.67.78

enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ test-connection google.com is an equal-bytes alternative. \$\endgroup\$ Feb 17, 2021 at 11:02
  • \$\begingroup\$ @KevinCruijssen nice find \$\endgroup\$
    – Wasif
    Feb 17, 2021 at 13:24
  • \$\begingroup\$ TIO uses a sandbox environment, which doesn’t allow internet connections, which is probably why it doesn’t work \$\endgroup\$ Feb 17, 2021 at 16:07
  • \$\begingroup\$ It's a shell. You can also use nslookup \$\endgroup\$ Feb 17, 2021 at 21:53
3
\$\begingroup\$

Ruby, 41 bytes

require'resolv';Resolv.getaddress'goo.gl'

Try it online!

Or as an alternative:

dig goo.gl

wget goo.gl

\$\endgroup\$
3
\$\begingroup\$

C (gcc) (-m32), 67 bytes

To avoid having to include any header files, I flatten the structure and manually dereference the first entry of the h_addr_list member of the hostent structure. Replace google.com with localhost to test in tio.run.

f(){puts(inet_ntoa(**(((int***)gethostbyname("google.com"))[4])));}

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Anything with triple pointers gets an upvote in my estimation :) \$\endgroup\$ Feb 19, 2021 at 1:18
2
\$\begingroup\$

Bash + dnsutils, 15 bytes

host google.com
\$\endgroup\$
1
  • 1
    \$\begingroup\$ dig google.com \$\endgroup\$ Feb 17, 2021 at 17:35
2
\$\begingroup\$

PHP, 20 bytes

<?=`ping google.co`;

For some reason google.co returns exactly the same IP as google.com.

\$\endgroup\$
1
  • \$\begingroup\$ you can also change it to goo.gl \$\endgroup\$
    – user100752
    Feb 18, 2021 at 1:02
2
\$\begingroup\$

MATLAB/Octave, 26 bytes

resolvehost goo.gl address

This solution uses resolvehost which is part of the Instrumentation Toolbox (instrument-control in Octave). We provide a second input ('address') to specify that we want the IP address.

Also we avoid using parentheses because both inputs should be interpreted as strings already so we can save 5 bytes by not using parentheses, quotes, and commas:

Long form: resolvehost('goo.gl','address')

Notes:

  • On Octave at least, we cannot shorten address (in many MATLAB/Octave commands you can shorten string literals). I don't have MATLAB available to me with this toolbox so it's possible this could be shortened in MATLAB.

  • We could alternately use the second output without a second input: [,a]=resolvehost('goo.gl') which would be the same length as this answer but we have to use parentheses and quotes. Also the empty first output is only valid in Octave. In MATLAB we would have to do [~,a]=resolvehost('goo.gl') which is 1 byte longer.

\$\endgroup\$
2
\$\begingroup\$

Golang, 77 bytes

package main;import(."net";."fmt");func main(){Print(LookupIP("google.com"))}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to Code Golf! \$\endgroup\$ Feb 17, 2021 at 21:16
2
\$\begingroup\$

Mathematica, 23 Bytes

HostLookup["google.co"]

Like with most things in life, Mathematica/Wolfram Language has a builtin for it. Requires version 11 or above.

\$\endgroup\$
2
\$\begingroup\$

Bash, 164 bytes 127 bytes

base64 -d<<<3dYBIAABAAAAAAABBmdvb2dsZQNjb20AAAEAAQAAKRAAAAAAAAAMAAoACCjxxt05OkuU|nc 1.1.1.1 -u 53|hexdump -e '4/1 "%0.2d.""\n"'

Why use dig when you can use netcat.

Example Run:

221.214.129.128.
00.01.00.01.
00.00.00.01.
06.103.111.111.
103.108.101.03.
99.111.109.00.
00.01.00.01.
192.12.00.01.
00.01.00.00.
01.29.00.04.
142.250.182.238. //Google's IP
00.00.41.04.
208.00.00.00.

EDIT: Saved a lot of bytes thanks to @DigitalTrauma

\$\endgroup\$
4
  • 1
    \$\begingroup\$ For people who want to know more about DNS protocol. \$\endgroup\$
    – Anunay
    Feb 17, 2021 at 22:49
  • 1
    \$\begingroup\$ Bash only, no utils: exec 9<>/dev/tcp/api.hackertarget.com/80 ; echo -e "GET /dnslookup/?q=google.com HTTP/1.1\nHost: api.hackertarget.com\n" >&9 ; cat <&9 \$\endgroup\$
    – spuck
    Feb 18, 2021 at 16:59
  • \$\begingroup\$ @spuck haha nice. \$\endgroup\$
    – Anunay
    Feb 18, 2021 at 18:09
  • 1
    \$\begingroup\$ You can base64 encode your request datagram to save several bytes. Also herestrings help: base64 -d<<<3dYBIAABAAAAAAABBmdvb2dsZQNjb20AAAEAAQAAKRAAAAAAAAAMAAoACCjxxt05OkuU|nc 1.1.1.1 -u 53|hexdump -e '4/1 "%0.2d.""\n"' \$\endgroup\$ Feb 19, 2021 at 1:52
1
\$\begingroup\$

Python 3, 58 54 bytes

-4 bytes by Arnauld, who got better reading skills than me...

from socket import*
print(gethostbyname('google.com'))
\$\endgroup\$
3
  • \$\begingroup\$ Oh, good catch Arnauld, thank you! Missed that somehow.. \$\endgroup\$
    – movatica
    Feb 17, 2021 at 12:45
  • \$\begingroup\$ import socket as s;s.gethostbyname('g.co') if in console, otherwise with print. Perhaps google.com if shortener is disallowed. \$\endgroup\$
    – KeyWeeUsr
    Feb 17, 2021 at 23:11
  • \$\begingroup\$ import socket as s;s. is one byte more than from socket import*;. Shorteners are not allowed according to the question. \$\endgroup\$
    – movatica
    Feb 18, 2021 at 10:29
1
\$\begingroup\$

Standard ML, 35 bytes

OS.Process.system"ping google.com";

I never used it before, but it turns out there is a OS structure in the basis library which lets you execute arbitrary commands in the system's default shell.

\$\endgroup\$
0
\$\begingroup\$

Windows Batch, 19 bytes

nslookup google.com
\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 57 bytes

A full program that prints the first A record (IPv4).

require('dns').lookup('google.com',(_,s)=>console.log(s))

(Don't) Try it online!

\$\endgroup\$
0
\$\begingroup\$

PHP, 31 bytes

<?=gethostbyname('google.com');

(Cannot) Try it online!

Works on my localhost, still trying to golf it

\$\endgroup\$
0
\$\begingroup\$

Perl 5, 53 bytes

say join".",map ord,gethostbyname("google.com")=~/./g

Try it offline with perl -E'say join".",map ord,gethostbyname("google.com")=~/./g'

\$\endgroup\$
0
\$\begingroup\$

Python 3, 46—47 bytes

from socket import*
getaddrinfo('google.co',0)

TIO doesn't let me run it, but the Jupyter and normal compiler returns this:

[(<AddressFamily.AF_INET: 2>, 0, 0, '', ('173.194.220.100', 0)),
 (<AddressFamily.AF_INET: 2>, 0, 0, '', ('173.194.220.113', 0)),
 (<AddressFamily.AF_INET: 2>, 0, 0, '', ('173.194.220.101', 0)),
 (<AddressFamily.AF_INET: 2>, 0, 0, '', ('173.194.220.102', 0)),
 (<AddressFamily.AF_INET: 2>, 0, 0, '', ('173.194.220.139', 0)),
 (<AddressFamily.AF_INET: 2>, 0, 0, '', ('173.194.220.138', 0))]

While the end result leads to google.com, the .com version of this code would return entirely different ip-ranges. Which would add +1 to the byte amount if you consider this not fair.

\$\endgroup\$
0
\$\begingroup\$

C# (Visual C# Interactive Compiler), 44 bytes

A function that prints the IP among other things

_=>System.Net.Dns.GetHostEntry("google.com")

Can't Try it online!

\$\endgroup\$
0
0
\$\begingroup\$

Shell / cURL: 12 bytes

curl g.co -v        # 12
curl goo.gl -v      # 14
curl google.co -v   # 17
curl google.com -v  # 18

IPs

$ nslookup google.com
Non-authoritative answer:
Name:   google.com
Address: 172.217.23.238
Name:   google.com
Address: 2a00:1450:4014:80d::200e

$ nslookup g.co
Non-authoritative answer:
Name:   g.co
Address: 172.217.23.238
Name:   g.co
Address: 2a00:1450:4014:80d::200e
\$\endgroup\$
3
  • \$\begingroup\$ I'm not sure g.co is going to have the same IP address as google.com...it seems to be a separate service, despite being owned by the same company. \$\endgroup\$ Feb 17, 2021 at 23:51
  • \$\begingroup\$ It has because g.co/g is basically google.com :) \$\endgroup\$
    – KeyWeeUsr
    Feb 17, 2021 at 23:52
  • \$\begingroup\$ Most probably they have a pile of services / load balancer with various IPs/ranges and route the traffic internally (would make more sense than to jump over Internet) \$\endgroup\$
    – KeyWeeUsr
    Feb 17, 2021 at 23:58
0
\$\begingroup\$

Visual Basic .NET (.NET Core), 60 53 bytes

Console.WriteLine(Net.Dns.GetHostEntry("google.com"))

Try it online!

-7 bytes thanks to @JoelCoehoorn

\$\endgroup\$
2
  • \$\begingroup\$ Since the System namespace is imported by default, you can shave 7 bytes by removing it: Console.WriteLine(Net.Dns.GetHostEntry("google.com")) \$\endgroup\$ Feb 17, 2021 at 21:56
  • \$\begingroup\$ @JoelCoehoorn thanks \$\endgroup\$
    – Wasif
    Feb 18, 2021 at 3:46