我正在使用下面的脚本ping多个Linux主机,如果它通过硬编码将hostfile
个主机放入脚本本身,那么效果会非常好,但我希望这是基于用户输入的.
虽然我使用read
该文件作为用户的输入,但这越来越失败,请建议我这里做错了什么.
#!/bin/bash
read -rsp $'Please Enter your hostFile name: ' target_File
echo $target_File
printf "$(date) Starting the Ping Check...."
function pingHost () {
target_host=${1}
ping -c2 ${target_host} >/dev/null 2>&1 &&
printf "%-20s %-10s\n" "host ${target_host}" "ping SUCCESS" ||
printf "%-20s %-10s\n" "host ${target_host}" "ping FAILED"
}
#
# Variable "data" seeks the filename containing host list.
data="$(< target_File)"
for line in $data
do
pingHost ${line} &
done
wait
printf "Completed @=> $(date)"
printf "\n"
While giving the 100 inside the script then it works as follows ..
data="$(< hostfile)" <-- this hard-coded file containing hosts
$ ./ping_parallel.bash
Sun May 22 10:55:24 IST 2022 Starting the Ping Check....
host savfav0194 ping SUCCESS
host savfav0268 ping SUCCESS
host savfav0263 ping SUCCESS
host savfav0196 ping SUCCESS
host savfav0260 ping SUCCESS
host savfav0259 ping SUCCESS
host savfav2088 ping SUCCESS
host savfav2135 ping SUCCESS
host savfav2136 ping SUCCESS
host savfav3088 ping SUCCESS
host savfav0257 ping SUCCESS
host savfav0262 ping SUCCESS
host savfav0261 ping SUCCESS
host savfav0270 ping SUCCESS
host savfav0255 ping SUCCESS
host savfav0265 ping SUCCESS
host savfav0266 ping FAILED
Completed @=> Sun May 22 10:55:26 IST 2022
Failing while trying user input :
Please Enter your hostFile name: target_File
Sun May 22 10:58:54 IST 2022 Starting the Ping Check...../ping_parallel.bash: line 22: target_File: No such file or directory
Completed @=> Sun May 22 10:58:54 IST 2022