Hello mighty unix pros could you please check the unix script i have done and tell me how to massivly improve the middle part?
it works right now but i know it can be done easier, intelligenter more beautiful, sexier .......
all this script does is to scan a D network for
ip adress and domain name
Code:
print_intro()
{
echo
echo "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
echo " IP: $1 "
echo " IP: $2 "
echo " $3 entries from $4 to `date | cut -f5 -d' '` "
echo "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
echo
}
check_valid()
{
if (!(echo $1 | grep '^[0-1]\?[0-9]\?[0-9]\?$' || echo $1 | grep '^2[0-5][0-5]$') | >/dev/null)
then
echo "IP Adress invalid [1-255]"
exit
fi
}
if [ $# != 4 ]
then
echo "You have to specify exactly 4 parameters"
echo "$0 <startip> <endip> <ips_dns> <ips_ip>"
exit
else
echo "Starting ..."
startip=$1
endip=$2
date_start=`date | cut -f5 -d' '`
This part isnt very neat ... and i really tried some time to read in the ip adress parts in a shorter way ... but somehow the syntax was never exactly right.
Some hint to the right direction would be nice.
i know i can use the 'read' command but as i said the syntax was always a bit wrong.
Code:
IFS="."
sipA=`echo $startip | cut -f1 -d' '`
sipB=`echo $startip | cut -f2 -d' '`
sipC=`echo $startip | cut -f3 -d' '`
sipD=`echo $startip | cut -f4 -d' '`
eipA=`echo $endip | cut -f1 -d' '`
eipB=`echo $endip | cut -f2 -d' '`
eipC=`echo $endip | cut -f3 -d' '`
eipD=`echo $endip | cut -f4 -d' '`
IFS=" "
check_field=($sipA $sipB $sipC $sipD $eipA 4eipB $eipC $eipD)
for check in check_field
do
check_valid $check
done
didnt work. i thought it takes one parameter of the field every turn.
but it took all at once
Code:
check_valid $sipA
check_valid $sipB
check_valid $sipC
check_valid $sipD
check_valid $eipA
check_valid $eipB
check_valid $eipC
check_valid $eipD
Code:
if [ $sipA -ne $eipA ] || [ $sipB -ne $eipB ] || [ $sipC -ne $eipC ];
then
echo "You can't scan A,B and C networks."
exit
elif [ $sipD -gt $eipD ]
then
echo "StartIP has to be smaller than the EndIP"
exit
fi
count=`tail -n 1 $4 | cut -f1 -d':'`
runip=$startip
while [ $sipD -le $eipD ]
do
if [ `ping $runip -w 1 -c1 -q | grep transmitted | cut -f4 -d' '` -eq 1 ]
then
count=$(expr $count + 1)
echo IP $runip is `traceroute $runip | grep $runip | cut -f4 -d' '` >> $3
echo $count:$runip >> $4
sipD=$(expr $sipD + 1)
runip=$sipA.$sipB.$sipC.$sipD
else
echo "## $runip - unreachable ##"
sipD=$(expr $sipD + 1)
runip=$sipA.$sipB.$sipC.$sipD
fi
done
fi
print_intro $startip $endip $count $date_start
i hope someone can help me with the two parts i mentioned cause they even hurt my noobie eye when i look at them
thanx a lot