Skip to content
Advertisement

Writing a script with ping information into an excel or .csv file

I would like to create a table of min/avg/max ping data using a script.

The input would be a .txt of host names and their locations, the output should be a table of those names and their ip address and max, avg and min ping of the command ping -c 10 example.com

It would be something similar to this:

awk -F"time=" 'NR==2 || NR==3 || NR==4 {gsub(/ms/,X,$2);print $2}'

Advertisement

Answer

Your specs aren’t very clear, but I think this might be what you want…

Assuming that the list of host names is called hostlist and the CSV file is pings.csv try this:

while read -r h; do ping -c 10 $h; done<hostlist |  awk 'BEGIN{printf "host,min,avg,maxn"} /PING/{host=$2};  $1=="rtt"{split($4,a,"/");printf "%s,%s,%s,%sn",host,a[1],a[2],a[3]}'  > pings.csv

Read the host list line by line: while read -r h; do ping -c 10 $h; done<hostlist and ping each host ….

Pipe the entire output to awk.

Have awk print a “header row”:

BEGIN{printf "host,min,avg,maxn"}

If a row of output matches PING save the hostname:

/PING/{host=$2};

From the last row (my ping starts that line with rtt) filter the times:

$1=="rtt"{split($4,a,"/");

Then print the whole data:

printf "%s,%s,%s,%sn",host,a[1],a[2],a[3]}'

and redirect it to the CSV

> pings.csv

Caution: this doesn’t have ANY error handling at all.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement