Shell script testing of host connectivity in a certain network segment

Source: Internet
Author: User

1. Testing the connectivity of the 192.168.4.0/24 entire network segment (while version)

#!/bin/bash#author: The Adventures of Tintin (Jacob) #定义变量i控制循环次数, I cycle from 1, once per loop, I self plus 1, until I equals 254 loop exit # Ping An IP in each loop, if Ping Pass, Prompt host is up # If you cannot ping the same, prompt the host is down the #ping command's-C option to control the number of ping tests,-C2 for the target host to perform 2 ping Test #ping command of the-I option, the time interval to control multiple ping tests default to 1 seconds,- i0.3 can improve the test efficiency of ping #ping the-w option of the command, you can control the time-out, the default one host cannot ping, need to wait a long time out, #才会提示无法ping通, using-W1, set the time-out to 1 seconds. i=1while  [  $i   -le  254 ]do           ping -c2 -i0.3 -w1 192.168.4. $i   &>/dev/null          if [ $? –eq 0 ];then                echo  "192.168.4. $i  is  up "         else                echo   "192.168.4. $i  is down"           fi          let i++done 

2. Test the connectivity of the 192.168.4.0/24 entire network segment (for version)

#!/bin/bash#author: The Adventures of Tintin (Jacob) #定义for循环变量i, execution cycles are 254 times, I cycle from 1 to 254# each cycle to a target host test ping connectivity, The syntax format of the ping command references the previous while version for I in {1..254}do ping-c2-i0.3-w1 192.168.4. $i &>/dev/null if [$? –eq 0];then echo "192.168.4. $i are up" else echo "192.168.4. $i are down" Fidon E

3. Test 192.168.4.0/24 connectivity across the entire network segment (multi-process version)

#!/bin/bash#author: The Adventures of Tintin (Jacob) #定义一个函数, ping a host and detect the host's survival status, ping syntax format refer to the previous while version myping () {PING-C2-I0.3-W1 $ & >/dev/nullif [$?-eq 0];thenecho "is up" Elseecho "was down" fi}for i in {1..254}do myping 192.168.4.$ I &done# use the & symbol to put the executed function into the background # The benefit of this is that you do not have to wait for the ping first host to respond, you can continue to ping the second host concurrently, and so on.


This article is from the "Ding Ding Adventures" blog, please be sure to keep this source http://manual.blog.51cto.com/3300438/1971293

Shell script testing of host connectivity in a certain network segment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.