Linux bash ping check

Bash ping script file for checking host availability

I am trying to write a bash script in a file that would, when run start pinging a host until it becomes available, when the host becomes reachable it runs a command and stops executing, I tried writing one but the script continues pinging until the count ends, Plus I need to put that process in the background but if I run the script with the dollar ( $ ) sign it still runs in foreground,

#!/bin/bash ping -c30 -i3 192.168.137.163 if [ $? -eq 0 ] then /root/scripts/test1.sh exit 0 else echo “fail” fi 

6 Answers 6

I would use this, a simple one-liner:

while ! ping -c1 HOSTNAME &>/dev/null; do echo "Ping Fail - `date`"; done ; echo "Host Found - `date`" ; /root/scripts/test1.sh 

Replace HOSTNAME with the host you are trying to ping.

I missed the part about putting it in the background, put that line in a shellscript like so:

#!/bin/sh while ! ping -c1 $1 &>/dev/null do echo "Ping Fail - `date`" done echo "Host Found - `date`" /root/scripts/test1.sh 

And to background it you would run it like so:

nohup ./networktest.sh HOSTNAME > /tmp/networktest.out 2>&1 & 

Again replace HOSTNAME with the host you are trying to ping. In this approach you are passing the hostname as an argument to the shellscript.

Just as a general warning, if your host stays down, you will have this script continuously pinging in the background until you either kill it or the host is found. So I would keep that in mind when you run this. Because you could end up eating system resources if you forget about this.

Источник

How to Check Availability by Using ping in Bash Scripts

ping is a command-line tool used to test connectivity between two network devices, typically over the Internet Protocol (IP) network. It sends ICMP (Internet Control Message Protocol) packets to the target host and measures the response time. In Bash scripts, the ‘ping’ command can be used to check the availability of a host.

In this article, we will discuss how to use ‘ping’ in Bash scripts to check the availability of a host.

Checking Host Availability Using ping in Bash

The ‘ping’ command in Bash can be used to check the availability of a host, and the syntax for using ‘ping’ is as follows:

The options available for the ‘ping’ command are:

‘-c count’: determines how many packets to send.

‘-i interval’: Specifies the interval between sending packets, in seconds.

t ttl’: Specifies the Time To Live (TTL) value for the packets.

w deadline’: specifies in seconds how long you must wait for a response.

‘destination’: This parameter specifies the hostname or IP address of the target host to ping.

Here is an illustration of how to use “ping” to determine whether a host is accessible:

ping -c 1 $HOST > / dev / null

echo «Host $HOST is available»

echo «Host $HOST is not available»

Here I have first defined the host that we want to ping, which is ‘google.com’ and then used the ‘ping’ command to send a single packet to the host. The output of the ‘ping’ command is redirected to /dev/null to suppress any output to the terminal.

Читайте также:  Mobility radeon hd 5650 linux

Ne,xt I have checked the exit status of the ‘ping’ command using the ‘$?’ variable and if the exit status is 0, it means that the host is up, and we print the message “$HOST is available”. If the exit status is non-zero, it means that the host is down and the message “$HOST is not available” is printed.

Conclusion

The ‘ping’ command is a simple and effective way to check the connectivity between two devices on a network. By following the examples provided in this article, you can start using ‘ping’ in your own Bash scripts to check the availability of hosts.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.

Источник

9 commands to check if connected to internet with shell script examples

In this article I will share multiple commands and examples to check if connected to internet for your Linux machine. At the end of the article I will also share some example to test internet connection from the Linux to external network using shell script.

But before we do that there are certain things you must take care of, such as

  • You have IP Address assigned to your network interface (static or dhcp)
  • Your gateway is reachable from the Linux system
  • If you are on virtual machine then make sure your VM is configured to be able to connect external network
  • Your DNS is properly configured

Now there are multiple commands which are available in Linux which can be used to check if connected to internet, we will try to discuss some of them here in brief.

1. Ping Test

The very first tool I use to check if connected to internet is via ping utility . But do you know how to check ping or perform ping check to check if connected to internet? Ping is part of iputils rpm so make sure iputils is installed on your setup

To perform ping check, try to ping any page on internet such as google.com

# ping -c 1 google.com PING google.com (172.217.166.110) 56(84) bytes of data. 64 bytes from maa05s09-in-f14.1e100.net (172.217.166.110): icmp_seq=1 ttl=49 time=46.9 ms --- google.com ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 46.923/46.923/46.923/0.000 ms

Here we use -c 1 with ping tool to send only one packet . Now check the statistics section, we see 1 packet was transmitted and received and there was 0% packet loss so it means we are connected to internet.

If you execute ping check without -c argument then ping will continue to transmit packets, press Ctrl+C to stop the transmission any time and observe the statistics for «packet loss»

If you plan to check if connected to internet via shell script then you can use below, anything in the output other than «0» means not connected to internet.

# ping -c 1 -q google.com >&/dev/null; echo $? 0

If your system is using proxy server to connect to internet then ping check will not work. You can use other commands from this article to check if connected to internet

Читайте также:  Linux resize ntfs resize

2. Check port availability using cat, echo..

There are various tools which can be used to check port availability which I will share in this article. But many of them are not installed by default so you may have to install them manually. Alternatively you can use below command to check if connected to internet without installing any additional rpm

If the output is 0 then it means you are connected to internet, but if the output is something like below

# cat < /dev/null >/dev/tcp/8.8.8.8/53; echo $? -bash: connect: Network is unreachable -bash: /dev/tcp/8.8.8.8/53: Network is unreachable 1

Then your linux node is not connected to internet.

Alternatively you can also use

# echo -n >/dev/tcp/8.8.8.8/53; echo $? 0

3. DNS lookup using nslookup, host etc..

You can perform a DNS lookup any web page address to check if connected to internet. With a successful DNS lookup you should get a response something like below.

# nslookup google.com Server: 192.168.0.10 Address: 192.168.0.10#53 Non-authoritative answer: Name: google.com Address: 172.217.166.110 Name: google.com Address: 2404:6800:4007:811::200e

For a failed DNS lookup you should get something like

# nslookup google.com1 Server: 8.8.8.8 Address: 8.8.8.8#53 ** server can't find google.com1: NXDOMAIN

Now getting this error » server can’t find XXXX: NXDOMAIN » doesn’t specifically mean that the node is not connected to internet, it may also mean that the provided server XXX is invalid. So try to lookup a domain which you know is UP to avoid false positive.

There are many more commands to perform DNS lookup such as host, dig etc

4. Curl

curl is a tool to transfer data from or to a server, using one of the many supported protocols such as HTTP, FTP etc. We can also use this tool to query a webpage and test internet connection in Linux.

# curl -Is http://www.google.com | head -n 1 HTTP/1.1 200 OK

Here if you receive anything other than 200 OK then it means the server failed to connect to the provided page. So unless you provide an invalid webpage, your node is connected to internet

5. Telnet

Telnet is another tool to check port connectivity so you can check port availability of any webpage from the Linux node to check if connected to internet. We can try connecting port 53 of Google DNS to check internet connection.

# telnet 8.8.8.8 53 Trying 8.8.8.8. Connected to 8.8.8.8. Escape character is '^]'. Connection closed by foreign host.

As you see the session was » connected «. If you get an output like below

# telnet 8.8.8.8 53 Trying 8.8.8.8. telnet: connect to address 8.8.8.8: Network is unreachable

then it means there is a problem with internet connectivity.

6. Nmap

nmap is normally a port scanner to check the list of open ports on a system. We will use this to connect to external network to scan the port. If it is able to connect to the external network for port scanning then we can check if connected to internet.

# nmap -p 443 google.com Starting Nmap 7.70 ( https://nmap.org ) at 2019-11-19 07:40 IST Nmap scan report for google.com (172.217.166.110) Host is up (0.054s latency). Other addresses for google.com (not scanned): 2404:6800:4007:811::200e DNS record for 172.217.166.110: maa05s09-in-f14.1e100.net PORT STATE SERVICE 443/tcp open https Nmap done: 1 IP address (1 host up) scanned in 0.77 seconds

Here I am scanning google.com on port 443. As you see highlighted section, nmap was able to establish connection with google.com

Читайте также:  Удалить директорию со всем содержимым linux

7. netcat or nc

In some variant of Linux you will find netcat while in others nc , you can use either of these tools for port scanning. nc or ncat is part of nmap-ncat rpm. Here we use nc command to check connection to google.com on port 443

# nc -vz google.com 443 Ncat: Version 7.70 ( https://nmap.org/ncat ) Ncat: Connected to 172.217.166.110:443. Ncat: 0 bytes sent, 0 bytes received in 0.07 seconds.

8. wget

GNU Wget is a free utility for non-interactive download of files from the Web. But we can also use this to check if connected to internet.

# wget -q --spider http://google.com ; echo $? 0

Here with —spider Wget will behave as a Web spider, which means that it will not download the pages, just check that they are there
With echo $? we check the exit status, anything other than 0 in the output means your system is not connected to internet

9. Traceroute

The traceroute command shows every step taken on the route from your machine to a specified host. Assuming you are having problems connecting to www.google.com so you can use traceroute to check all the hubs taken to reach the destination server. This will also test internet connection.

# traceroute www.google.com traceroute to www.google.com (216.58.196.164), 30 hops max, 60 byte packets 1 _gateway (192.168.0.1) 3.046 ms 2.873 ms 3.291 ms 2 10.249.0.1 (10.249.0.1) 4.148 ms 4.676 ms 4.678 ms 3 broadband.actcorp.in (202.83.20.173) 5.153 ms 5.445 ms 6.305 ms 4 broadband.actcorp.in (202.83.20.181) 4.952 ms 6.212 ms 6.195 ms 5 * * * 6 72.14.194.18 (72.14.194.18) 48.202 ms 49.669 ms 49.504 ms 7 108.170.253.113 (108.170.253.113) 50.061 ms 108.170.253.97 (108.170.253.97) 48.480 ms 48.512 ms 8 216.239.43.239 (216.239.43.239) 49.911 ms 56.915 ms 216.239.43.235 (216.239.43.235) 57.610 ms 9 * * * 10 * * * 11 * * * 12 * * *

Here we were able to trace the route to google.com upto a certain point.

What do those * * * mean?

Each one indicates a five-second timeout at that hop. Sometimes that could indicate that the machine simply doesn’t understand how to cope with that traceroute packet due to a bug, but a consistent set of * indicates that there’s a problem somewhere with the router to which 216.239.43.239 hands off packets.

On a Linux system with no internet connection, you would face problem with DNS resolution itself

# traceroute www.google.com www.google.com: Name or service not known Cannot handle "host" cmdline arg `www.google.com' on position 1 (argc 1)

How To check if connected to internet using shell script?

You can easily use any or all the above commands/methods to check if connected to internet using bash/shell script.

For example by using wget you can test internet connection in the below shell script:

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

Here this script will print the status of the internet, similarly for other scripts you can check the exit status and perform tasks accordingly.

Lastly I hope the steps from the article to check if connected to internet (test internet connection) on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Источник

Оцените статью
Adblock
detector