Results 1 to 10 of 10

Thread: can you write a ping sweeper in shell script

  1. #1

    can you write a ping sweeper in shell script

    As an excercise i'd like to write ping sweeper in bash script but there are 2 things i'd like to know. (I know that for you experts it's gonna be easy but don't laugh)

    1. How do I make ping command ping the host only once?
    2. How does the program determine if the host is alive or not?

  2. #2
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    2) Ping works by sending an ICMP echo request (type 8 code 0). If the machine is up and something along the way isn't stopping it, the machine will send an ICMP echo reply (type 0 code 0).

    1) While I suppose it would be possible to do it in bash, sh, etc, what about something a little more structured like perl?

    I will be looking into this to see if I can get one to work real quick and get an answer back to the rest of it.

    /nebulus

    EDIT: Btw, I know you said it is an exercise, but I don't know of anything that can beat nmap's ping sweep, simply because fyodor doesn't wait for a response before pinging the next host...which I don't think will be possible in sh...which would have to use the stock ping program which will wait...
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  3. #3
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401

    Re: can you write a ping sweeper in shell script

    Originally posted here by cybernetic
    1. How do I make ping command ping the host only once?
    man ping


    2. How does the program determine if the host is alive or not?
    man ping
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  4. #4
    I do know how the ping itself determ. if the host is up or not but what I need to know is how does the script determine if the ping got a reply

    ei: if (insert what here???) echo "host alive"

  5. #5
    Senior Member
    Join Date
    Jul 2001
    Posts
    461
    ping -c 1 ip.to.ping.here

    or, if you wanted to do it in windows sometime

    ping -n 1 ip.to.ping.here

    As for the script figuring out which hosts are alive, you will need to pipe the output of ping to grep or sed.

  6. #6
    AntiOnline Senior Member souleman's Avatar
    Join Date
    Oct 2001
    Location
    Flint, MI
    Posts
    2,883
    ping -c 1 <address> (- c is count)
    if you get a reply, the host is up, else either the host is down, or it is not responding to icmp packets.
    \"Ignorance is bliss....
    but only for your enemy\"
    -- souleman

  7. #7
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    I recommend against using the stock ping, look into hping2 ... nice tool. I am not sure about the linux versions, but the ping is fairly limited in solaris (which is what I use), so that is the reason I suggest Hping2...

    Here is what my algorithm would be if I was going to write it:

    1) Ask for network range to scan, make sure it is able to generate IP's correctly.
    2) Once I know IP's generated correctly, have it execute hping2 -c 1 -1 <IP> >> outfile
    3) Read the outfile, then use pattern matching to grab a success or fail
    4) Print result as found

    It is probably possible to eliminate need of using a temp file, but that would be a little more
    advanced, and I usually go for simple functionality on the first try at something...

    /nebulus
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  8. #8
    ok i see ... you know the temp file is kind of a workaround....

    i'd really like to somehow resove the ping reply on-the-fly... what about the 'grep' idea... i'm awfull w/ grep. ... how do you pipe it .... is it like this "ping 'blablablab' | grep blablabbal"

  9. #9
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    You might be able to do something like:

    a=`ping -c 1 <host> <timeout> | grep 'is alive'`
    if [ "$a" -ne "" ]; then
    echo "$a"
    else
    echo "<host> is dead."
    fi

    /nebulus
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  10. #10
    you know ... it think i got it i'll do it when i get home and post it here... just for S & G ... thnax guys...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •