Home > other >  Ping results are not saved in a file
Ping results are not saved in a file

Time:02-02

I am running a simple shell script which initiates the ping and save the result to a txt file.

ips=$(cat host.txt)

for ip in $ips
do
ping -c 2 $ip > pingtest.txt  
done

However for some reason, there is no output in the text file,I am sure the ip is pinging(I have confirmed with TCP DUMP).

Can someone please help me?

hostfile output is:

10.0.0.10

10.0.0.11

172.28.209.43

172.16.84.131

CodePudding user response:

Don't overwrite the file on every iteration. Do something like this.

ips=$(cat host.txt)

for ip in $ips
do
ping -c 2 $ip 
done > pingtest.txt

Also as a suggestion, use consistent indentation and avoid word splitting when filename expansion a.k.a globbing is enabled. For example you can use a while read loop instead of a for.

while IFS="" read -r ip
do
    ping -c 2 "$ip"
done <host.txt > pingtest.txt
  • Related