I am basically a scripting noob, I have some programming logic, and I wouldn't post here if my 3 hours of searching actually found something.
So far this is what I have:
"
#! /bin/ksh
List=./pinglist1.txt
cat $List | while read ip
do
Pingable=""
ping $ip -n 2 | awk '/100%/ {print "no"}' |read Pingable
if [[ "$Pingable" != "no" ]]
then
print $ip "PINGS">>pingresults.txt
else
print $ip "DOESN'T PING">>pingresults.txt
fi
done
"
pinglist1.txt is just a plain file with IP addresses.
I just want the script to ping each IP address and report if it is pinging or not to a text file.
Please and thanks.
Could you provide the output of your script?
Also, the 'dotslash' isn't necessary in
List=./pinglist1.txt
as './' tells the shell to use the $PWD.
Is the pinglist1.txt file in the same directory as the script?
I went ahead and got rid of the ./
and yes, the .txt is in the same directory
These are the results:
usage: ping [-LRdfmnqrtv] [-T ttl] [-I addr] [-c count] [-i wait] [-l preload]
[-p pattern] [-s packetsize] host|[!]@hop1@hop2@...[@|:]dst
UX:sh (ping.sh): ERROR: [[: not found
UX:sh (ping.sh): ERROR: print: not found
usage: ping [-LRdfmnqrtv] [-T ttl] [-I addr] [-c count] [-i wait] [-l preload]
[-p pattern] [-s packetsize] host|[!]@hop1@hop2@...[@|:]dst
UX:sh (ping.sh): ERROR: [[: not found
UX:sh (ping.sh): ERROR: print: not found
usage: ping [-LRdfmnqrtv] [-T ttl] [-I addr] [-c count] [-i wait] [-l preload]
[-p pattern] [-s packetsize] host|[!]@hop1@hop2@...[@|:]dst
UX:sh (ping.sh): ERROR: [[: not found
UX:sh (ping.sh): ERROR: print: not found
usage: ping [-LRdfmnqrtv] [-T ttl] [-I addr] [-c count] [-i wait] [-l preload]
[-p pattern] [-s packetsize] host|[!]@hop1@hop2@...[@|:]dst
UX:sh (ping.sh): ERROR: [[: not found
UX:sh (ping.sh): ERROR: print: not found
Well, I have minimal experience with shell scripting, but from the output, I see that it's erroring that your `ping' syntax is incorrect. Analyzing the script, you have the IP address of the host before `-n 2', which I believe to be incorrect.
Either way, I cannot help with `awk' syntax.
Looks more like ping syntax
Are you trying to do 2 echoes to ip, if so
ping -c 2 $ip | awk '/100%/ {print "no"}' |read Pingable
Thanks, now I can tell it is running the ping, and I went ahead and modified my code to this now:
#! /bin/ksh
List=pinglist1.txt
cat $List | while read ip
do
Pingable=""
ping -c 2 $ip | awk '/100%/ {print "no"}' |read Pingable
if [ "$Pingable" != "no" ]
then
print $ip "PINGS">>pingresults.txt
else
print $ip "DOESN'T PING">>pingresults.txt
fi
done
AND the results:
UX:sh (ping1.sh): ERROR: print: not found
Do I need to have a print.pl or something uploaded to that directory?
try echo...
echo "$ip PINGS" >> pingresults.txt
else
echo "$ip DOESN'T PING" >> pingresults.txt
Appreciate the responses, so far I am trying to read up on what to do next, the problems I am facing are I need to know how to step to the next line in the text file, and the results I got so far are troubling. I have 2 ip addresses so far in the text file, when the IP address that pings is first in the list, it prints:
--IP address 1-- PINGS
but when I have an IP address that doesnt ping first in the list it prints:
--IP address 2-- PINGS
I also modified my code to delete the results file at the start of the script
EDIT: I have removed the real IP addresses and just have them named --IP address 1-- and --IP address 2--
Give this a shot...
#!/bin/ksh
List=pinglist1.txt
cat $List | while read ip
do
ping -c 2 $ip
rc=$?
if [[ "$rc" = "0" ]]
then
echo "$ip PINGS">>pingresults.txt
else
echo "$ip DOESN'T PING">>pingresults.txt
fi
done
Thanks.
Ya, unfortunately I'm having the same problem, both programs just ping the first in the file and then quits.
I need to know how to step to the next line -a loop basically-(I am reading up on it as well)
Can you try this one:
#! /bin/ksh
IPLIST=`cat ./pinglist1.txt`
for ip in $IPLIST
do
echo $ip
ping -c 2 $ip >>log.txt
if [[ $? -eq 0 ]]
then
print $ip "PINGS">>pingresults.txt
else
print $ip "DOESN'T PING">>pingresults.txt
fi
done
THANKS.
I just had to modify it a bit, changed [] to and print to echo and it worked.
pingresults.txt produced the following (xxx for security purposes):
xxx.xxx.193.16 DOESN'T PING
xxx.xxx.135.194 PINGS
Thanks!