[Question]
  [
Write a program which, according to whether the script has access to the internet, produces an output which is ["Truthy/Falsey"](http://meta.codegolf.stackexchange.com/questions/2190/interpretation-of-truthy-falsey). You may try and connect to any existing site, **at your own discretion** (don't use a shady site which only has 10% uptime - try to keep to above 80% annual uptime). If the site is down, your program does *not* have to work.
It must be a **standalone program** or a **function**. You may use libraries outside of the standard library to achieve this. [Standard loopholes](http://meta.codegolf.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) are forbidden. This is code golf, so the code with the shortest byte-count wins. 
Example pseudocode:
```
function a:
    try:
        connect to internet 
        return 1
    catch error:
        return 0
```
This is my first post on code golf, so if this violates any rules in any way or is a dupe, please alert me.
EDIT: Due to numerous suggestions, I have removed the UTF-8 byte count restriction
  
[Answer]
# Bash (with *dnsutils*), 3 bytes
Sends a DNS request for "." (DNS root), exit code is 0 for success and >0 otherwise.
**Golfed**
```
dig
```
**Test**
```
% dig >/dev/null; echo $?;        
0
% nmcli nm wifi off
% dig >/dev/null; echo $?;
9
```
**Disclaimer**
This will obviously only work if your *DNS* server is sitting in the provider's network, i.e. in the "Internet" (as your provider network is normally a part of it), or if your system is using a public DNS server (like 8.8.8.8 from Google, which Android based systems use), as otherwise, you can get a cached copy from a local *LAN* server (or localhost).
But I assume this is not against the [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") rules, as there are obviously more than one system where this does work as intended.
Pure-HTTP methods can also give false positives, due to an intermediate caching proxy, and are not guaranteed to work everywhere, so that is not something unique to this method.
**A slightly more reliable version, 8 bytes**
```
dig +tra
```
(a little tribute to @**Dig**ital **Tra**uma !)
Enables the "trace mode", which will force *dig* to do the recursive search by itself (see <https://serverfault.com/a/778830>), avoiding any cache issues.
[Answer]
# Bash + GNU utils, 8
* 5 bytes saved thanks to @Muzer.
```
wget to.
```
The other shell answers check the return code and echo some status output accordingly. This is unnecessary. The shell return code is already a usable Truthy/Falsey code and accessible in the `$?` parameter which is idiomatic for bash. Return code 0 means True. Return code >0 means False.
In use:
```
ubuntu@ubuntu:~$ wget to.
--2017-01-13 09:10:51--  http://to./
Resolving to. (to.)... 216.74.32.107, 216.74.32.107
Connecting to to. (to.)|216.74.32.107|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11510 (11K) [text/html]
Saving to: ‘index.html.6’
index.html.6        100%[===================>]  11.24K  --.-KB/s    in 0.04s   
2017-01-13 09:10:51 (285 KB/s) - ‘index.html.6’ saved [11510/11510]
ubuntu@ubuntu:~$ echo $?
0
ubuntu@ubuntu:~$ sudo ifconfig ens33 down
ubuntu@ubuntu:~$ wget to.
--2017-01-13 09:11:00--  http://to./
Resolving to. (to.)... failed: Temporary failure in name resolution.
wget: unable to resolve host address ‘to.’
ubuntu@ubuntu:~$ echo $?
4
ubuntu@ubuntu:~$ sudo ifconfig ens33 up
ubuntu@ubuntu:~$ # Local network up, upstream link down
ubuntu@ubuntu:~$ wget to.
--2017-01-13 09:11:34--  http://to./
Resolving to. (to.)... failed: Name or service not known.
wget: unable to resolve host address ‘to.’
ubuntu@ubuntu:~$ echo $?
4
ubuntu@ubuntu:~$ 
```
[Answer]
## Batch, 8 bytes
```
ping to.
```
`ping` will set `ERRORLEVEL` to `1` if the address cannot be resolved or reached.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~11~~ 9 bytes
Saved 2 bytes on "to." courtesy of *ev3commander*
```
…to..wgX›
```
Checks if the length of the content at <http://to>. is greater than 1.  
`.w` returns 0 on error. 
[Answer]
## R, 20 bytes
```
curl::has_internet()
```
There's a function for exactly this task in the `curl` package.
[Answer]
## Bash ~~66~~ ~~62~~ 21 bytes
```
ping -c1 g.gl echo $?
```
Thanks @Alex L. for the URL shortening tip.
Ungolfed version:
```
r=$(ping -c1 g.gl)
if [ $? -ne 0 ];
 then echo "0"
else echo "1"
fi
```
This is my first answer in Bash , i'm not sure i have shortened the script enough.
[Answer]
# Java, 72 bytes
```
a->new java.net.InetSocketAddress("to.",80).getAddress().isReachable(9);
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~15~~ 14 bytes
*One byte saved thanks to [Kevin Cruijssen](https://codegolf.stackexchange.com/users/52210/kevin-cruijssen)'s suggestion*
```
'http://to.'Xi
```
Output is through STDOUT. This displays a non-empty string containing non-zero chars (which is truthy) if there is an Internet connection; and displays nothing (which is falsy) if there's no connection.
This can't be tested online because the `Xi` is not allowed in the online interpreters.
### Explanation
```
'http://to.'  % Push this string
Xi            % Return contents of that URL as a string. If there is no Internet
              % connection this gives an error, with no output on STDOUT
                
```
[Answer]
## Perl, 15 bytes
```
print`curl to.`
```
Run with:
```
perl -e 'print`curl to.`' 2> /dev/null
```
`curl` outputs stuffs on `STDERR`, don't mind them. If the computer has access to internet, it will print a few lines of html (truthy), otherwise, it will print nothing (falsy).
*Saved 1 bytes by using `to.` (instead of my previous `b.io`) thanks to @Kevin Cruijssen.*
[Answer]
## C#, 87 bytes
```
_=>{try{new System.Net.WebClient().OpenRead("http://g.gl");return 1;}catch{return 0;}};
```
If an exception is considered falsey, which I don't think it is, then this is 65 bytes:
```
_=>new System.Net.WebClient().OpenRead("http://g.gl").ReadByte();
```
I also tried using the link `http://to.` as stated by @KevinCruijssen but it didn't seem to work.
[Answer]
# [8th](http://8th-dev.com/), ~~23~~ 21 bytes
*Two bytes saved thanks to [Kevin Cruijssen's](https://codegolf.stackexchange.com/users/52210/kevin-cruijssen) suggestion* and to my discovery: [<http://to>](http://to) seems to work as well as [<http://to>.](http://to.) (saving another byte)
```
"http://to" net:get .
```
If site <http://to> can be reached, it then prints `true`. Otherwise it prints `false`.
It leaves retrieved data on the stack.
[Answer]
# Javascript, 16 bytes
```
navigator.onLine
```
Returns `true` or `false`.
[Try It!](https://jsconsole.com/?navigator.onLine)
I'm new here, so IDK if I counted bytes properly, or if builtins are cheating...
[Answer]
# MATLAB, ~~32~~ 22 bytes
```
urlread('http://g.gl')
```
**Explanation:**
If the internet connection is up, this will result in `ans` (the default variable) being a string with the entire html-code in plain text (which is **true** in MATLAB).
If the internet connection is down, this will write an error message to STDERR and leave the workspace empty (which is **false** in MATLAB).
Unfortunately, [`urlread`](https://se.mathworks.com/help/matlab/ref/urlread.html) requires a *full url-address*, so `g.gl` is not enough. 11 of the 22 bytes are therefore just the url-address.
---
**Alternative approach:**
A solution that catches the error and leave a 0 (also **false**) in the workspace if the connection is down:
```
0;try urlread('http://g.gl'),end
```
`0;` initializes the default variable `ans` to `0`, which is `false` in MATLAB. Then we [`try`](https://se.mathworks.com/help/matlab/matlab_prog/use-trycatch-to-handle-errors.html) to read the url. This will give an error if the internet connection is down, or a character array if not (which is `true` in MATLAB).
We don't need to catch anything, so we just end. If the urlread call was successful, then `ans` will be a long string with the content of the website, otherwise `ans=0`.
[Answer]
## Bash, 39 bytes
```
exec 4<>/dev/tcp/to./80&&echo 1||echo 0
```
[Answer]
## Mathematica 10 Bytes
Assuming you have a valid copy of Mathematica, and login credentials on user.wolfram.com
```
CloudPut@1
```
will write the value 1 to the cloud.
Truthy: CloudObject[""] 
Falsey: $Failed
```
CloudGet@%
```
Will return the value 1 that was uploaded to the cloud.
[Answer]
# JavaScript ES6, 71 43 bytes
```
fetch``.then(a=>alert(1)).catch(a=>alert``)
```
Alerts 1 if online, alerts an empty string if offline. Thanks to Patrick Roberts for helping me shave off some bytes
Old version
```
_=>fetch('http://enable-cors.org').then(a=>alert(a)).catch(a=>alert(0))
```
Alerts `[object Reponse]` if online, alerts `0` if offline
Removed the code snippet, it doesn't work because it loads from a different domain without CORS, but it works in the browser console
[Answer]
# JavaScript ES6, 90 81 Bytes
```
f=a=>{i=new Image();i.src="//placehold.it/1x1";i.onload=b=>a(1);i.onerror=c=>a()}
```
# JavaScript ES6, 22 21 bytes (Invalid)
Some browsers don't fully support, or produce the expected result when using navigator.onLine.
```
f=a=>navigator.onLine
```
[Answer]
# Scala, 54 bytes
```
x=>(Runtime.getRuntime exec "ping -c 1 ai."waitFor)<1
```
Pretty simple; executes a ping command to `http://ai./`, and returns true if it exits with 0, or false otherwise.
[Answer]
## Powershell, ~~64~~ ~~26~~ 23 bytes
Saved 38 bytes, thanks to Shawn Esterman
Saved 3 bytes, and repaired script, thanks to briantist
```
Test-Connection -q g.gl
```
[Answer]
# PHP, 23 PHP + Curl, 14
Using PHP's backtick operator:
```
<?=`curl to.`;
```
---
*Orignal answer:*
I will try to make a start:
```
<?=file('http://x.gl');
```
This outputs nothing if x.gl can't be reached and `Array` if it is.
Another version where I'm not quite sure if they fit:
```
<?=getmxrr('x.gl',$a);  // 22 chars
```
[Answer]
# Python 3 + requests, ~~59~~ ~~55~~ ~~53~~ 39 bytes
There has to be a requests answer, right?
```
from requests import*
get("http://to.")
```
Exit status is 0 for internet, 1 for no internet. Example:
```
$ python inet.py 
$ echo $?
0
$ # Remove ethernet cable
$ python inet.py 
$ echo $?
1
```
Changelog:
* -4 bytes (thanks Mego)
* -14 bytes (thanks pppery)
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 8 bytes
```
`g.gl`¨U
```
*Wait, `¨U` exists?*
Doesn't work on the online interpreter. Exits with error for falsy, runs successfully with no output for truthy.
```
`g.gl`   # string literal g.gl
      ¨U # GET request to that URL
```
For fun, here's a fairly cheaty version. Ports JavaScript:
# [Vyxal](https://github.com/Vyxal/Vyxal) `E`, 13 bytes
```
`ƈ⌈(₍†.on¬ḣ)`
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyJFIiwiIiwiYMaI4oyIKOKCjeKAoC5vbsKs4bijKWAiLCIiLCIiXQ==)
Just runs the JavaScript `alert(navigator.onLine)` using the `E` flag
[Answer]
# Elixir, 33 bytes
```
{:ok,_}=:inet.getaddr('to',:inet)
```
0 if connected, 1 otherwise.
[Answer]
# [PowerShell](https://github.com/PowerShell/PowerShell), 12 bytes
```
!!(irm g.gl)
```
[Answer]
## Python 2.7, 70 ~~77~~ Bytes
```
from urllib import*
a=1
try:urlopen('http://to.')
except:a=0
print a
```
```
import urllib as l
try: 
 l.urlopen('http://a.uk')
 print 1
except:
 print 0
```
Uses 1 for truthy, 0 for falsy. a.uk redirects to a motorbike clothing company. Saved 3 bytes by assigning to a variable and printing that. And another one for the "to." trick (confirmed to work with urllib), two for getting rid of the pesky indents.
[Answer]
# Julia + Bash (with dnsutils), 10 bytes
```
run(`dig`)
```
``command`` in julia creates a `cmd` object that can be run with `run`.
[Answer]
# [Arturo](https://arturo-lang.io/), 21 bytes
```
download"http://x.gl"
```
Outputs via exit code.
[Answer]
# [R](https://www.r-project.org/), 18 bytes
(2 bytes shorter than the [previous](https://codegolf.stackexchange.com/a/106845/86301) R answer; additionally, this solution does not require an external package to be installed.)
```
!is.null(nsl("a"))
```
[Try it online!](https://tio.run/##K/r/XzGzWC@vNCdHI684R0MpUUlT8/9/AA "R – Try It Online")
The `nsl` function looks up the IP address of a hostname. If there is no Internet connection, it outputs `NULL`, otherwise it outputs an IP address.
Note that we do not need to specify an existing domain name: when the domain name does not resolve (and as long as it does not end in a `.`), the domain name is instead appended to the server domain name. So we can use any 1-letter domain name, and the output of `nsl` is the non-NULL server IP. I think this behaviour is not specific to R.
[Answer]
# Scratch (scratchblocks3 syntax), 32 bytes
```
define
say(translate(1)to(Zulu v
```
"1" on success and an empty string on failure. Relies on Google Translate.
(Also, a slightly more amusing solution that actually outputs "true" but was unfortunately not optimal:)
```
define
say(translate(gwir)to(English v
```
[Answer]
# Clojure, 49 bytes
```
#(try(slurp"http://to.")1(catch Exception _ nil))
```
Returns `1` if it can connect, and `nil` otherwise.
Just attempts to `slurp` the page; throwing a `NoRouteToHostException` exception on failure, which is caught.
Unfortunately, the protocol and dot seem to be mandatory.
]