测试IP能够ping通,但只有一个循环结束的时间。 [英] Test ip is able to ping, but only one loop it ended.
问题描述
如果我想进入一个IP来平,如果它能够ping通,我希望进入一个不同的IP地址。我应该如何去改剧本?
看看我的剧本,如果我输入的IP 192.168.0.1能ping通,我想进入一个不同的IP。我应该如何去补充。
回声==============平IP ==============
呼应-n请输入IP ping通192.168.0。
阅读IPADDRESS
Addip = 192.168.0。$ bmcipaddress
回声================================
回声IP地址:$ Addip
平-c 1 $ Addip>中pinglog
ping_ok =`猫pinglog| grep的Unreachable`
回声=======
如果[$ ping_ok==];然后
回声$ bmcip==> PING通
其他
回声$ bmcip==>平BMC FAIL科幻
试试这个。
而读-p请输入IP ping通192.168.0。 IP地址;做
bmcip =192.168.0。$ ip地址
如果平-c 1$ bmcip>的/ dev / null的;然后
状态=PASS
其他
状态失败
科幻
回声$ bmcip ==>平$状态
DONE
的读
命令将失败在文件末尾,这样你就可以轻松地终止与CTRL-D脚本(以及,当然,CTRL-C)。您可以轻松地扩展它,也退出对空的输入。
从退出状态的ping
表示它是否成功,所以这次捕获更多的错误情景不仅仅是无法接通。一般情况下,壳工具,以利用这种条件结构,看看命令是否成功pcisely返回退出状态$ P $,你应该避免硬编码输出字符串,如果你能(它们可以本地化,变化版本等)之间。
如果您想进一步减少这一点,你可以挤下来到
而读-p请输入IP ping通192.168.0。 IP地址;做
Addip =192.168.0。$ ip地址
回声-n$ bmcip ==>平
平-c 1$ Addip>的/ dev / null的&放大器;&安培;回声PASS||回声FAIL
DONE
由于不使用任何东西日志文件的脚本之外,我认为这是一个不必要的临时文件,我们最好不要创建。如果你需要它的诊断,也许你不应该覆盖。
If i wants to enter a ip to ping, if it is able to ping, i wish to enter a difference ip. How am i going to change the script? Look at my script, if i enter ip 192.168.0.1 is able to ping, i wants to enter a different ip. HOw am i going to add.
echo "==============Ping IP=============="
echo -n "Please enter ip to ping 192.168.0."
read ipaddress
Addip=192.168.0.$bmcipaddress
echo "================================"
echo "IP Add: $Addip"
ping -c 1 $Addip >"pinglog"
ping_ok=`cat "pinglog"| grep Unreachable`
echo "======="
if [ "$ping_ok" == "" ]; then
echo $bmcip "==>ping PASS"
else
echo $bmcip "==>ping BMC FAIL"
fi
Try this.
while read -p "Please enter ip to ping 192.168.0." ipaddress; do
bmcip="192.168.0.$ipaddress"
if ping -c 1 "$bmcip" >/dev/null; then
status="PASS"
else
status"FAIL"
fi
echo "$bmcip ==>ping $status"
done
The read
command will fail at end of file, so you can easily terminate the script with ctrl-D (as well as, of course, ctrl-C). You could easily extend it to also exit on empty input.
The exit status from ping
indicates whether or not it succeeded, so this catches more error scenarios than just "Unreachable". In general, shell tools return an exit status precisely in order to use this sort of conditional construct to see whether or not a command succeeded, and you should avoid hard-coding output strings if you can (they could be localized, change between versions, etc).
If you want to minimize this further, you could squeeze it down to
while read -p "Please enter ip to ping 192.168.0." ipaddress; do
Addip="192.168.0.$ipaddress"
echo -n "$bmcip ==>ping "
ping -c 1 "$Addip" >/dev/null && echo "PASS" || echo "FAIL"
done
Since you are not using the log file for anything outside of your script, I assume it's an unnecessary temporary file which we are better off not creating. If you need it for diagnostics, maybe you should not overwrite it.
这篇关于测试IP能够ping通,但只有一个循环结束的时间。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!