通過ping驗證某臺主機是否在線;
- ping命令
NAME:send ICMP ECHO-REQUEST to network hosts
OPTIONS:
-c count:Stop after sending count ECHO_REQUEST packets.
-i interval:Wait interval seconds between sending each packets.The default is to wait for one second between each packets normally.
-v:verbose output.
- 參數說明
$0:對應當前shell腳本的名稱
$#:參數數量,一共有多少個參數
$*:所有位置的參數值
$1、$2.....:分別對應着第N個位置的參數值
$?:上一次命令的執行返回值,命令正確執行返回0,命令執行出錯返回非0
- shell腳本
#! /bin/bash # Detect whether the host is alive IP=39.105.190.238 ping -c 4 -i 1 $IP >> /dev/null [ $? -ne 0 ] && echo "host $IP does not exist" || echo "host $IP exists" IP=2.2.2.2 ping -c 4 -i 1 $IP >> /dev/null if [ $? -eq 0 ] then echo "host $IP exists" else echo "host $IP does not exists" fi
- python腳本