Shell Script help

noj88

New Member
Joined
Aug 28, 2018
Messages
2
Reaction score
0
Credits
0
HI,

I am new to shell scripting and need help to modify this in order to ping multiple hosts to decide if the route should be changed. This a sample of a single host that works but I need at least two host to fail before it completes.

#!/bin/sh
MissPings=0
while true;
do
ping -c 1 1.2.3.4
# If pings return success connectivity, the standard output ($?) will be set to zero
if [ $? -eq 0 ]
then
echo "Ping Success"
# If pings fail or timeout, increase the MissedPings variable by one
else
(( MissPings++ ))
echo $MissPings
fi
# If MissPings reaches five, edit the VPC main Route Table below subnets to forward traffic
# eni-123 (the spare vMX Elasitc Network Interface), and exit the script
if [ $MissPings -eq 5 ]
then
`aws ec2 replace-route --route-table-id xyz --destination-cidr-block 192.168.1.0/24 --network-interface-id ENI-123`
break
fi
sleep 1
done
 


I'll try to take a look at this later on this evening, or at some point over the weekend. I don't have time to look at it right now.

But I'd imagine you'd need to set up a list of IP's to ping - either hard-coded in the script in an array or something, or perhaps read from a file. Then you'd iterate through them, pinging each of them in turn and tracking which ones have failed and counting the failures of each machine, perhaps?

There is another possibility - you could modify your above script to take an IP as a parameter. Then you could write another script that calls the above script passing the IP of the machine to ping.

BTW - speaking of code - and for future reference - you should enclose your code in [code][/code] tags - then the indentation and formatting of your code will be kept.

You can either write the tags in the editor and then paste your code between the two tags, or you can use the "insert" button in Linux.org's post-editors toolbar. The "insert" button is the square with a '+' in it. Simply click the "insert" button and select "Code" and then paste your code in the dialog that pops up. Then click the "Insert" button on the code-dialog (not the one in the editor bar!) and your code will automatically be put inside code-tags.
 
I'll try to take a look at this later on this evening, or at some point over the weekend. I don't have time to look at it right now.

But I'd imagine you'd need to set up a list of IP's to ping - either hard-coded in the script in an array or something, or perhaps read from a file. Then you'd iterate through them, pinging each of them in turn and tracking which ones have failed and counting the failures of each machine, perhaps?

There is another possibility - you could modify your above script to take an IP as a parameter. Then you could write another script that calls the above script passing the IP of the machine to ping.

BTW - speaking of code - and for future reference - you should enclose your code in [code][/code] tags - then the indentation and formatting of your code will be kept.

You can either write the tags in the editor and then paste your code between the two tags, or you can use the "insert" button in Linux.org's post-editors toolbar. The "insert" button is the square with a '+' in it. Simply click the "insert" button and select "Code" and then paste your code in the dialog that pops up. Then click the "Insert" button on the code-dialog (not the one in the editor bar!) and your code will automatically be put inside code-tags.

Hi

What I did try was to have the first target fail, then move onto a second target and a third. Only at that point trigger the route change.

This maybe more reliable that a temporary glitch causing all three targets to fails. The sample counts the fails - even if they are not sequential so over a day could amount to a "fail".

What I did was but I'm not sure what to do with the second ping as a get an error.

#!/bin/sh
MissedPings=0
MissedPings1=0

while true;
do
ping -c 1 1.2.3


if [ $? -eq 0 ]
then
echo "Ping Successful"


else
(( MissedPings++ ))
echo $MissedPings
fi


if [ $MissedPings -eq 5 ]

then

ping -c 1 4.5.6.7

if [ $? -eq 0 ]

then

echo "Ping Successful"
fi
else
(( MissedPings1++ ))

echo $MissedPings1


if [ $MissedPings1 -eq 5 ]

then

echo "route changed"


break
fi
sleep 1
done
 

Staff online

Members online


Top