Reputation: 117
I'm trying to combine these 2 arguments to make my function work
#!/bin/bash
while [ $? -gt 0 ]
do
case "$1" in
[0-9]*-[0-9]*)
for ip in $(sec ${1%-*} ${##*-})
do
ping -c 1 192.168.1.$ip
(shift)?
done
;;
a)
>/dev/null;
[ $? -eq 0 ] && echo "192.168.1.$ip is up!" ||:;
;;
esac
done
Normally if I put it both functions in the [0-9]*-[0-9]*)
argument we can get for example as output
someTest.sh 90-105
It would check for IP numbers between 90 and 105 But i would like to do it like this:
sometest.sh 90-105 -a
Upvotes: 3
Views: 3211
Reputation: 101
If what you want to do is ping IPs within a certain range, and also to be able to pass additional arguments (-a and/or -b), you could look at getopts to easily deal with as many arguments as you need, with or without options:
usage()
{
cat << EOF
usage: $0 options iprange
OPTIONS:
-a set a option
-b set b option
-r set rangei, e.g 1 10
EOF
}
A=
B=
while getopts “abr:” OPTION
do
case $OPTION in
a)
A=1
;;
b)
B=1
;;
r)
RANGE="$OPTARG"
;;
?)
usage
exit
;;
esac
done
for i in $(seq $RANGE); do
ping -c 1 -w 1 192.168.1.$i >> /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "192.168.1.$i is up";
else
echo "192.168.1.$i is down";
fi
done
You would call this like:
./somescript.sh -a -b -r "10 34"
You can check the $A and $B variables and do whatever you want to do based on their values.
I know, no dash between the range delimeters and you need the quotes, wrote this quickly, you could use something like KingsIndian's solution above to deal with that.
Upvotes: 0
Reputation: 121397
I see that you want to check whether the machines are up or down from the range you specify from the command line. You can simply do it using a for loop.
#!/bin/bash
a=$1;
b=$2;
for ((i=a;i<=b;i++)) do
ping -c 1 -w 1 192.168.1.$i >> /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "192.168.1.$i is up";
else
echo "192.168.1.$i is down";
fi
done
From the command line, run it: ./script 10 50
to ping the machines from 192.168.1.10 to 192.168.1.50.
If you want to pass the arguments like: ./script 10-50
then you can do that as well:
#!/bin/bash
OLDIFS=$IFS
IFS=$'\-'
set $@
a=$1;
shift;
b=$1;
for ((i=a;i<=b;i++)) do
ping -c 1 -w 1 192.168.1.$i >> /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "192.168.1.$i is up";
else
echo "192.168.1.$i is down";
fi
done
IFS=$OLDIFS
Upvotes: 1