验证某台主机是否在线

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

 

posted @   LiveOrDie  阅读(43)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
点击右上角即可分享
微信分享提示