JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

linux写一个shell脚本去进行ping探测

wys521 2025-01-03 19:27:40 精选教程 23 ℃ 0 评论


可以用shell脚本去执行一些功能,

比如批量批一个网段的主机

代码如下:

root@owaspbwa:~# cat ping.sh

#!/bin/bash


for s in {1..100};

do

host=192.168.198.$s

ping -c3 $host >/dev/null

if [ $? = 0 ]

then

echo "192.168.198.$s is a available host"

else

echo "192.168.198.$s is not a available host"

fi

done


添加执行权限


root@owaspbwa:~# chmod +x ping.sh



效果如下:


root@owaspbwa:~# ./ping.sh

192.168.198.1 is a available host

192.168.198.2 is not a available host

192.168.198.3 is not a available host

192.168.198.4 is not a available host

192.168.198.5 is not a available host

192.168.198.6 is not a available host

192.168.198.7 is not a available host

192.168.198.8 is not a available host

192.168.198.9 is not a available host

192.168.198.10 is not a available host

192.168.198.11 is not a available host

如果有很多个,可能按ctrl+c 结束不了这个脚本的执行,可以用kill 9 进程号来结束脚本的执行

root@owaspbwa:~# ps -a | grep ping.sh

11982 pts/0 00:00:00 ping.sh

root@owaspbwa:~# kill -9 11982

root@owaspbwa:~#


本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表