LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-13-2005, 10:55 AM   #1
daleo
LQ Newbie
 
Registered: Nov 2005
Posts: 7

Rep: Reputation: 0
How to create a ping-tracking script..?


Hi!

Got a problem.
OS is Linux Mandrake 9.2 with special routing soft running on it. That
soft handles several channels: two for source hosts and others for subscriber-hosts. All hosts have ip-adresses, and the term "channel" is of logical nature. Those channels could be opened and closed from the command line with the appropriate commands (like " # chclose 123", where 123 - channel number). One of the source hosts is the main source host, the other source host - is reserved source host. Both source hosts are online (their ip-addresses could be pinged) but only the channel for the main source host is up (opened).
The idea is to write a script that will at some periods of time (every 5 sec. for example) ping the main source host and when it will go down (destination host unreachable) the script will execute a command for closing its channel and another command for opening channel for reserved source host . The script must continue pinging the main source host and , when it will be up again, the script should execute a command for closing channel of the reserved source host and a command for opening channel for main source host.

Thank you!
 
Old 11-14-2005, 04:17 AM   #2
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
You may need to tweek this, especially the ping statements. I was using cygwin and the ping has different options than I'm used to.

#!/bin/bash
MAIN=123.123.123.123
RESERVED=123.123.123.124

while true
do

until [ "`ping $MAIN 56 1 |grep received|sed 's/^.*ived, //'`" = "100.0% packet loss" ]; do
sleep 5
done
chclose 123
chopen 124

until [ "`ping $MAIN 56 1 |grep received|sed 's/^.*ived, //'`" = "0.0% packet loss" ]; do
sleep 5
done
chclose 124
chopen 123
done


<edit>
The idea with the ping is to just send out one packet. With some versions of ping you can do that with an option like:
ping -c1 IP-Address
But with cygwin's ping you have to tell it the packet size then the number of packets. That is why it looks strange:
ping IP-Address 56 1

Last edited by /bin/bash; 11-14-2005 at 04:35 AM.
 
Old 11-15-2005, 07:48 AM   #3
daleo
LQ Newbie
 
Registered: Nov 2005
Posts: 7

Original Poster
Rep: Reputation: 0
For testing purpuses I did so:

#!/bin/bash
MAIN=10.2.1.15
ME=10.2.1.18

while true
do

until [ "`ping -c1 $MAIN |grep received|sed 's/^.*ived, //'`" = "100.0% packet loss" ]; do
sleep 5
done
ping -c3 $ME

until [ "`ping -c1 $MAIN |grep received|sed 's/^.*ived, //'`" = "0.0% packet loss" ]; do
sleep 5
done
ping -c6 $ME
done

This script I run on server (10.2.1.30)
The idea is that when $MAIN goes down, $ME will get 3 icmp packets, and when it'll
get up $ME will get 6 icmp packets.
I looked through packets with analyzer run on $ME in promisc mode, and only icmp
requests from server (10.2.1.30) to $MAIN are seen...
What it can be ?
 
Old 11-15-2005, 10:26 PM   #4
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
I think the problem is because I used cygwin to write the first script. The ping command is completely different and has a totally different output than regular Linux ping. Sometimes I get stuck on a windows computer and cygwin is the only option I have.

Try your ping command in an xterm and check the output when the server is up and when the server is down. I rewrote your test script using regular Linux ping so hopefully this will work better.

#!/bin/bash
MAIN=10.2.1.15
ME=10.2.1.18

while true
do

until [ "`ping -c1 $MAIN |grep received|cut -d' ' -f8`" = "100%" ]; do
sleep 5
done
ping -c3 $ME

until [ "`ping -c1 $MAIN |grep received|cut -d' ' -f6`" = "0%" ]; do
sleep 5
done
ping -c6 $ME
done


The cygwin ping had a decimal place in the percentage where a regular linux ping doesn't have one. Also the loss percentage is in a different place if the ping is good or bad but in cygwin it is always in the same place.
 
Old 11-16-2005, 12:13 AM   #5
daleo
LQ Newbie
 
Registered: Nov 2005
Posts: 7

Original Poster
Rep: Reputation: 0
Thank you!!

Due to the fact, that script is run on freeBSD, i had to change "cut -d' ' -f8" to
"cut -d ' ' -f7" 'cause the ping output on BSD is bit different from that in Linux.

Great. It's really seems simple now, though i had some ideas of writing it in C,
using ping code and symbol sockets..
Thank you, again!
 
Old 11-17-2005, 03:03 AM   #6
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
HTH
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Keep Alive Ping Script Apollo77 Linux - Networking 8 05-19-2006 12:29 PM
Linux ping script MPowers Linux - Networking 6 07-02-2005 04:18 PM
ping script richiehawtin Programming 7 08-28-2004 02:39 AM
How to create many by a script kelper Linux - General 11 09-24-2003 03:09 PM
How to create a user who can ONLY do *telnet* and *ping*? yuzuohong Linux - General 5 08-24-2002 07:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration