ping:./ipsweep.sh.105:名称或服务未知

ping:./ipsweep.sh.105:名称或服务未知

#代码:

#!/bin/bash
for ip in `seq 100 110`; do
    ping -c 1 $0.$ip | grep "64 bytes" | cut -d " " -f 4 | tr -d ":" &
done

#命令:

./ipsweep.sh 192.168.0

#结果:

ping: ./ipsweep.sh.105: Name or service not known

答案1

$0是脚本的路径名。$1是第一个参数。

您生成的命令是

ping -c 1 ./pingsweep.sh.100

答案2

这是一个简单的脚本,可以完成您想要的操作。您可以修改它以满足您的需求

#!/bin/bash
        
ping_hosts () 
{ 
    network="192.168.0"
    start=$(date +"%d-%m-%Y-%T")
    local s=$1
    local e=$2
    printf '%s\n\n' "Pinging $network"
    for ((i=s; i<=e; i++))
    do
        if ping -s 64 -c 1 -W 1 ${network}.${i} >/dev/null; then
            ((count1 += 1))
            printf '%s\n' "${network}.${i} is UP"
        else
            ((count2 += 1))
            printf '%s\n' "${network}.${i} is DOWN"
        fi
    done
    end=$(date +"%d-%m-%Y-%T")
    printf '%s\n' "Start:$start"
    printf '%s\n\n' "End**:$end"
    printf '%s UP and %s DOWN\n' "$count1" "$count2"
}
if (($# < 2)); then
    printf '%s\n' "Use start and end range i.e:  .${0##*.} 100 110"
    exit 1
else
    ping_hosts $1 $2
fi

您可以通过 ie 使脚本可执行。

chmod u+x ping_hosts

如果你不带参数运行它,它会产生:

$> ./ping_host 
Use start and end range i.e:  ./ping_host 100 110

如果您将其运行为:

$> ./ping_host 100 110
Pinging 192.168.0

192.168.0.100 is UP
192.168.0.101 is DOWN
192.168.0.102 is UP
192.168.0.103 is DOWN
192.168.0.104 is UP
192.168.0.105 is DOWN
192.168.0.106 is DOWN
192.168.0.107 is DOWN
192.168.0.108 is DOWN
192.168.0.109 is DOWN
192.168.0.110 is DOWN
Start:24-08-2021-22:55:59
End**:24-08-2021-22:56:07

3 UP and 8 DOWN

相关内容