Link to home
Start Free TrialLog in
Avatar of jackdaniel_china
jackdaniel_china

asked on

awk script using function

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

Open in new window

Avatar of amit_g
amit_g
Flag of United States of America image

Change

awk ' X=$IP

to

awk ' X="'$IP'"
Avatar of jackdaniel_china
jackdaniel_china

ASKER

thanks Amit
I changed it and it seems that it still did not recognize the IP as a parameter...below is the result

192.168.196.100
Usage: ping [-LRUbdfnqrvVaA] [-c count] [-i interval] [-w deadline]
            [-p pattern] [-s packetsize] [-t ttl] [-I interface or address]
            [-M mtu discovery hint] [-S sndbuf]
            [ -T timestamp option ] [ -Q tos ] [hop1 ...] destination
ITS UP
192.168.196.101
192.168.196.102

my source file has just 3 ips for testing...
192.168.196.100
192.168.196.101
192.168.196.102



thanks a lot!
Why are you doing it this way? Could you not use something like
#!/bin/sh
echo reading file
filename="/usr/src/wakeonlan-0.41/pingafterboot.out"
 
awk '
                        function pinger(x)
                        {
                                print "Inside function " x;
                        }
                {print $1; print pinger($1);}
' < $filename

Open in new window

because I need to ping each one of the ips and check the ones that did not wakeup by the wakeuponline script.
the purpose it's to check the computers that did not wakeup and check why if all have the bios and network card configuration set.
the above example just print the lines but do not test if they are not pinging.
thanks dude
That was only to demonstrate. you can have anything in the pinger function and do anything you want. I added a print just to show that it can be done this way.
Thank you again.
I will try to do in this way and get back soon.
I am not very familiar using awk with functions inside...
cheers
ASKER CERTIFIED SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I wouldn't bother with awk at all.  That just complicates things.

Here's a nice simple way of doing it.
#!/bin/sh
echo reading file
filename=/usr/src/wakeonlan-0.41/pingafterboot.out
 
while read ip
do
  echo "Pinging $ip"
 
  if ping -c -c3 $ip | grep -q '100% packet loss'
  then
     echo "$ip did not wakeup by script"
  else
     echo "$ip is UP"
  fi
done <$filename

Open in new window

Great Dude, thanks a lot!!!
Thanks Tintin for your suggestion as well,
it also can be used!!!
cheers