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! |
Hi and welcome to LinuxQuestions!
Quote:
Code:
subnet=$(echo $begin | sed 's/\.[0-4]*$//') Quote:
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}' |
colucix inspired me :)
Code:
#!/bin/bash |
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. |
Quote:
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? |
Quote:
Another error I made: :o 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. |
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 |
Code:
$ ping -bc1 `ifconfig | grep 'Bcast:' | cut -d":" -f3 | cut -d\ -f1` |
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!!! |
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?? |
Quote:
|
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). |
hi,
I am new to shell scripting in sed and regular expression. colucix can you please explain Quote:
Quote:
|
Please start your own question and not resurrect a 4 year old thread :)
|
All times are GMT -5. The time now is 07:54 PM. |