ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
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
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:
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.
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?
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.
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.
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).
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.