Validate an IP address - Return to menu


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Validate an IP address - Return to menu
# 1  
Old 12-03-2009
MySQL Validate an IP address - Return to menu

hi!

I need a script that, among other things, presents a menu to the user, checks the user input for an IP address (in quad dotted notation), and then "redirects" (with sed) that IP address to the Firewall script.

The script must check that all of the inputs are valid IP addresses (numeric, within 1-255 range, etc).

Well, I have found here on this Forums a magnificent script that does just that. The script is posted bellow.

But the script has a problem... Whenever the user introduces an invalid IP address the script just exits.

I would like another kind of behaviour: When the user introduces an invalid IP address the script should go back, presenting the menu again to the user and re-asking him to introduce a valid IP address.

Could someone please help me? Please someone modify the script bellow in order to accomplish the desired behaviour. I need this to terminate a professional work.


Code:
#!/bin/sh

echo "Enter IP address"
read quad

oldIFS=$IFS
IFS=.
set -- $quad

if [ "$#" -ne "4" ]; then
  echo "Must have 4 parts"
  exit 1
fi

for oct in $1 $2 $3 $4; do
  echo $oct | egrep "^[0-9]+$" >/dev/null 2>&1
  if [ "$?" -ne "0" ]; then
     echo "$oct: Not numeric"
     exit 1
  else
     if [ "$oct" -lt "0" -o "$oct" -gt "255" ]; then
        echo "$oct: Out of range"
        exit 1
     fi
  fi
done

echo "$quad" | grep "\.$" >/dev/null 2>&1
if [ "$?" -eq "0" ]; then
  echo "Trailing period - invalid"
  exit 1
fi

if [ "$1" -eq "0" ]
then
echo "First octet must not be zero (0)..."
exit 1
fi

# if we're here, we're validated
echo "$quad validates OK!"
exit 0


Last edited by pludi; 12-04-2009 at 03:07 AM.. Reason: code tags, please...
# 2  
Old 12-04-2009
OK I brushed it up a little bit so that is handles ".1.1.1" :
Code:
#!/bin/sh
set -x
oldIFS=$IFS
while : ; do
  IFS=$oldIFS
  echo "Enter IP address"
  read quad

  IFS=.
  set -- $quad

  if [ $# -ne 4 ]; then
    echo "Must have 4 parts"
    continue
  fi

  for oct in $1 $2 $3 $4; do
    echo $oct | egrep "^[0-9]+$" >/dev/null 2>&1
    if [ "$?" -ne "0" ]; then
       echo "$oct: Not numeric"
       continue 2
    else
       if [ "$oct" -lt "0" -o "$oct" -gt "255" ]; then
          echo "$oct: Out of range"
          continue 2
       fi
    fi
  done

  echo "$quad" | grep "\.$" >/dev/null 2>&1
  if [ "$?" -eq "0" ]; then
    echo "Trailing period - invalid"
    continue
  fi

  if [ -n $1 ] || [ $1 -eq 0 ]
  then
    echo "First octet must not be zero (0)..."
    continue
  fi

  # if we're here, we're validated
  echo "$quad validates OK!"
  exit 0
done

Not my cup of tea though. Check CFA Johnson's solution:

Last edited by Scrutinizer; 12-04-2009 at 04:02 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Gnome 3.28.3 menu item dissapears under the system menu

I installed CentOS 8 with Gnome 3.28.2 and I noticed that the "switch user" menu item disappeared from under the system menu of Gnome classic (Both X11 & Wayland). I checked google and this problem seems to have a history going back several releases of Gnome. Unfortunately, I never found a... (1 Reply)
Discussion started by: bodisha
1 Replies

2. IP Networking

Set default return address for all mails sent from mailx

Hi, I have a question about mailx in Red Hat: If I use 'mail userid@mydomain.com -- -f root@mydomain.com', the return mail address will be root@mydomain.com. How can I make all mails sent from this system use root@mydomain.com as return address by default? Thank you in advance! nz (1 Reply)
Discussion started by: aixlover
1 Replies

3. Shell Programming and Scripting

Need to get return code from mutt if an address is invalid/undeliverable from Unix shell script

I am using mutt on ksh Unix to send emails to addresses plucked from the database. If the "To:" email address is not longer valid and so the email is not sent to the "To:" recipient, but is sent to the valid cc address, I need to be able to get an error code returned to the shell script so that... (3 Replies)
Discussion started by: jzuber
3 Replies

4. Programming

validate IPV6 address in windows using c++

I know there is a function inet_tpon for unix platforms to validate ipv6 addresses.But i need an equivalent of windows.When i use this function with the header file <winsock2.h> the visual studio 2005 on win2003 issues an error saying identifier not found :confused: (3 Replies)
Discussion started by: guru13
3 Replies

5. Shell Programming and Scripting

Scripting problem - when the file is not found i want it to return to the menu

when the file is not found i want it to return to the menu, however it carries out the next line when i hit a key I know its probably something simple can anyone help? here is my pause function: function pause(){ read -s -n 1 -p "Press any key to return to Menu . . ." echo } SCRIPT... (2 Replies)
Discussion started by: Alendrin
2 Replies

6. Shell Programming and Scripting

How to validate an IP address

hi I need a simple script to do the following. I need to check the user input for 1. quad dotted notation. 2. check that all of the inputs are numeric and within 1-255 range. Could someone please help me? I tried to use egrep to check for dotted notation. quad=1.1.1.12 ... (5 Replies)
Discussion started by: sabina
5 Replies

7. Shell Programming and Scripting

Read Hostname and Return IP Address

Dear Experts, I have a text file on my itanium box which contains list of nearly 1000 hostnames. I do not have the IP Address of them. I want read each entry from the text file and want to do a nslookup to the DNS Server and get the IP Adresses and put them in another file in the format... (1 Reply)
Discussion started by: PrasannaKS
1 Replies

8. UNIX for Dummies Questions & Answers

return IP address

Hi, If my IP address is 192.29.0.131 I want output _0_131 # /sbin/ifconfig | grep "inet addr" | grep 192 | cut -d "." -f3 0 I have tried the above and I get third digit. I want forth digit as well separated by underscore. Or is there any better approach? (4 Replies)
Discussion started by: shantanuo
4 Replies

9. Programming

Return IP address

Not sure whether this is an appropriate request, however, Using C++ code I'm running a unix command from the pc that needs the IP address of the PC in order to set the DISPLAY variable. The code is written using Builder C++ (Borland) but I'm having difficulty finding what functions / header... (3 Replies)
Discussion started by: gefa
3 Replies

10. IP Networking

pine username return address

hello all I am happily using pine but unhappily sending email as username@where.my.mail.works instead of ****@my.college.edu well, my username on my computer is one thing, but my username on my college account (**** here) is different. what I'm trying to do is use the college server for... (1 Reply)
Discussion started by: plusran
1 Replies
Login or Register to Ask a Question