nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Validate IP Address

This is a discussion on Validate IP Address within the Shell scripting forums, part of the Development/Scripting category; I have a script, which gathers input from the user, and I want to validate an IP that is entered. ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 20-11-2007, 11:01 PM
Junior Member
User
 
Join Date: Nov 2007
OS: SuSE
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
devxtech is on a distinguished road
Default Validate IP Address

I have a script, which gathers input from the user, and I want to validate an IP that is entered. How can I validate an IP to make sure it is indeed an IP address without having to echo it out and run grep + regular expression on the variable?
Reply With Quote
  #2 (permalink)  
Old 21-11-2007, 01:52 AM
Junior Member
User
 
Join Date: Nov 2007
OS: SuSE
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
devxtech is on a distinguished road
Default

Got it figured out. For reference for anyone else here is what I did.

PHP Code:
IP_ADDR_VAL=(echo "$ipadd | grep -Ec ^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])'")
if ! [ 
IP_ADDR_VAL == ]; then
    
echo -"Bad IP address: $ipadd\nTry again with correct IP Address.\nNo data has been entered into DNS."
    
exit 2
fi 
Reply With Quote
  #3 (permalink)  
Old 21-11-2007, 11:07 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Yup using sed or grep or awk you can validate ip. There is also command but I don't remember its name.
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #4 (permalink)  
Old 21-11-2007, 02:53 PM
Junior Member
User
 
Join Date: Nov 2007
Location: India
OS: Fedora core
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
sanjustudy is on a distinguished road
Send a message via Yahoo to sanjustudy
Default IP Address reconginsation

Hi,
it depends on the OS your using u need to define the hard code format for Windows/Linux for recoginsation of IP address for eg: in linux it will be always of type
192.168.12.200 like that but it changes for windows,so better have switch cases and using string format condition but i prefer regexp more it works effectively.

Regards,
Sanju
Reply With Quote
  #5 (permalink)  
Old 21-11-2007, 09:41 PM
Junior Member
User
 
Join Date: Nov 2007
OS: SuSE
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
devxtech is on a distinguished road
Default

I made a correction in the script. Use the following for reference if anyone needs to.

PHP Code:
IP_ADDR_VAL=$(echo "$ipadd" grep -Ec '^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])')

if [ 
$IP_ADDR_VAL -eq 0 ]; then
    
echo -"*** Bad ip address: $ipadd\nTry again with correct IP Address.\nNo data has been entered into DNS."
    
exit 2
fi 
Reply With Quote
  #6 (permalink)  
Old 21-11-2007, 11:02 PM
raj's Avatar
raj raj is offline
Senior Member
User
 
Join Date: Jun 2005
Location: Hyderabad
OS: Fedora, Debian Linux
Posts: 307
Thanks: 42
Thanked 8 Times in 8 Posts
Rep Power: 6
raj will become famous soon enough raj will become famous soon enough
Default

wow! thanks dude for sharing the soultion
__________________
Raj
Linux rulz.
I have never turned back in my life ; I shall not do so today.. haha
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
log mac address in squid log sunlinux Networking, Firewalls and Security 1 04-09-2007 02:32 AM
Ldap authentication on the basis of Mac address bijayant Networking, Firewalls and Security 0 01-08-2007 11:47 AM
Grep ip address in Linux or UNIX sweta Getting started tutorials 0 27-07-2007 06:47 AM
Iptables block ip address raj Getting started tutorials 0 05-05-2007 01:01 AM
airtel dns server ip address sweta Windows Xp/2000/2003 server administration 2 13-10-2006 01:11 AM


All times are GMT +5.5. The time now is 11:27 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38