Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test an Internet connection with bash?

How can an internet connection be tested without pinging some website? I mean, what if there is a connection but the site is down? Is there a check for a connection with the world?

like image 744
lauriys Avatar asked May 30 '09 08:05

lauriys


People also ask

How do I test a Linux server connection?

ping. The ping command is the simplest and most often used command for doing basic connectivity testing. It sends out packets called echo requests and are packets that request a response.

How do I know if my internet is not working in CMD?

Step 1: In the search bar type “cmd” (Command Prompt) and press enter. This would open the command prompt window. “netstat -a” shows all the currently active connections and the output display the protocol, source, and destination addresses along with the port numbers and the state of the connection.


2 Answers

Without ping

#!/bin/bash  wget -q --spider http://google.com  if [ $? -eq 0 ]; then     echo "Online" else     echo "Offline" fi 

-q : Silence mode

--spider : don't get, just check page availability

$? : shell return code

0 : shell "All OK" code

Without wget

#!/bin/bash  echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1  if [ $? -eq 0 ]; then     echo "Online" else     echo "Offline" fi 
like image 190
user3439968 Avatar answered Sep 22 '22 06:09

user3439968


Ping your default gateway:

#!/bin/bash ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null && echo ok || echo error 
like image 23
mateusza Avatar answered Sep 22 '22 06:09

mateusza