0

Good morning, I would like to create a script that allows me to ping and traceroute from a list of ip, I have already started to make this little script. the problems I encounter are : that the script pings and traceroutes only the last ip of the file and I can't export the ping results to another file

#!/bin/bash
# in order:
# - saintpierre
# - ping and traceroute saintpierre
FILENAME="./ip/sainpierre/ipsaintpierre.txt"
sudo mkdir ./result/saintpierre/
outputfile="./result/saintpierre/$(date "+%F-%Hh%Mm%S")-$pingtestlogsaintpierre.txt"
for DC in $FILENAME
do
    printf "$DC: \t$(ping -i .2 -c 10 -q $DC | awk -F/ '/^round|^rtt/{print $5}') ms\n" | expand -t 20
done >>$outputfile
echo -e "\n"
traceroute $FILENAME >>$outputfile

thanks

  • Why not just `ping >resultfile.txt` – Nic3500 Apr 14 '22 at 16:34
  • Note that substituting data into a printf format string is bad practice. Use `printf '%s: \t%s ms\n' "$DC" "$(ping ...)"` so that your `$DC` and your ping output are used to replace the `%s`s without being treated as part of the format string themselves. – Charles Duffy Apr 14 '22 at 16:36

1 Answers1

0

Assuming you need to create the path /result/saintpierre/, you need to use -p parameter:

mkdir -p /result/saintpierre/

-p will create all folders in path

Since you need to read a file to get all IPs on loop, you have to change to:

for DC in $(cat $FILENAME)
Juranir Santos
  • 370
  • 2
  • 6
  • 1
    `for DC in $(cat $FILENAME)` is buggy. See [DontReadLinesWithFor](https://mywiki.wooledge.org/DontReadLinesWithFor). An easy, more reliable practice if you're only targeting bash 4.0 or newer is `readarray -t dcs <"$FILENAME"`, after which one can do `for dc in "${dcs[@]}"; do ...` – Charles Duffy Apr 14 '22 at 16:36