[英]Bash script to Process two files and loop through to have mountpoint check
[英]Bash Script to loop through list to check server status
我正在嘗試創建一個腳本,該腳本將在列表中的遠程服務器中執行 ping 操作。
這就是我所擁有的,但不斷收到此錯誤:
./monitor_sats.sh:無法為命令替換制作管道:打開的文件太多。
這是我的代碼,感謝您的幫助。
#!/bin/bash
function ping {
for i in `cat server_list`
do
ping -c1 ${i} > /dev/null
if [ ${?} -eq 0 ]; then
echo "$(tput setaf 2)ON$(tput sgr0)"
else
echo "$(tput setaf 1)OFF$(tput sgr0)"
fi
done
}
echo "AMSTERDAM - Server $(ping) "
echo "HONG KONG - Server $(ping) "
echo "LONDON - Server $(ping) "
echo "SINGAPORE - Server $(ping) "
更改函數名稱如下;
#!/bin/bash
function pingToServer {
for i in `cat server_list`
do
ping -c1 ${i} > /dev/null
if [ ${?} -eq 0 ]; then
echo "$(tput setaf 2)ON$(tput sgr0)"
else
echo "$(tput setaf 1)OFF$(tput sgr0)"
fi
done
}
echo "AMSTERDAM - Server $(pingToServer) "
echo "HONG KONG - Server $(pingToServer) "
echo "LONDON - Server $(pingToServer) "
echo "SINGAPORE - Server $(pingToServer) "
你也可以使用這個;
#!/bin/bash
Countries=("AMSTERDAM" "HONG KONG" "LONDON" "SINGAPORE")
counter=0
cat server_list | while read server;
do
ping -c1 ${server} > /dev/null
if [ ${?} -eq 0 ]; then
echo "${Countries[$counter]} - SERVER- $(tput setaf 2)ON$(tput sgr0)"
else
echo "${Countries[$counter]} - SERVER-$(tput setaf 1)OFF$(tput sgr0)"
fi
counter=$(($counter+1))
done
這個腳本在語法上看起來是正確的,但有一些問題:為什么要為不同的城市 ping 相同的服務器列表?
這讓我覺得你沒有給出整個背景......
另外,你能給出ulimit -a
的輸出嗎? 並告訴列表的大小?
for i in $(cat list_server) ;
do ping -c 1 -w 1 $i > /dev/null && echo "$i - OK" || echo "$i - NOK" ;
done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.