?
Solved

Shell scripting.

Posted on 2002-06-17
15
Medium Priority
?
265 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:Ned_Kelly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
  • 2
15 Comments
 
LVL 6

Expert Comment

by:Triskelion
ID: 7088697
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.
0
 

Author Comment

by:Ned_Kelly
ID: 7088776
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
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 7088890
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?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Ned_Kelly
ID: 7089063
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
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7090548
# 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
0
 

Author Comment

by:Ned_Kelly
ID: 7096158
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.  
0
 

Author Comment

by:Ned_Kelly
ID: 7096160
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.  
0
 

Author Comment

by:Ned_Kelly
ID: 7096256
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
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7096502
/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 ;-)
0
 

Author Comment

by:Ned_Kelly
ID: 7096646
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
0
 

Author Comment

by:Ned_Kelly
ID: 7096685
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
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7097111
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?
0
 

Author Comment

by:Ned_Kelly
ID: 7098436
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


0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 200 total points
ID: 7098638
ok, for bash, sh, and ksh the foreach looks like:

for f in` awk ' ($1 ~ /#.*/) {next;} ($NF==0) {next} {print $1} ' /etc/hosts`; do
  echo $f | egrep ...
done
0
 

Author Comment

by:Ned_Kelly
ID: 7107426
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question