Link to home
Start Free TrialLog in
Avatar of Ned_Kelly
Ned_KellyFlag for Australia

asked on

Shell scripting.

Hi folks, my program accesses the /etc/hosts file so you can add, search, delete, edit and view the ip address and the host name.  I also need to check if the ip address is correct as far as the format layout of the ip address is concerned.  Is the ip address in a valid format using numbers from 0 - 255 and in an octal format.
Many thanks for any help.
Regards Ned
Avatar of Triskelion
Triskelion
Flag of United States of America image

I don' truly understand the question, but here goes:

No, the IP address is not octal, they are octets (groups of 8 bits).  That is the number of bits necessary to represent 0-255.

As for the other piece, are you looking to create a program or do you already have one that parses and manipulates the hosts file?

If you have not created it yet, I wouldn't.
A text editor would do just as well without the hassle.
You /could/ create a simple utility to ensure the format of the data, but anything more than that is just emulating what can be done with a text editor.
Avatar of Ned_Kelly

ASKER

I already have the program, I just have to validate the ip address before it is written to file.
This is for an assignment for school.
Regards Ned
OK then, so what is the rest of the question?
Is the question "How do you validate an IP address"?

1) Must be between 7 characters and 15 characters long
2) by the 4th character, you must encounter a period (dot)

...that kind of stuff or something different?

----------------------------------------

Here are the "What If's" that would keep me from making a "hosts" manipulator:

What if the IP address has more than one host name?
   - How many are allowable by the RFC or by your program?

What if someone has put in an entry that does not adhere to the RFC-952?
   - Do you modify or truncate them?
   - Is there notification to be sent (in case programs break after the mod.)?

What happens to comments?
   - multi-line comments?
   - how long will/can comments be?

What if someone manipulates the file without using your program?

What if the permissions are not set fo write mode after you've made edits?
HI, a lot of what ifs.  
The program copies the hosts file as hosts.old when the program runs.  It also sets the permissions in the script as root rwx and users r-x.
I only need to check that each octet is between 0-255 and that there is a period between each number.
Ned
# csh syntax
foreach f ( `awk '($1 ~ /#.*/){next;}($NR==0){next}{print $1}' /etc/hosts` )
echo $f|egrep '^([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])$';echo $status
end
# prints status <>0 if wrong IP
# to be improved in many ways
Hi Folks, Thanks for your comments.
Your solution to my problem is above my head, ahoffmann.
I have entered it in the addpairs, which is the script for adding the IP and Host name.  I have a slight understanding  in my reading of your solution but I didn't think it would be so involved.  
Hi Folks, Thanks for your comments.
Your solution to my problem is above my head, ahoffmann.
I have entered it in the addpairs, which is the script for adding the IP and Host name.  I have a slight understanding  in my reading of your solution but I didn't think it would be so involved.  
Sorry, pressed the wrong key, before I had finished.
I understand the grep and the awk to some degree, $1 is the first field, print, yes, next is self explanatory, the next bit I'm unsure of.
~ /#.*/){next;}($NR==0)
/#.*  maybe number separated by a period.
$NR==0  No record equals zero
$f|egrep  $f variable,  | pipe,  e  execute,
'^([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.
' ^ unsure of
([01]? first number can be between [0-9], I don't know why twice.  |2 second number, not sure why the first [0-4] only goes to 4, the |25 can only go to 255
\. period yes, but not sure on the \
Many thanks for you participation.
Regards Ned
/m.*/     skip comment lines
(NR==0)     is a typo, sorry, should be:
(NF==0)     skip empty lines
^     start of word/line (both: awk and egrep)
([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])
     that's the regular expression for the range: 1..255
\.     period, yes: needs to be escaped 'cause it's a egrep meta character

And EE's formating smashed the line, it should read like:
  echo $f|egrep '....';
  echo $status

Why you simply try it ;-)
Hi ahoffmann, the awk line I get a syntax error with the ( ) around the awk line, if I take them out the addpairs will run but I then get the following two errors.
./addpairs foreach command not found
egrep invalid back reference.
Thanks for your help.
Ned
Hi ahoffmann, the awk line I get a syntax error with the ( ) around the awk line, if I take them out the addpairs will run but I then get the following two errors.
./addpairs foreach command not found
egrep invalid back reference.
Thanks for your help.
Ned
this command works as I posted it (the NR vs. NF typo doesnt matter).
Please post the complete line you exected.
Do you probably use another shell (than csh or tcsh)?
Which UNIX flaviour do you have?
Hi ahoffmann, sorry I overlooked the csh shell. I am using bash with redhat 7.1, I'm not sure if this will change things.
I'm not sure what you mean by the complete line, so I posted the addpairs script.

#====================================================
trap "rm -/tmp/* 2> /dev/null; exit" 0 1 2 3
hostfile=/etc/hosts
looptest=y
while [ "$looptest" = y ]
do
     clear
     cursor 1 4; echo "       Hosts File"
     cursor 2 4; echo "========================="
     cursor 4 4; echo "IP Address : "
     cursor 5 4; echo "Host Name  : "
     cursor 6 4; echo "Add Another? (Y)es or (Q)uit "
     cursor 4 16; read ip
     cursor 5 16; read host
     cursor 8 4;
     grep "$ip" $hostfile
     # Check to see if IP and host is there before you write to disk
     if [ "$ip" > "               " -a "$host" > "                  " ]
          then echo $ip    $host >> $hostfile
          else echo "Please insert both the IP address and a Host name"
     fi
     # csh syntax
     foreach f ( ` awk ' ($1 ~ /#.*/) {next;} ($NF==0) {next} {print $1} ' /etc/hosts` )
     echo $f|egrep '^([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])$';
     echo $status
     end
     # prints status <>0 if wrong IP
     # to be improved
     cursor 12 30; read looptest
     if [ "$looptest" = 'q' ]
          then  clear; exit
     fi
done

Regards Ned


ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Ahoffmann, Thankyou for your help, I'm very greatful for your comments and advice.  It was only a short module on shell programming that followed linux install and configure and network.  I have a long road ahead, but at least I have started.
Regards Ned