1

i am trying to monitor my hosts through ping. the hosts information are in a mysql table. i m using fping command the code is as followings

#/bin/sh

id=$(mysql -B --column-names=0 -uroot -pPassword -D monitor -e "SELECT ipv4 FROM nics WHERE icmp=1");

result=$(fping -c 10 $id |grep 'xmt/rcv/%loss');

#echo $result;

for line in $result;

do

echo $line

done

the output is

111.125.140.6 : xmt/rcv/%loss = 10/10/0%, min/avg/max = 234/234/235

123.135.140.7 : xmt/rcv/%loss = 10/0/100%

111.125.140.1 : xmt/rcv/%loss = 10/10/0%, min/avg/max = 230/231/231

111.125.130.2 : xmt/rcv/%loss = 10/10/0%, min/avg/max = 234/234/234

now i want to get IP, loss and average record from each line and input the data in a new table

thanks in advance

3
  • actually i am a bit new and tried to give detail . but some errors was showing and cant post the question. i m trying again . Commented Apr 17, 2019 at 18:57
  • From my understanding, if getting data from each of the lines is what you need, you can look at some tools like awk Commented Apr 17, 2019 at 19:10
  • If you are parsing lines like 111.125.130.2 : xmt/rcv/%loss = 10/10/0%, min/avg/max = 234/234/234 then try to make :|/ as your FS in awk . it may help Commented Apr 17, 2019 at 20:25

1 Answer 1

1

Using a while loop (instead of a for loop) with awk:

while read -r line
    do declare $(awk '{split($0,a,"[%/ ,]"); print \
    "ip="a[1],"send="a[8],"rec="a[9],"loss="a[10],"min="a[17],"avg="a[18],"max="a[19]}' <<< $line);
    ## variables created: [ip, send, rec, loss, min, avg, max]
    ## test with: echo $ip, $send, $rec, $loss, $min, $avg, $max
    ## now you can insert the variables into your new SQL table:
    echo "INSERT INTO newtable (IP,SEND,REC,LOSS,MIN,AVG,MAX) \
    VALUES ('$ip','$send','$rec','$loss','$min','$avg','$max');"
done< <(printf '%s\n' "$result") | mysql -uroot -pPassword foobar

This will to declare individual variables from your $line strings which you can insert into SQL.

Sign up to request clarification or add additional context in comments.

2 Comments

its showing error test.sh: line 22: syntax error near unexpected token <' test.sh: line 22: done< <(printf '%s\n' "$result") | mysql -uroot -pPassword monitor'
@RazibShahriarRubence: It should work, here’s an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.