IP Ping Sweep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting IP Ping Sweep
# 1  
Old 05-21-2014
IP Ping Sweep

I put this together to search for unused IP addresses on a router. It's run as a Bash Command on OSX 10.9. Basically the user inputs the router address' IP range to sweep. 'jot' spits out a list of IP addresses based on that input and each one of those gets pinged. The output looks like this (tab delimitated):

xx.xxx.xx.xx Used name.domain.com
xx.xxx.xx.xx Used No Domain
xx.xxx.xx.xx Not_used


I was hoping to get help on 2 topics:

1. How can I manage error handling in the user input fields?
The first call to the user must only contain the first three quadrants of the router address: xx.xxx.xx(x). If I can just have it verify the input is numbers only with no more/less than 3 periods, that is sufficient
The last two calls are the low/high range inputs for the forth quadrant of the IP address which should be no more than 3 numbers (only numbers).

if entered incorrectly I would really like it to loop around and re-ask the user for that same input.

2. To exit, I press 'ctrl + c' but it requires I press it like 20 times before it breaks the actual process. I'm guessing I'm just breaking that instance of the while loop and it's moving onto the next line. Is there a more elegant way to do this without closing the window?

Any insight is appreciated. If there are any parts that can be streamlined below, let me know.

Code:
echo "\nEnter first three quadrants of router address (must contain three periods) \nExample- xx.xxx.xx. :"
read PT1ADD
echo 'Enter beginning range of the fourth quadrant :'
read PT2ADD
echo 'Enter the end range of the forth quadrant :'
read PT3ADD
echo '\n--------------------------------------'
echo 'Running scan on '$PT1ADD$PT2ADD-$PT3ADD
echo '--------------------------------------\n'
jot -w  $PT1ADD $PT3ADD $PT2ADD $PT3ADD | uniq |
while read SRC
do ping -c1 -W1 $SRC &>/dev/null
STATUS=$(echo $?)
    if [ $STATUS == 0 ]; then
        echo $SRC '\tUsed' | tr '\n' '\t'
        host $SRC | awk '/pointer/ {print $5}' | grep -m1 '.com' || echo 'No Domain'

## ^ I know grep is redundant here but if awk failed to find "pointer" it did not trigger the || echo 'No Domain' command. 
Grep was my workaround ##

    elif [ $STATUS == 2 ]; then
    echo $SRC '\tNOT_Used'
    else echo '\tPing Error Occured'
    fi
done
echo '\n--------------------------------------'
echo 'Scan Complete'
echo '--------------------------------------\n'


Last edited by sudo; 05-21-2014 at 06:30 PM.. Reason: typo
# 2  
Old 05-22-2014
How about this for your quadrants prompt:

Code:
function get_three_quad
{
   REPLY=""
   while [ -z "$REPLY" ]
   do
      printf "$1"
      read
      if [ "${REPLY//[0-9]/}" != ".." ]
      then
         echo "Illegal input"
         REPLY=""
      fi
    done
}

get_three_quad "
Enter first three quadrants of router address (must contain three periods)
Example- xx.xxx.xx. : "
echo "You entered: $REPLY"

---------- Post updated at 02:13 PM ---------- Previous update was at 02:09 PM ----------

Or if your happy to use grep to validate, and your grep supports Extended REs (-E option).
You could replace the if with the following for full validation:

Code:
if ! echo "$REPLY" | grep -qE "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){3}$"

For a single value 1-255 you have a number of choices but I like:

Code:
[ $REPLY -gt 0 -a $REPLY -lt 256 ] 2>/dev/null && return
echo "Illegal input"
REPLY=""


Last edited by Chubler_XL; 05-22-2014 at 01:21 AM..
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 05-22-2014
Quote:
Originally Posted by Chubler_XL
How about this for your quadrants prompt:

Code:
function get_three_quad
{
   REPLY=""
   while [ -z "$REPLY" ]
   do
      printf "$1"
      read
      if [ "${REPLY//[0-9]/}" != ".." ]
      then
         echo "Illegal input"
         REPLY=""
      fi
    done
}

get_three_quad "
Enter first three quadrants of router address (must contain three periods)
Example- xx.xxx.xx. : "
echo "You entered: $REPLY"

---------- Post updated at 02:13 PM ---------- Previous update was at 02:09 PM ----------

Or if your happy to use grep to validate, and your grep supports Extended REs (-E option).
You could replace the if with the following for full validation:

Code:
if ! echo "$REPLY" | grep -qE "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){3}$"

For a single value 1-255 you have a number of choices but I like:

Code:
[ $REPLY -gt 0 -a $REPLY -lt 256 ] 2>/dev/null && return
echo "Illegal input"
REPLY=""

That function is great. I see the value defined by the user can can called by using $REPLY later on in the script. Is there a way I can create another variable?

The reason I ask, I want to build that function for all 3 user input fields- so $REPLY does't work for my variable. I need specific names such as PT1ADD, PT2ADD, and PT3ADD.
# 4  
Old 05-22-2014
Well you could always assign your variable from reply after calling the function.

Or here is a change that adds a 2nd parameter for the variable to store the result.

Code:
function get_three_quad
{
   REPLY=""
   while [ -z "$REPLY" ]
   do
      printf "$1"
      read     
      if ! echo "$REPLY" | grep -qE "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){3}$"
      then
         echo "Illegal input"
         REPLY=""
      fi
    done
    [ $# -gt 1 ] && printf -v "$2" "%s" "$REPLY"
}

get_three_quad "
Enter first three quadrants of router address (must contain three periods)
Example- xx.xxx.xx. : " PT1ADD
echo "You entered: $PT1ADD"

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 05-22-2014
Quote:
Originally Posted by Chubler_XL
Well you could always assign your variable from reply after calling the function.

Or here is a change that adds a 2nd parameter for the variable to store the result.

Code:
function get_three_quad
{
   REPLY=""
   while [ -z "$REPLY" ]
   do
      printf "$1"
      read     
      if ! echo "$REPLY" | grep -qE "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){3}$"
      then
         echo "Illegal input"
         REPLY=""
      fi
    done
    [ $# -gt 1 ] && printf -v "$2" "%s" "$REPLY"
}

get_three_quad "
Enter first three quadrants of router address (must contain three periods)
Example- xx.xxx.xx. : " PT1ADD
echo "You entered: $PT1ADD"

ah! That was the missing piece to the puzzle. Thanks for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Ping test sends mail when ping fails

help with bash script! im am working on this script to make sure my server will stay online, so i made this script.. HOSTS="192.168.138.155" COUNT=4 pingtest(){ for myhost in "$@" do ping -c "$COUNT" "$myhost" &&return 1 done return 0 } if pingtest $HOSTS #100% failed... (4 Replies)
Discussion started by: mort3924
4 Replies

2. OS X (Apple)

A Bash Audio Sweep Generator...

This is a small program as a tester for a basic sweep generator for bandwidth testing of AudioScope.sh. This DEMO is only capable of 4KHz down to about 85Hz and back due to the low bit rate, but it is proof of concept for a much wider variant using a much higher bit rate. The file generated... (4 Replies)
Discussion started by: wisecracker
4 Replies

3. Shell Programming and Scripting

How to get reason for ping failure using perls Net::Ping->new("icmp");?

Hi I am using perl to ping a list of nodes - with script below : $p = Net::Ping->new("icmp"); if ($p->ping($host,1)){ print "$host is alive.\n"; } else { print "$host is unreacheable.\n"; } $p->close();... (4 Replies)
Discussion started by: tavanagh
4 Replies

4. UNIX for Dummies Questions & Answers

Where is PING ?

SHAME, that's all that I'm felling right now, however I'm not finding PING or either tracert in my box (Solaris 10). 1) Yes my PATH does have /usr/bin /usr/sbin # echo $PATH /opt/csw:/usr/bin:/sbin:/usr/sbin:/usr/ccs/bin:/usr/sfw/bin:/opt/csw/bin:/opt/csw/gcc2/bin:/opt/csw/bin 2) If I... (1 Reply)
Discussion started by: pxb368@motorola
1 Replies

5. IP Networking

PING

I am unable to ping my remote server.My server is unable to ping the same. both are able to ping the gateway. both the ip's are on same network.i use a proxy tunnel on my remote server.Help if any clues. (6 Replies)
Discussion started by: oslbhavana
6 Replies

6. Shell Programming and Scripting

Animation Ping on Solaris Like Cisco Ping

Hi, I develop simple animation ping script on Solaris Platform. It is like Cisco ping. Examples and source code are below. bash-3.00$ gokcell 152.155.180.8 30 Sending 30 Ping Packets to 152.155.180.8 !!!!!!!!!!!!!.!!!!!!!!!!!!!!!. % 93.33 success... % 6.66 packet loss...... (1 Reply)
Discussion started by: gokcell
1 Replies

7. IP Networking

Can't Ping

I have an old MP-Ras Unix system. I have setup netowkring but am unable to ping any local network pcs or the default gateway. If i use the arp -a command I receive the correct mac address for all connected pcs but I cannot ping anything except the local address. Any help would be appreciative. ... (7 Replies)
Discussion started by: Rutgerncas
7 Replies

8. Linux

Not able to ping

Hi All, Need your help one more time. I am trying to ping a linux machine which is not responding to ping. However traceroute can reach the machine and I can log in to it by ssh. I have checked /proc/sys/net/ipv4/icmp_echo_ignore_all it is already set as "0". It is not happening in the... (1 Reply)
Discussion started by: ailnilanjan
1 Replies

9. UNIX for Dummies Questions & Answers

Ping

Hi , I have one system installed on Linux Red Hat 3.0. I have ip of system 3.156.168.*** and i want to ping some port that is on this IP which command i can do this? sam70 (1 Reply)
Discussion started by: sam70
1 Replies

10. Shell Programming and Scripting

ping -t

I need a script that recieves an IP adress and a number of seconds as arguments. and display a massage if the IP replies or not. (8 Replies)
Discussion started by: jaber87
8 Replies
Login or Register to Ask a Question