LinuxQuestions.org
Visit Jeremy's Blog.
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 04-11-2011, 04:47 AM   #1
ugurgazi
LQ Newbie
 
Registered: Apr 2011
Posts: 10

Rep: Reputation: 0
Bash script to ping a range or own IP-range


Hello!
I have a question foy those who can/wants to help me out.

I want to build a bash script, which can ping a range IP adresses which will be filled in by the admin. If there is no IP-adress filled in, then the script must ping the subnet where the system is logged on.

So if my ip is 192.168.1.6, then the script must ping from 192.168.1.1 till 192.168.1.255
Or else, if there is given a beginning and ending ip it must ping that!


The first part of the bash script is to ping a given range (see below). But there is one problem, how can I tell the script to ping from $begin till $end, [..] is of course wrong!
But what must be filled in there???

echo "Enter beginning IP-adres:"
read begin
echo "Enter ending IP-adres:"
read end
ping -c 1 $begin [..] $end


The second part is to find my own ip and ping the whole range.. How to do that?
I only can find my own IP, but I cant ping the whole range,, how to do that??
#!/bin/bash
ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' |
cut -d: -f2 | awk '{ print $1 }'


Many thanks in advance!!!!
Help will be wonderful!
 
Old 04-11-2011, 06:45 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Hi and welcome to LinuxQuestions!
Quote:
Originally Posted by ugurgazi View Post
The first part of the bash script is to ping a given range (see below). But there is one problem, how can I tell the script to ping from $begin till $end
You can try a loop to cycle over the range of IP addresses. First you have to extract information from the given IPs, then do a loop incrementing the address from the beginning to the end of the range. Example:
Code:
subnet=$(echo $begin | sed 's/\.[0-4]*$//')

start=$(echo $begin | sed 's/.*\.//')
stop=$(echo $end | sed 's/.*\.//')

while [[ $start -le $stop ]]
do
  ping -c 1 $subnet.$start
  ((start++))
done
Two notes: first you have to check the input from the user, e.g. are they valid IP addresses? Is the begin address before the end address in the sequence? And so on... Second, are you sure you want to ping the broadcast address, since it can generate a lot of traffic? What is the purpose of that?
Quote:
Originally Posted by ugurgazi View Post
The second part is to find my own ip and ping the whole range.. How to do that?
I only can find my own IP, but I cant ping the whole range,, how to do that??
You can do that only from the router and it depends on how the subnet is created. For example, are they fixed IP addresses or are they assigned dinamically through DHCP? You may always ping the whole IP range of the subnet from 1 to 254, but... if you explain the purpose of this script maybe someone more experienced in network issues can give some better advice.

A final note: to extract the IP address from the output of ifconfig, you might use only awk. Example:
Code:
/sbin/ifconfig | '/inet addr:/ && ! /127.0.0.1/{sub(/inet addr:/,""); print $1}'
Hope this helps.
 
1 members found this post helpful.
Old 04-11-2011, 08:55 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
colucix inspired me
Code:
#!/bin/bash

usage()
{
    echo "Usage: $0 [start_address end_address]"
    exit 1
}

if (( $# == 1 || $# > 2 ))
then
    usage
elif (( $# == 2 ))
then
    REGEX='^[1-9][0-9]{,2}[.]([0-9]{1,3}[.]){2}[0-9]{1,3}$'
    BEGIN=$1
    END=$2

    if ! [[ $BEGIN =~ $REGEX && $END =~ $REGEX ]]
    then
        usage
    elif [[ ${BEGIN%[0-9]*} == ${END%[0-9]*} ]]
    then
        SUBNET=${BEGIN%[0-9]*}
        START=${BEGIN##*.}
        STOP=${END##*.}
    else
        echo "First three octets must match when entering range!!"
        exit 2
    fi
else
    START=1
    STOP=254
    SUBNET=$(/sbin/ifconfig | awk '!/lo/{print gensub(/[^:]*:|[^.]+$/,"","g",$7)}' RS="")
fi

#in case you wish to kill it
trap 'exit 3' 1 2 3 15

while (( START <= STOP ))
do
    ping -c 1 $SUBNET$START
    ((START++))
done

Last edited by grail; 04-11-2011 at 09:04 AM.
 
Old 04-11-2011, 11:54 AM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
If IP addresses segments are three digit octal numbers, wouldn't the range span be %[0-8]* rather than %[0-9]*? And aren't they two bit unsigned integer values, so 0 <= ((address+0)) < 256?

I wonder, since I've never tried it, if one could use two digit hex digits, x00 ... xFF, for the IP address components rather than octal? That might make the map from www/xxx/yyy/zzz to "unsigned int64" somewhat more explicit, although I don't suppose that there is any need for that.

I.e., I think the "valid address" test could be a little sharper.
 
Old 04-11-2011, 10:48 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
If IP addresses segments are three digit octal numbers, wouldn't the range span be %[0-8]* rather than %[0-9]*? And aren't they two bit unsigned integer values, so 0 <= ((address+0)) < 256?

I wonder, since I've never tried it, if one could use two digit hex digits, x00 ... xFF, for the IP address components rather than octal? That might make the map from www/xxx/yyy/zzz to "unsigned int64" somewhat more explicit, although I don't suppose that there is any need for that.

I.e., I think the "valid address" test could be a little sharper.
Hey PT, Yes I am open to suggestion as I am a novice to intermediate network person Whilst I do understand that the digits are referred to as an octet
in an ip address, I was not aware that a particular decimal value was not allowed??

On my home network the first 3 'octets' are 192.168.1, as the first one has a 9 in it does this not disprove that the limit to individual digits is an 8?
Also my work network uses all ip's in the range 75 - 254 for workstations, so again a machine can have 249 as last octet.

If I have missed the point or am confused (easily done sometimes ) please advise where my thinking has gone astray?
 
Old 04-12-2011, 04:35 PM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by grail View Post
Hey PT, Yes I am open to suggestion as I am a novice to intermediate network person Whilst I do understand that the digits are referred to as an octet
in an ip address, I was not aware that a particular decimal value was not allowed??

On my home network the first 3 'octets' are 192.168.1, as the first one has a 9 in it does this not disprove that the limit to individual digits is an 8?
...
Oops! No, I was confused by the "octet" comment you included, and the time of night when I was posting. The second part of my comment, that the numbers in the four "positions" of the address are in the range [0-256) as a decimal number (or [\x00=\x100) as a hexadecimal number) is correct. So the check could be sharpened to check that the value in in the correct range.

Another error I made: The IPv4 address is a 32-bit unsigned integer, not a 64-bit one.

I do wonder if the OP needs to consider private, non-routable, address spaces, that is, the /10.0-255.0-255.0-255/, /172.16-31.0-255,0-255/. and /192.168.0-255,0-255/ ones.
 
Old 04-14-2011, 07:47 AM   #7
ugurgazi
LQ Newbie
 
Registered: Apr 2011
Posts: 10

Original Poster
Rep: Reputation: 0
So far so good.....


ifconfig | grep 'Bcast:' | cut -d: -f3 | awk '{print $1}'


with the above I can see by broadcast IP...
but how can I tell the script in the same line to ping the output??

Output for: ifconfig | grep 'Bcast:' | cut -d: -f3 | awk '{print $1}' is 192.168.101.255 or something else. How can I also ping -b the output?



Thanks in advance
 
Old 04-14-2011, 05:10 PM   #8
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Code:
$ ping -bc1 `ifconfig | grep 'Bcast:' | cut -d":" -f3 | cut -d\  -f1`
WARNING: pinging broadcast address
PING 192.168.0.255 (192.168.0.255) 56(84) bytes of data.
^C
--- 192.168.0.255 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms

Last edited by PTrenholme; 04-14-2011 at 05:12 PM.
 
Old 04-26-2011, 04:18 AM   #9
ugurgazi
LQ Newbie
 
Registered: Apr 2011
Posts: 10

Original Poster
Rep: Reputation: 0
Hello again,
I have got 2 scripts now which I want to place in one script..
When no input is given at first part then the second part must be run.. How can I accomplish that with if else statement?
When no beginning and ending ip is given, then run a ping to broadcast.


#INPUT 1
#!/bin/bash
read -p "Enter beginning IP-addres: " host1
read -p "Enter ending IP-addres: " host2
SUBNET=${host1%.*}
netId1=${host1#$SUBNET.}
netId2=${host2#$SUBNET.}
for ((i=netId1; i<=netId2; i++)); do
ping -c 1 ${SUBNET}.$i
done


#INPUT 2
#!/bin/bash
ping -bc 2 'ifconfig | grep 'Bcast:' | cut -d ":" -f3 | cut -d\ -f1'



Many thanks in advance!!!
 
Old 04-26-2011, 04:41 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I would have to say I am feeling a little neglected ... did you bother to read the script I put together?

Not only is there no need for 2 scripts but it also handled the situation where nothing is passed.

Maybe I missed something??
 
Old 04-28-2011, 08:44 PM   #11
Shao Lung
Member
 
Registered: Apr 2011
Posts: 71

Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Well I would have to say I am feeling a little neglected ... did you bother to read the script I put together?

Not only is there no need for 2 scripts but it also handled the situation where nothing is passed.

Maybe I missed something??
Well I just tried your code, thats pretty sweet. I was trying to build something similar, though not limiting to my own computer. You rock!!
 
Old 04-29-2011, 08:54 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
As 8-bit numbers (octets) the range of a single octet is decimal 0 to 255 (octal 0 to 377, hex 0 to FF).

The netmask must be used to determine the range of IP addresses.

In the common case of netmask 255.255.255.0 (a.k.a /24), the possible range of addresses is x.x.x.1-254 (because 0 is the network address and 255 the broadcast address).

For netmask 255.255.0.0 I'm less confident what the range is; if the network address is x.x.0.0 and the broadcast is x.x.255.255 (I think that's right) then all other possibilities are the IP address range.

If the netmask splits an octet then calculating the range becomes harder; in the simple case of 255.255.255.252 the range is x.x.x.253 to 254 (252 is the network address and 255 the broadcast address).

EDIT: the netmask 255.255.255.252 part is wrong, it should be: the range is x.x.x.x+1 to x+2 (x+0 is the network address and x+3 the broadcast address).

Last edited by catkin; 04-29-2011 at 08:59 AM.
 
Old 05-02-2016, 07:43 AM   #13
sanju_sethi2004
LQ Newbie
 
Registered: Oct 2008
Posts: 2

Rep: Reputation: 0
hi,

I am new to shell scripting in sed and regular expression.

colucix can you please explain

Quote:
subnet=$(echo $begin | sed 's/\.[0-4]*$//')
grail please explain
Quote:
then
REGEX='^[1-9][0-9]{,2}[.]([0-9]{1,3}[.]){2}[0-9]{1,3}$'
BEGIN=$1
END=$2

if ! [[ $BEGIN =~ $REGEX && $END =~ $REGEX ]]
then
usage
elif [[ ${BEGIN%[0-9]*} == ${END%[0-9]*} ]]
then
SUBNET=${BEGIN%[0-9]*}
START=${BEGIN##*.}
STOP=${END##*.}
else
echo "First three octets must match when entering range!!"
exit 2
fi
 
Old 05-02-2016, 09:08 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please start your own question and not resurrect a 4 year old thread
 
  


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
[SOLVED] Bash mayhem with for loop and variable range oukourj Linux - Server 1 03-01-2011 10:15 AM
Bash script - Test variable against a range spartiati Programming 3 01-29-2010 01:35 AM
Question about moving a range of files with a Bash script surfrock66 Linux - Newbie 6 11-22-2009 03:37 PM
[SOLVED] What are short range link and long range links in routing? mq15 Linux - Networking 6 06-26-2009 11:16 PM
Ping ip range to find live ip addresses? ginda Linux - Networking 2 07-16-2005 09:58 PM

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

All times are GMT -5. The time now is 08:00 PM.

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