我编写了以下脚本来检查我的服务器:
#!/bin/bash
rm -f /tmp/res-ok.txt 1>/dev/null 2>/dev/null
rm -f /tmp/res-failed.txt 1>/dev/null 2>/dev/null
echo "***********************************************"
echo "*+++++++++++++++++++++++++++++++++++++++++++++*"
echo "*+ +*"
echo "*+ PINGING SERVERS: Is UP OR DOWN +*"
echo "*+ +*"
echo "*+++++++++++++++++++++++++++++++++++++++++++++*"
echo "***********************************************"
echo " ================ ============ "
echo "| IP ADDRESS | | STATUS |"
echo " ================ ============ "
while read ip; do
ping $ip -s 1 -c 1 1>/dev/null 2>&1
if [ $? -eq 0 ]; then
echo " $ip UP"
echo $ip >> /tmp/res-ok.txt
elif [ $? -ne 0 ]; then
echo " $ip DOWN"
echo $ip >> /tmp/res-failed.txt
fi
echo "-----------------------------------------------"
done <<____HERE
____HERE
ok=`wc -l /tmp/res-ok.txt | awk '{sum += $1} END {print sum}'`
failed=`wc -l /tmp/res-failed.txt | awk '{sum += $1} END {print sum}'`
echo You have $ok servers UP
echo You have $failed servers DOWN
如何告诉我的脚本在特定位置显示STATUS column
[第二列]?[确切]
我想我应该控制游标。如何?
任何的想法?
谢谢
答案 0 :(得分:1)
如果我正确理解您的问题,您的布局会被破坏,因为您有不同长度的IP地址。根据它们的不同程度,您可以通过使用制表符而不仅仅是空白来获得更合适的布局:
echo -e " $ip\t\t\t\tDOWN"
...或通过列:
输出您的输出output="$output `echo -e "\n$ip UP"`"
...
## Outputting your output:
echo -e "$output" | column -t
结果:
172.16.2.4 UP
172.16.2.5 UP
192.168.178.200 UP
192.168.178.254 UP
为了使后者看起来很完美,您可能需要将welcome-header的宽度调整为输出的最终宽度。
您还可以通过添加分隔符在字段之间添加更多空白列:
output="$output `echo -e "\n$ip - UP"`"
结果:
172.16.2.4 - UP
172.16.2.5 - UP
192.168.178.200 - UP
192.168.178.254 - UP
192.168.178.254 - UP
因此,在这些更改后,您的脚本应如下所示:
#!/bin/bash
rm -f /tmp/res-ok.txt 1>/dev/null 2>/dev/null
rm -f /tmp/res-failed.txt 1>/dev/null 2>/dev/null
echo "***********************************************"
echo "*+++++++++++++++++++++++++++++++++++++++++++++*"
echo "*+ +*"
echo "*+ PINGING SERVERS: Is UP OR DOWN +*"
echo "*+ +*"
echo "*+++++++++++++++++++++++++++++++++++++++++++++*"
echo "***********************************************"
echo " ============= ============ "
echo "| IP ADDRESS | | STATUS |"
echo " ============= ============ "
output="" ## Unset the variable, in case it still contains anything.
while read ip; do
ping $ip -s 1 -c 1 1>/dev/null 2>&1
if [ $? -eq 0 ]; then
output="$output `echo "\n$ip - UP"`"
echo $ip >> /tmp/res-ok.txt
elif [ $? -ne 0 ]; then
output="$output `echo "\n$ip - DOWN"`"
echo $ip >> /tmp/res-failed.txt
fi
echo "-----------------------------------------------"
done <<____HERE
____HERE
echo -e "$output" | column -t
ok=`wc -l /tmp/res-ok.txt | awk '{sum += $1} END {print sum}'`
failed=`wc -l /tmp/res-failed.txt | awk '{sum += $1} END {print sum}'`
echo You have $ok servers UP
echo You have $failed servers DOWN
答案 1 :(得分:0)
最终答案:
#!/bin/bash
if [ $# -ne 1 ]; then
echo "USAGE: ./server-status.sh <ip-list>"
exit 1
fi
rm -f /tmp/whole.txt 1>/dev/null 2>/dev/null
rm -f /tmp/res-ok.txt 1>/dev/null 2>/dev/null
rm -f /tmp/res-failed.txt 1>/dev/null 2>/dev/null
echo "***********************************************"
echo "*+++++++++++++++++++++++++++++++++++++++++++++*"
echo "*+ +*"
echo "*+ PINGING SERVERS: Is UP OR DOWN +*"
echo "*+ +*"
echo "*+++++++++++++++++++++++++++++++++++++++++++++*"
echo "***********************************************"
echo " ================ ============ "
echo "| IP ADDRESS | | STATUS |"
echo " ================ ============ "
output=""
while read ip; do
ping $ip -s 1 -c 1 -W 1 1>/dev/null 2>&1
if [ $? -eq 0 ]; then
output="$output `echo "\n$ip -------------------- UP"`"
echo $ip >> /tmp/res-ok.txt
elif [ $? -ne 0 ]; then
output="$output `echo "\n$ip -------------------- DOWN"`"
echo $ip >> /tmp/res-failed.txt
fi
done < $1
echo -e "$output" | column -t
cat /tmp/res-ok.txt >> /tmp/whole.txt
cat /tmp/res-failed.txt >> /tmp/whole.txt
total=`wc -l /tmp/whole.txt | awk '{sum += $1} END {print sum}'`
ok=`wc -l /tmp/res-ok.txt | awk '{sum += $1} END {print sum}'`
failed=`wc -l /tmp/res-failed.txt | awk '{sum += $1} END {print sum}'`
echo "UP: $ok"
echo "DOWN: $failed"
echo "TOTAL: $total"