Shell Script Ping-w Usage

Source: Internet
Author: User

Today wrote a circular ping script to scan the network segment some of the IP address, the code is as follows:

#!/bin/bash

Ping_info () {

Ping-c 2-w 1 $;

if (($?! =0));

Then echo $ >>/tmp/iplist;

Fi

}

For i in ' seq 129 254 ';d O

ip_dx=192.168.1. $i

ip_lt=192.168.2. $i

ip_yd=192.168.3. $i

Ping_info $ip _DX

Ping_info $ip _lt

Ping_info $ip _yd

Done


After execution found a problem: regardless of the address can ping, are written to the/tmp/iplist, and the original intention of the script does not match.

After the command: Ping-c 2-w $1 Separate run with a pass-through IP and a non-pass IP to do the test, found that the value of the echo $? are all the same as the values in the. $. $. $.

Ping-c 2-w 1 IP:-C 2 means ping two packets, and-W 1 means the wait time is 1 seconds (in order to save time to execute the script), the problem is in this-W 1 here. Depending on the environment, the ping delay does not pass, ping two packets of time also does not pass. I use the time test here to ping two packets over a period of more than 1 seconds. So if you specify a time-out of 1 seconds, the second package may end up without a ping, so echo $? results are 1. After setting the time-out to 3 seconds, the script runs successfully.

#!/bin/bash

Ping_info () {

Ping-c 2-w 3 $;

if (($?! =0));

Then echo $ >>/tmp/iplist;

Fi

}

For i in ' seq 129 254 ';d O

ip_dx=192.168.1. $i

ip_lt=192.168.2. $i

ip_yd=192.168.3. $i

Ping_info $ip _DX

Ping_info $ip _lt

Ping_info $ip _yd

Done


This article is from the "I am very simple" blog, please be sure to keep this source http://easyyx.blog.51cto.com/8603153/1734825

Shell Script Ping-w Usage

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.