首页 > 其他分享 >验证某台主机是否在线

验证某台主机是否在线

时间:2023-03-19 11:36:28浏览次数:20  
标签:某台 在线 exists 主机 ping echo host IP

通过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脚本

 

标签:某台,在线,exists,主机,ping,echo,host,IP
From: https://www.cnblogs.com/freeasyman/p/17232666.html

相关文章