ping script

Discussion in 'Programming/Scripts' started by mcrosby, Jun 1, 2006.

  1. mcrosby

    mcrosby New Member

    hello, I am looking to make a script that will ping a remote ip address. Upon completion of the ping I want the program to either ping again if the # of packets transmitted is equal to the number of packets received or exit if the two values are unequal and information was lost. I am not sure if I should go about this program with a shell script or a C script or any other alternative. In either case would someone be able to point me in the correct direction?
     
  2. falko

    falko Super Moderator Howtoforge Staff

    If you use a shell script, you might have to parse the ping output with awk and/or sed.

    But it might be easier to use a PHP script for it. Have a look at the functions exec(), system(), shell_exec, and passthru().
     
  3. mcrosby

    mcrosby New Member

    I am very new to scripting so to put it bluntly I have no idea what you are talking about lol..I am familiar with PHP but i do not know how to code it. Are those functions I should take a look at in PHP or shell script
     
  4. sjau

    sjau Local Meanie Moderator

    Last edited: Jun 1, 2006
  5. mcrosby

    mcrosby New Member

    ok so i wanted to use bash scripting and i received a bit of help thus far.

    but i am looking to get the program to display the results in the terminal window. I would like it to display the normal ping information the size as well as the interval. I would also like it so that when the user runs the program they would do "nameofprogram -c3 -i0.05 -s800 ... so they take those commands from the command line and then the output is the size, the interval, and the packet size and then it gives the message if they are equal or not can anyone help?
     
  6. falko

    falko Super Moderator Howtoforge Staff

    Have a look here: http://www.tldp.org/LDP/abs/html/internalvariables.html#ARGLIST
     
  7. mcrosby

    mcrosby New Member

    hmm that page would help more if there were discriptions of what they were doing
     
  8. falko

    falko Super Moderator Howtoforge Staff

    You can refer to arguments you pass to your script with $1, $2, ... $0 is the script itself.
     
  9. mcrosby

    mcrosby New Member

    thanks Falco that did in fact prove to help
     
  10. mcrosby

    mcrosby New Member

    ok so now for example my output is:

    And then once the ping either times out or the user presses cntrl C you get

    My question is how do I reference that number of packets transmitted and received so that I can calculate if they are equal or not. I know i would put those values into variables but what do I set each variable equal too?
     
  11. falko

    falko Super Moderator Howtoforge Staff

    I think you must parse this somehow:

    Code:
    5 packets transmitted, 5 received, 0% packet loss, time 12009ms
    Maybe there's a regular expression for it, but unfortunately I'm not a regular expresions expert... :(
     
  12. msfz751

    msfz751 New Member

    My ping function to detect remote host is alive

    I got the idea with the posts above and came up with this function:

    #!/bin/bash
    # 401_ping_function.sh
    # 07.JAN.07 Original version modified to use it as a function

    host=zeus

    #=========================================================================
    function is_host_alive() # Returns success or failure as boolean
    #=========================================================================
    # Called as: is_host_alive my_host_name
    # Arg_1 = remote_host_to_test
    {
    count=3
    loopit=5
    qhost=$1 # saving contents in $1 before is used
    echo "Pinging $qhost ; Use ctl c to end the loop"
    i=0
    while [ $i -lt $loopit ]
    do
    info=$(ping -qc$count $qhost |grep packets| cut -d" " -f1,4)
    set -- $info
    echo -e "$i: $1 $2 \t"
    if [ $1 != $2 ]; then
    echo "transmitted and received packets do not match"
    # exit
    exitcode=1
    else
    echo "transmitted and received packets do match"
    exitcode=0
    fi
    i=$(expr $i + 1)
    done
    return $exitcode
    }


    ##################
    # main starts here
    ##################

    if is_host_alive $host
    then
    echo "host alive"
    else
    echo "host "
    fi
     
  13. falko

    falko Super Moderator Howtoforge Staff

    Instead of specifiying the hostname in the script (
    Code:
    host=zeus
    ) You could use
    Code:
    host=$1
    You would then call the script like this:
    Code:
    /path/to/401_ping_function.sh server1.example.com
     

Share This Page