Register
It is currently Wed Apr 23, 2014 8:44 pm

Requesting IP verification help


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Fri Feb 18, 2011 1:59 pm   

Joined: Fri Feb 18, 2011 1:41 pm
Posts: 2
I've recently got back into a bit of shell scripting after starting a new job, and in an effort to speed up the build/configure process I have a script in the making. I have all of the OS/hardware verification automated with no user input necessary. However, in an effort to make this script user friendly for other employees I need a way to ensure the IP they submit is in the correct format under 255 and over 0 for the first octet.

I wrote the basis for it, but when the script runs it does not compare the values and ends up verifying the wrong information. I am not very experienced with shell scripting, and I am not even sure if using a function would work by forcing the user to re-input any invalid data or just continue down the rest of the code. I know there are plenty of better ways to complete this task, but it seemed that it was the easiest way to understand and complete for testing. I am completely open to suggestions, so I thank anyone that can help in any way possible.


Code:
NETID() {

echo Please enter a valid Network ID:
read NETID
IFS="."
set $NETID

#This is the if statement that is causing the problem, it is not correctly verifying user input.

if [ "255" > "$1" ] && [ "$1" > "0" ] && [ "255" > "$2" ] && [ "255" > "$3" ] && [ "255" > "$4" ]; then

echo "..." #I have code completed for calculating values
else

NETID #Will this restart the process or just cause unnecessary issues?

fi
}
NETID

#Lots of OS verifications and input of IP information into the appropriate config files


Top
 Profile  
 PostPosted: Sat Feb 19, 2011 12:15 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hi SteveC, welcome!

Here's a neat function that I found on another forum that looks pretty clean, using awk:

Code:
if [ -n "$(echo $IP|egrep '([0-9]{1,3}\.){3}[0-9]{1,3}')" ]; then
   echo "$IP" | awk -F. '{
      if ( (($1>=0) && ($1<=255)) &&
           (($2>=0) && ($2<=255)) &&
           (($3>=0) && ($3<=255)) &&
           (($4>=0) && ($4<=255)) ) {
         print($0 " is a valid IP address.");
      } else {
         print($0 ": IP address out of range!");
      }
   }'
else
   echo "${IP} is not a valid IP address!"
fi



I hope this helps!
-J


Top
 Profile YIM  
 PostPosted: Sat Feb 19, 2011 1:12 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Code:
if [ "255" > "$1" ] && [ "$1" > "0" ] && [ "255" > "$2" ] && [ "255" > "$3" ] && [ "255" > "$4" ]

the > character is used to redirect output from a command to a file (or device)
If you want a 'less than' or 'greater than' operator, lose the quotes and use -lt or -gt


Top
 Profile  
 PostPosted: Sat Feb 19, 2011 1:45 am   

Joined: Fri Feb 18, 2011 1:41 pm
Posts: 2
jeo wrote:
Hi SteveC, welcome!

Here's a neat function that I found on another forum that looks pretty clean, using awk:

Code:
if [ -n "$(echo $IP|egrep '([0-9]{1,3}\.){3}[0-9]{1,3}')" ]; then
   echo "$IP" | awk -F. '{
      if ( (($1>=0) && ($1<=255)) &&
           (($2>=0) && ($2<=255)) &&
           (($3>=0) && ($3<=255)) &&
           (($4>=0) && ($4<=255)) ) {
         print($0 " is a valid IP address.");
      } else {
         print($0 ": IP address out of range!");
      }
   }'
else
   echo "${IP} is not a valid IP address!"
fi



I hope this helps!
-J


Thanks, I'll try and mess with that because I am not too familiar with awk or |egrep and the format in the if statement that validates the numerical values. I'll take a look into some more guides so I can eventually filter different sets of user input. If you have any suggestions for tutorials I would greatly appreciate it.

Patsie wrote:
Code:
if [ "255" > "$1" ] && [ "$1" > "0" ] && [ "255" > "$2" ] && [ "255" > "$3" ] && [ "255" > "$4" ]

the > character is used to redirect output from a command to a file (or device)
If you want a 'less than' or 'greater than' operator, lose the quotes and use -lt or -gt


I actually did use the -... operator at first, but when it didn't work I go rid of it and never tried it again when I revised the actual validation sequence. I knew that the > or >> could replace/send text to a file, but I guess my inexperience didn't think it would effect an if statement. Thanks a lot though that will help with lots of things.



As for anyone else that may happen to look at this thread; if you know of any good tutorials for more advanced coding (with explanations of what the given code does broken down) please leave a link on this post. I'm really sorry if there are guides on the forum I missed (I looked through the stickies for the most part), but I am very new and plan to stick around for a while to learn and hopefully one day help people with their issues.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP