LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script (https://www.linuxquestions.org/questions/linux-newbie-8/script-4175484297/)

amartlk 11-12-2013 12:42 AM

script
 
Hi

i have centos 5.3 installed on server i want to write a script which will check dns ips i.e 214.253.255.231 and 214.253.255.236 is pinging continously if both ips are not pinging then i want to run another .sh file can you pls assist me in same

linuxlover.chaitanya 11-12-2013 12:43 AM

What do you want to achieve from this? Whats the purpose?

NM04 11-12-2013 01:05 AM

amartlk,
what exactly do you want to accomplish ?
1) do you want both ip's to be pinged by another one ?
2) OR both ip's should ping each other ?

I would like to help provided you give more details !!

regards,
nm

amartlk 11-12-2013 01:12 AM

Hi

these are the 2 dns ips address of isp i want only ping this 2 ips should ping from my server if both are not pinged the i run one sh file which we restart adsl and my net is working now i do it manually

chrism01 11-12-2013 01:39 AM

Please show us what code you've done so far

Firerat 11-12-2013 01:53 AM

Code:

ping -c1 <ip> || FixTheInternet.sh
expanded
Code:

ping -c1 <ip> || dns1=1 && dns1=0
ping -c1 <ip2>|| dns2=1 && dns2=0
(( "$dns1" == "0" && "$dns2" == "0" )) || FixTheInterWebs.sh


amartlk 11-12-2013 02:56 AM

Thanks for reply
it is ok as below where below are 2 ip of dns
ping -c1 <214.253.255.231> || dns1=1 && dns1=0
ping -c1 <214.253.255.236>|| dns2=1 && dns2=0
(( "$dns1" == "0" && "$dns2" == "0" )) || FixTheInterWebs.sh

Firerat 11-12-2013 03:12 AM

no, remove < and >

<ip> and <ip2> were intended as 'markers'

Code:

ping -c1 214.253.255.231 || dns1=1 && dns1=0
ping -c1 214.253.255.236 || dns2=1 && dns2=0
(( "$dns1" == "0" && "$dns2" == "0" )) || FixTheInterWebs.sh

you will still need to do some work for that to do what you want

while I'm at it,
a re-write
Code:

for ip in 214.253.255.231 214.253.255.236;do
    ping -c1 $ip || ( FixTheInterWebs.sh; break )
done

does the same, but less 'messing about'

amartlk 11-12-2013 03:35 AM

#!/bin/bash

for ip in 214.253.255.231 214.253.255.236;
do
ping -c1 $ip || ( FixTheInterWebs.sh; break )
done


is this correct now

druuna 11-12-2013 04:09 AM

@amartlk: Have you tried running Firerat's examples?

The only way to learn anything is to try to understand what's going on and play around with the given examples.

If there's something specific about the code you don't understand we can/will explain it to you.

amartlk 11-12-2013 04:14 AM

Hi

ya i want to understand the flow of code thats why i ask you

druuna 11-12-2013 04:30 AM

Quote:

Originally Posted by amartlk (Post 5063046)
ya i want to understand the flow of code thats why i ask you

Which example and what part are you unclear about?

Firerat 11-12-2013 04:33 AM

Quote:

Originally Posted by amartlk (Post 5063024)
#!/bin/bash

for ip in 214.253.255.231 214.253.255.236;
do
ping -c1 $ip || ( FixTheInterWebs.sh; break )
done


is this correct now

well, the script is ok,, but my logic is broken

the examples I have given are not difficult

lets ignore the second one for now, and look at what the first is doing

Code:

ping -c1 214.253.255.231 || dns1=1 && dns1=0
# if ping ( with count of 1 , so only one ping ) 214.253.255.231 returns a none-zero  exit (error ) set variable  dns1 to 1
# if exit is 0 ( no error ) set variable  dns1 to 0
# repeat with second ip , var is dns2
(( "$dns1" == "0" && "$dns2" == "0" )) || FixTheInterWebs.sh
# if var dns1 AND dns2 are both 0 return 'true'
# if either dns1 or dns2 are not zero return 'false'
# || ( action if false )

alternative
Code:

ping -c1 214.253.255.231;dns1=$?
ping -c1 214.253.255.236;dns2=$?
if (( "$dns1" != "0" && "$dns2" != "0" )); then
    FixTheInterWebs.sh
fi


there is not much else I can type about this
there are a number of ways to do it



anyway, back to my broken logic
Code:

for ip in 214.253.255.231 214.253.255.236;
do
    ping -c1 $ip || ( FixTheInterWebs.sh; break )
done

this is broken as it will run FixTheInterWebs.sh if 214.253.255.231 is 'down' even if 214.253.255.236 'up'
you want to Fix the Interwebs if both dns servers are 'down'


All times are GMT -5. The time now is 12:36 AM.