nixCraft Linux Forum

nixCraft

Linux 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

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 11-20-2007, 11:01 PM
Junior Member
User
 
Join Date: Nov 2007
My distro: SuSE
Posts: 3
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
Sponsored Links
  #2 (permalink)  
Old 11-21-2007, 01:52 AM
Junior Member
User
 
Join Date: Nov 2007
My distro: SuSE
Posts: 3
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 11-21-2007, 11:07 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Any distro with shell
Posts: 901
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 | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #4 (permalink)  
Old 11-21-2007, 02:53 PM
Junior Member
User
 
Join Date: Nov 2007
Location: India
My distro: Fedora core
Posts: 3
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 11-21-2007, 09:41 PM
Junior Member
User
 
Join Date: Nov 2007
My distro: SuSE
Posts: 3
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 11-21-2007, 11:02 PM
raj raj is offline
Contributors
User
 
Join Date: Jun 2005
Location: Hyderabad
Posts: 144
Rep Power: 4
raj is on a distinguished road
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

Bookmarks


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 On

Similar Threads

Thread Thread Starter Forum Replies Last Post
log mac address in squid log sunlinux Networking, Firewalls and Security 1 09-04-2007 02:32 AM
Ldap authentication on the basis of Mac address bijayant Networking, Firewalls and Security 0 08-01-2007 11:47 AM
Grep ip address in Linux or UNIX sweta Getting started tutorials 0 07-27-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 10-13-2006 01:11 AM


All times are GMT +5.5. The time now is 07:04 AM.


Powered by vBulletin® Version 3.7.2 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

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