bash shell脚本以使用IPS监视文件
我希望构建一个脚本,在上下时,我可以在其中用ping命令监视IP的文件。
我在Stackoverflow上找到了两种出色的方法,我正在尝试将它们结合起来,但是无论我做什么都行不通。我正在阅读Man Shell也可以学习以后的学习,但我认为我缺少一些东西,无法使它起作用。
脚本1:
我似乎找不到我在stackoverflow上找到的脚本,但在此资源下找到了相同的脚本:(bash and ping)部分: https://jmanteau.fr.fr.fr.fr.fr.fr.fr.fr /posts/ping的facets/#check-if-if-host-hosts-are-alive
这个惊人的脚本可以在并行
#!/bin/bash
argc=$#
if [ $# -lt 1 ]
then
echo "Usage: $0 <ip-list-file>"
exit 1
fi
hosts=$1
function customping
{
DATE=$(date '+%d/%m/%Y %H:%M:%S')
ping -c 1 -W 1 $1 >/dev/null 2>&1 && echo "$DATE Node $1 is UP" || echo -e "\033[1;31m $DATE Node $1 is DOWN \033[0m"
# ping -c 1 -W 1 $1 >/dev/null 2>&1 && echo "$DATE Node $1 is UP" || echo "$DATE Node $1 is DOWN"
# sleep 0.01s
}
T="$(date +%s%N)"
DEFAULT_NO_OF_PROC=8
noofproc=$DEFAULT_NO_OF_PROC
if [ -n "$2" ] #user-set no. of process instead of default
then
noofproc=$2
echo "Max processes: $noofproc"
fi
export -f customping && cat $hosts | xargs -n 1 -P $noofproc -I{} bash -c 'customping {}' \;
脚本2:
这个令人惊叹的脚本通过IP
function check_health {
set 192.168.10.1 192.168.10.2 192.168.10.3 192.168.10.4 192.168.10.5 192.168.10.6 192.168.10.7 192.168.10.8 192.168.10.9 192.168.10.10 192.168.10.11 192.168.10.12 192.168.10.13
trap exit 2
for ipnumber in "$@"; do
DATE=$(date '+%d/%m/%Y %H:%M:%S')
ping -c 1 -t 1 $ipnumber > /dev/null
[ $? -eq 0 ] && echo -e "|\033[1;36m $DATE \033[0m" "|\033[1;32m Node |"$ipnumber "| UP \033[0m" | column -t -s "|"
done
while true; do
i=1
for ipnumber in "$@"; do
statusname=up$i
laststatus=${!statusname:-0}
ping -c 1 -t 1 $ipnumber > /dev/null
ok=$?
eval $statusname=$ok
if [ ${!statusname} -ne $laststatus ]; then
# echo $DATE Status changed for $ipnumber
DATE=$(date '+%d/%m/%Y %H:%M:%S')
if [ $ok -eq 0 ]; then
echo -e "|\033[1;36m $DATE \033[0m" "|\033[1;32m Node |"$ipnumber "| UP \033[0m" | column -t -s "|"
else
echo -e "|\033[1;36m $DATE \033[0m" "|\033[1;31m Node |"$ipnumber "| DOWN \033[0m" | column -t -s "|"
fi
fi
i=$(($i + 1))
done
# sleep 1
done
}
stackoverflow.com/a/470831/19313640" > https://stackoverflow.com/a/a/4708831/19313640 将这2个脚本放在一起,并将其完全并行为第一个脚本,并通过阅读文件而不是第二个脚本中的“设置”,还可以使用第二个脚本的监视功能。
编辑:如果使此工作至少如何使第二个脚本像第一个脚本一样读取文件是复杂的?
我希望我很彻底,并提供了足够的信息,以了解我要做的事情。
谢谢。
更新:
您好,我设法使其在凌乱的代码中起作用。
#!/bin/bash
trap exit 2
argc=$#
if [ $# -lt 1 ]
then
echo "Usage: $0 <ip-list-file>"
exit 1
fi
hosts=$1
function check_live {
trap exit 2
DATE=$(date '+%d/%m/%Y %H:%M:%S')
ping -c 1 -t 1 $1 > /dev/null
[ $? -eq 0 ] && echo -e "|\033[1;36m $DATE \033[0m" "|\033[1;32m Node |"$1 "| UP \033[0m" | column -t -s "|"
# sleep 1
}
function check_health {
trap exit 2
# DATE=$(date '+%d/%m/%Y %H:%M:%S')
# ping -c 1 -t 1 $1 > /dev/null
# [ $? -eq 0 ] && echo -e "|\033[1;36m $DATE \033[0m" "|\033[1;32m Node |"$1 "| UP \033[0m" | column -t -s "|"
# sleep 3
while true; do
# while read line; do
# i="$i $line"
i=1
for ipnumber in "$@"; do
statusname=up$i
laststatus=${!statusname:-0}
ping -c 1 -t 1 $ipnumber > /dev/null
ok=$?
eval $statusname=$ok
if [ ${!statusname} -ne $laststatus ]; then
# echo $DATE Status changed for $ipnumber
DATE=$(date '+%d/%m/%Y %H:%M:%S')
if [ $ok -eq 0 ]; then
echo -e "|\033[1;36m $DATE \033[0m" "|\033[1;32m Node |"$ipnumber "| UP \033[0m" | column -t -s "|"
else
echo -e "|\033[1;36m $DATE \033[0m" "|\033[1;31m Node |"$ipnumber "| DOWN \033[0m" | column -t -s "|"
fi
fi
i=$(($i + 1))
done
# sleep 1
done
}
function duck_art {
textred=$(tput setaf 1)
textcyan=$(tput setaf 12)
textyellow=$(tput setaf 11)
textpurple=$(tput setaf 4)
textpink=$(tput setaf 5)
textwhite=$(tput setaf 7)
textgray=$(tput setaf 8)
textgreen=$(tput setaf 10)
echo -e ${textpink} ================================================================
cat <<EOM
${textyellow}
EOM
cat << "EOF"
__ __
___( o)> DuckLab <(o )___
\ <_. ) Monitor ( ._> /
`---' `---'
EOF
echo -e ${textpink} ================================================================
echo -e ${textyellow} " Press <CTRL+C> to exit. "
echo -e ${textpink} ================================================================
echo -e "\033[1;36m $internal_ip \033[0m" " ${textpink}| " "\033[1;36m $my_name \033[0m" " ${textpink}| " "\033[1;36m $external_ip \033[0m"
echo -e ${textpink} ================================================================
}
external_ip=$(curl -s ifconfig.me)
internal_ip=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')
my_name=$(hostname)
function multi_process_live {
T="$(date +%s%N)"
DEFAULT_NO_OF_PROC=8
noofproc=$DEFAULT_NO_OF_PROC
if [ -n "$2" ] #user-set no. of process instead of default
then
noofproc=$2
echo "Max processes: $noofproc"
fi
export -f check_live && cat $hosts | xargs -n 1 -P $noofproc -I{} bash -c 'check_live {}' \; 2>/dev/null
}
function multi_process_health {
T="$(date +%s%N)"
DEFAULT_NO_OF_PROC=8
noofproc=$DEFAULT_NO_OF_PROC
if [ -n "$2" ] #user-set no. of process instead of default
then
noofproc=$2
echo "Max processes: $noofproc"
fi
export -f check_health && cat $hosts | xargs -n 1 -P $noofproc -I{} bash -c 'check_health {}' \; 2>/dev/null
}
# ================================ End of fucntions ================================
# ================================ Start of Script =================================
clear
duck_art
multi_process_live
multi_process_health
# ================================ End of Script ===================================
检查实时主机工作的第一个功能。 循环循环的第二个功能显示初始输出,但未正确循环通过文件的每一行,以监视哪些主机向上或向下并打印输出。它仅适用于第二行,我认为它不能正确读取文件的每行。
使这项工作和学习的任何想法,改进和建议都受到高度重视。
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,原始脚本的魔法是使用
XARGS -P $ NOOFPROC
并行运行检查。在不知道您的主机列表的情况下,很难猜测您在第一行中看到什么问题。我最好的猜测是,对于您的第一个条目,
ping
退出代码不是零,它没有退出任何东西。我看到了您最新的
check_health
功能的一些问题。虽然true
循环,正如您,这是没有意义的,因为您还是一次将其调用一个条目。
check_health
将以i = 1
开始这些调用中的每一个都将设置相同的状态名称
(up1
)最终将被使用。这是我建议的(我希望您不要介意我从脚本中删除了非必需品位):
运行初始实时检查,然后运行Multi_process_health功能。
关键是两件事。首先,使用
awk'{print fnr“” $ 1}'
,它符合主机列表:进入此:
第二,
xargs -n2
的使用使其每一个都如此呼叫check_health
将序列编号作为第一个参数,而实际主机作为第二个参数。A note that the magic of the original script was in using
xargs -P $noofproc
, to run the check in parallel.Without knowing what your list of hosts looks like, it is difficult to guess what problem you're seeing with the first line. My best guess is that for your first entry the
ping
exit code was non-zero and it just didn't exit anything.I see a few issues with your most recent
check_health
function.while true
loop, as you notedfor ipnumber in "$*"
which makes no sense since you're calling it with one entry at a time anyway.check_health
will start withi=1
, so even if thewhile true
wasn't there each of these calls would be setting the samestatusname
(up1
) will end up getting used.Here is what I would suggest (I hope you don't mind that I removed the non-essential bits from your script):
That runs the initial live check and then runs the multi_process_health function.
The key is two things. First the use of
awk '{print FNR " " $1}'
which turns a list of hosts:Into this:
Second the use of
xargs -n2
which makes it so each call tocheck_health
is given the sequence number as the first parameter and the actual host as the second parameter.如果主机数量适合您的屏幕,请尝试以下操作:
如果您没有并行-20220622,则可能足够:
If the number of hosts fit on your screen, then try this:
If you do not have parallel-20220622 this might be enough: