Check for valid hostnames


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check for valid hostnames
# 1  
Old 06-02-2016
Check for valid hostnames

Hello,

I am trying to develop a script to check for valid hostnames. Below are the prerequisites for a valid hostname which I got from wiki :

Hostnames are composed of series of labels concatenated with dots, as are all domain names. For example, "en.wikipedia.org" is a hostname. Each label must be between 1 and 63 characters long, and the entire hostname (including the delimiting dots but not a trailing dot) has a maximum of 253 ASCII characters.

The Internet standards (Requests for Comments) for protocols mandate that component hostname labels may contain only the ASCII letters 'a' through 'z' (in a case-insensitive manner), the digits '0' through '9', and the hyphen ('-'). The original specification of hostnames in RFC 952, mandated that labels could not start with a digit or with a hyphen, and must not end with a hyphen. However, a subsequent specification (RFC 1123) permitted hostname labels to start with digits. No other symbols, punctuation characters, or white space are permitted.

Could someone please help.

Thanks
Rahul
# 2  
Old 06-02-2016
Quote:
Originally Posted by rahul2662
Hello,

I am trying to develop a script to check for valid hostnames. Below are the prerequisites for a valid hostname which I got from wiki :

Hostnames are composed of series of labels concatenated with dots, as are all domain names. For example, "en.wikipedia.org" is a hostname. Each label must be between 1 and 63 characters long, and the entire hostname (including the delimiting dots but not a trailing dot) has a maximum of 253 ASCII characters.

The Internet standards (Requests for Comments) for protocols mandate that component hostname labels may contain only the ASCII letters 'a' through 'z' (in a case-insensitive manner), the digits '0' through '9', and the hyphen ('-'). The original specification of hostnames in RFC 952, mandated that labels could not start with a digit or with a hyphen, and must not end with a hyphen. However, a subsequent specification (RFC 1123) permitted hostname labels to start with digits. No other symbols, punctuation characters, or white space are permitted.

Could someone please help.

Thanks
Rahul
Hello Rahul,

Good to see you in forums Smilie. Regarding above query I would like to say it completely depends on the organization like they are really following the mentioned rules/guidelines or not. Why not do a ping test for each host and find out(means--> if a host is valid it will pass the test), please do let me know your thoughts on same.

Thanks,
R. Singh
# 3  
Old 06-02-2016
Hello Ravinder,

Thanks for your reply. Smilie I am actually developing a script to configure hostname. The script which will ask the user for the hostname. It will check for all the conditions mentioned in my previous mail and validate if the user has entered hostname as per the correct naming convention. If the user has not adhered to the valid hostname convention then script will echo error else script will proceed and make changes specific to the hostname in the RedHat configuration files.

Thanks
Rahul

Last edited by rahul2662; 06-02-2016 at 06:42 AM..
# 4  
Old 06-02-2016
Quote:
Originally Posted by rahul2662
Hello Ravinder,
Thanks for your reply. Smilie I am actually developing a script to configure hostname. The script which will ask the user for the hostname. It will check for all the conditions mentioned in my previous mail and validate if the user has entered hostname as per the correct naming convention. If the user has not adhered to the valid hostname convention then script will echo error else script will proceed and make changes specific to the hostname in the RedHat configuration files.
Thanks
Rahul
Hello rahul2662,

Could you please try following and let me know if this helps and covers your conditions, though I haven't tested it.
Code:
 echo "Please enter the name:"
read NAME
ONE=1
 VALUE=`echo $NAME | awk -F"." '{if(length($0)<=63 && NF==3 && $0 !~ /\.$/ && $0 !~ /^[[:digit:]]/ && $0 !~ /^-/ && $0 !~ /-$/ && $0 !~ /[[:space:]]/ && $0 !~ /[[:punct:]]/ && $0 !~ /[[:cntrl:]]/){print 1}}'`
 if [[ $VALUE == $ONE ]]
then
        echo "You have entered correct name as " $NAME
else
        echo "Please enter correct name as it is matching the standards."
fi

It is taking care of following conditions too.
i- length should be 63 char long.
ii- 3 dots.
iii- NO trailing dot at last of variable.
iv- should not start with a digit and hyphen, should not end with hyphen too.
v- no other symbols, punctuations, white spaces.
Could you please more elaborate about following point.
Quote:
has a maximum of 253 ASCII characters.
Thanks,
R. Singh

Last edited by RavinderSingh13; 06-02-2016 at 07:48 AM..
# 5  
Old 06-02-2016
Hello Ravinder,

Below conditions should be satisfied for a valid hostname :

1) Maximum 3 dots ( can have zero dots as well)
2) maximum 63 characters within each dot

for example below should be a valid hostname

]$ksh hostnamecheck.ksh
Please enter the name:
newhost
Please enter correct name as it is matching the standards.

Thanks
Rahul
# 6  
Old 06-02-2016
Hello rahul2662,

Could you please try following and let me know how it goes then.
Code:
echo "Please enter the name:"
read NAME
ONE=1
VALUE=`echo $NAME | awk '{if(length($0)<=253 && $0 !~ /\.$/ && $0 !~ /^[[:digit:]]/ && $0 !~ /^-/ && $0 !~ /-$/ && $0 !~ /[[:space:]]/ && $0 !~ /[[:punct:]]/ && $0 !~ /[[:cntrl:]]/){num=split($0, A,".");if(num==1){print 1}} else {for(i=1;i<=num;i++){if(length(A[i])<=63){k++}}};if(k==num){print 1}}'`
if [[ $VALUE == $ONE ]]
then
        echo "You have entered correct name as " $NAME
else
        echo "Please enter correct name as it is NOT matching the standards."
fi

You should check all permutations and combinations for above example too. Please let me know if you have any queries on same.

EDIT: I have added logic to check 0 occurrences for DOT too now in above code. Adding a more clarifying one liner form of awkwithin script.
Code:
echo "Please enter the name:"
read NAME
ONE=1
VALUE=`echo $NAME | awk '{
                                if(length($0)<=253 && $0 !~ /\.$/ && $0 !~ /^[[:digit:]]/ && $0 !~ /^-/ && $0 !~ /-$/ && $0 !~ /[[:space:]]/ && $0 !~ /[[:punct:]]/ && $0 !~ /[[:cntrl:]]/)      {
                                num=split($0, A,".");
                                if(num==1){
                                                print 1
                                          }
                                else      {
                                                for(i=1;i<=num;i++){
                                                                        if(length(A[i])<=63){
                                                                                                k++
                                                                                          }
                                                                   };
                                                                   }
                                          }
                                if(k==num){
                                                print 1
                                          }
                          }
                        '`
if [[ $VALUE == $ONE ]]
then
        echo "You have entered correct name as " $NAME
else
        echo "Please enter corect name as it is NOT matching the standards."
fi

Thanks,
R. Singh

Last edited by RavinderSingh13; 06-02-2016 at 08:45 AM.. Reason: Added logic for 0 occurances of DOT in user input now too.
# 7  
Old 06-02-2016
How about (pure recent bash):

Code:
read NAME
IFS="."; NARR=($NAME)
ST=1
for i in ${!NARR[@]}; do [ ${#NARR[$i]} -gt 63 ] && { ST=0; break; }; done
[ ${#NAME} -le 253 ] && [ ${#NARR[@]} -le 4 ] && [[ ! "$NAME" =~ [^[:alnum:].-] ]] && [ $ST -eq 1 ] && echo good || echo bad

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check if time format is valid

How can I validate if time (HH:MM:SS) argument is valid? I got this from web but I can't modify it to exit the script if the time argument is invalid. echo $1 | awk -F ':' '{ print ($1 <= 23 && $2 <= 59 && $3 <= 59) ? "good" : "bad" }' ex: ./script.ksh 12:34:21 = okay ./script.ksh... (10 Replies)
Discussion started by: erin00
10 Replies

2. Shell Programming and Scripting

Check if a string is a valid timestamp in UNIX.

Hi all, I have date and time value in a string, I want to check if it is a valid date and time. Need help on this. Thanks (7 Replies)
Discussion started by: Pratiksha Mehra
7 Replies

3. Shell Programming and Scripting

Need a script to check if an argument is valid shell variable

I need a script that should print 'yes' if the argument is a valid shell variable name else 'No' if it is not a valid shell variable. A valid one begins with an alphabet or percentage (%) character and is followed by zero or more alphanumberic or percentage (%) characters. For example: $... (6 Replies)
Discussion started by: pingiliarjun
6 Replies

4. Shell Programming and Scripting

How to check the user input to be valid using shell script?

How to check the user input to be valid using shell script? The valid input is in the format like as follows. 1. It can only have r,w,x or a hyphen and nothing else. 2. ensure the r, w, x are in the correct order. for example: rwxr-xr-x is a valid format. Thanks (5 Replies)
Discussion started by: hyeewang
5 Replies

5. UNIX for Dummies Questions & Answers

How to check if file contains valid strings?

Hi All, I am a newbie...I would like to have a function which ll check if a file contains valid strings before "=" operator. Just to give you my requirement: assume my file has content: hello= gsdgsd sfdsg sgdsg sgdgdg world= gggg hhhh iiiii xxxx= pppp ppppp pppp my... (5 Replies)
Discussion started by: rtagarra
5 Replies

6. Shell Programming and Scripting

how to check for valid password

I need to check if an account has a valid password. Would something like this work? read ACCNAME if grep -q "^$ACCNAME:\$6:" /etc/shadow; thenI noticed every entry in my shadow file that has a password starts with $6 ... it works for my current setup, but would it always work? I can't test... (4 Replies)
Discussion started by: ADay2Long
4 Replies

7. Homework & Coursework Questions

Bash shell - Check if value is valid directory.

1. The problem statement, all variables and given/known data: The script usage will be as follows: library.third source_directory - Your script will display an appropriate error message and exit with status 3 if no parameters are given - Your script will display an appropriate error... (2 Replies)
Discussion started by: netmaster
2 Replies

8. Shell Programming and Scripting

to check whether a directory or filename path is valid or not

the script on excution should take a directory path from useran a numric input and it should check indicate whether its write or not? if the cmmd sh<script-name>,dir/path.<500>" is greater than 500 in size should be copied to dir ,temp in pwd and display the mesage'files of 2000 bytes hav been... (4 Replies)
Discussion started by: arukr
4 Replies

9. Shell Programming and Scripting

Check valid records in really big file with one commend..

Hi, I have a 5 gig file, no record terminators, field terminators are newline. The record length is 768 and I would like to check that every 768th byte is a newline and print out the byte position if it isn't. I would like to do this going either forward or backwards with one command if... (3 Replies)
Discussion started by: vtischuk@yahoo.
3 Replies

10. Shell Programming and Scripting

How to check for a valid numeric input

Hi Folks, I'm using bash script. I would like to check whether input is a number or not.(Only positive numbers).. if space or non numeric is entered, it should say "invalid input". pls help.. thanks in adv. Br/// Vijay. (1 Reply)
Discussion started by: Vijayakumarpc
1 Replies
Login or Register to Ask a Question