Reputation: 99
I'm working on a script in Linux Bash, with different kinds of options to use. Basically, the program is going to ping to the given ip-address.
Now, I want to enable the user to write a range of ip-adresses in the terminal, which the program then will ping.
Fe: bash pingscript 25 - 125
the script will then ping all the addresses between 192.168.1.25 and 192.168.1.125.
That's not to hard, I just need to write a little case with
[0-9]-[0-9] ) ping (rest of code)
Now the problem is: this piece of code will only enable me to ping numbers of fe. 0 - 9 and not 10 - 25.
For that I'd need to write: [0-9][0-9]-[0-9][0-9] (fe: ping 25 - 50)
But then there's the possibility of having 1 number on one side and 2 on the other: [0-9]-[0-9][0-9] (fe: ping 1 - 25)
or: [0-9]-[0-9][0-9][0-9] (fe: ping 1 - 125)
and so on... That means there're a lot of possibilities. There's probably another way to write it, but how?
I don't want any letters to be in the arguments, but I can't start with that (loop-through system).
Upvotes: 2
Views: 912
Reputation: 7810
You can use extended pattern matching in your script by enabling the extglob
shell option:
shopt -s extglob
So you can use braces with a +
quantifier like this:
#!/bin/bash
shopt -s extglob
case $1 in
+([0-9])-+([0-9]) )
# do stuff
;;
esac
Upvotes: 2
Reputation: 9302
How about that:
for i in 192.168.1.{25..125}; do ping -qnc1 $i; done
Or in a script with variables as arguments:
for i in $(seq -f "192.168.1.%g" $1 $2); do ping -qnc1 -W1 $i; done
Where the first argument is the number where to begin and the second argument where to end. Call the script like this:
./script 25 125
The ping
options:
-q
: that ping doesn't print a summary-n
: no dns lookups-c1
: Only send 1 package-W1
: timeout to 1 second (can be increased of cource)Upvotes: 3