LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-04-2005, 07:53 AM   #1
kirmet
Member
 
Registered: Sep 2005
Location: austria
Distribution: fedora,backtrack
Posts: 31

Rep: Reputation: 15
unix scrpit simplification


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
 
Old 10-05-2005, 05:37 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
It's not too bad.
short and sweet.


one way to split for the read:

Code:
(IFS=.; echo $1) |
while read  a b c d;do
    echo a=$a b=$b c=$c d=$d
done
using the () doesn't break IFS for subsequent reads and prints.

Code:
$ ksh read 123.345.123.234
a=123 b=345 c=123 d=234
Also, I much prefer:
Code:
print_intro()
{
cat <<EOF
	
	 -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
	   	IP: $1							
	   	IP: $2							
	   	$3 entries from $4 to `date | cut -f5 -d' '`		
	 -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
EOF
}
much easier to edit
 
Old 10-05-2005, 05:53 AM   #3
zeropash
Member
 
Registered: Apr 2003
Location: Bangalore,India
Distribution: FC2, RHES, RH9, FC3, FC1, Slackware 3.0
Posts: 208

Rep: Reputation: 30
My try you can pass check_valid ip address of the form 192.1.1.1 etc
------
check valid () {
echo "44.1.1.1" | awk -F "." '{for(i=1;i<5;i++) { if ($i > 255) exit 1 }; {if ($1"."$2"."$3"."$4 != $0) exit 2} }'
if [ $1 -ne 0 ]; then
echo "bad ip"
fi
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
scrpit for finding out new entries in /etc/passwd icefairy83 Linux - Security 8 05-27-2005 11:35 AM
Unix Book for Beginners (Not Linux... Unix) suse2166 General 6 11-25-2004 11:46 AM
UNIX (Linux, BSD, etc) Programming :: UNIX kuphryn Programming 8 04-04-2004 11:50 PM
Why did you experienced users of Unix change to unix over Windows? Laptop2250 Linux - General 11 10-28-2003 11:51 AM
How to schedule unix script periodically from unix os level??? gopi_20us Programming 2 03-11-2002 06:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration