troubleshooting Question

awk script using function

Avatar of jackdaniel_china
jackdaniel_china asked on
LinuxShell Scripting
10 Comments1 Solution374 ViewsLast Modified:
Hi There
I am trying to create a simple script that reads a file that contains a list with ips.
the script reads the file, and try to ping ip by ip (line by line) and check if it's pinging or not.
I am using awk with an function to do it, but the IP variable is not being recognized inside the function.
not sure how to declare the varialbe with the $line content to be seing inside the function and ping ip by it.
I will use it to see the computers that did not wakeup automatically by another script that is running fine, but few computers are not working properly so I can easy identify them.
thank you
#!/bin/sh
echo reading file
filename="/usr/src/wakeonlan-0.41/pingafterboot.out"
 
while read -r line;
do
 
IP=$line
echo $IP
awk ' X=$IP
        function pinger(x) {
                command = "/bin/ping -n -c 3 " x
                while (( command | getline res )> 0 ) {
                        if ( res ~ /0 received|100% packet loss/ ) {
                                close(command)
                                return 100
                        }
                }
        close(command)
        return 0
        }
 
        BEGIN {
        if ( pinger(X) == 100 ) {
                print X "did not wakeup by script"
        } else { print X "ITS UP" }
 
}'
 
 
done < $filename
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 10 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros