FQDN into domain name and hostname


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting FQDN into domain name and hostname
# 1  
Old 10-08-2010
FQDN into domain name and hostname

I'm working on a rather large script atm (it already takes 9 arguments). As such, I need to obtain both a server name and domain from the FQDN. From this, I want to both populate the hostname of the server, as well as the domain line in the /etc/resolv.conf file.

Obviously, this isn't working. I know the regex should be correct.
Here are the regex's I'm working with to obtain the hostname and domain portions:
Code:
Domain: /(?<=\.)[[:graph:]]*/
Hostname: /[[:alnum:]]*(?=\.)/

Can anyone recommend an implementation using either sed or perl? Once I have them output to stdout I can cast them into variables, or even directly into the files. I'd appreciate any help anyone can offer. Thanks!
# 2  
Old 10-08-2010
how about the
Code:
domainname

command ?
# 3  
Old 10-08-2010
Quote:
Originally Posted by ctsgnb
how about the
Code:
domainname

command ?
The script is being used immediately following automated system provisioning, so that returns (none).

Basically the script takes in the following as inputs:
<bond0ip> <bond0snm> <bond0network> <eth1ip> <eth1snm> <eth1network> <FQDN> <DGW> <nameserver>

From this it will perform a ton of different actions. <FQDN> had originally just been hostname, however there is now a vendor requirement that we include a domain line in our resolv.conf file, so I have to make it into a FQDN and extract both from the one item so I can write them into the appropriate config files. Hope that clears stuff up a bit.

The easiest way would be if I could put in host name and domain separately, but as far as I know you can't use more than 9 arguments in a shell script (at least I've always had issues since $10 is viewed as $1 plus a 0).

Last edited by msarro; 10-08-2010 at 02:54 PM..
# 4  
Old 10-08-2010
Quote:
Originally Posted by msarro
The easiest way would be if I could put in host name and domain separately, but as far as I know you can't use more than 9 arguments in a shell script (at least I've always had issues since $10 is viewed as $1 plus a 0).
Code:
# cat script
#!/bin/sh
for i in ${@};do echo ${i};done

# /temp/script 1 2 3 4 5 6 7 8 9 10 11
1
2
3
4
5
6
7
8
9
10
11

This User Gave Thanks to danmero For This Post:
# 5  
Old 10-08-2010
you probably can't get much help without posting your script or at least relevant portions of it... nobody really wants to guess how you're trying to do everthing.

the way you're doing your hostname and domain seems a lot more complicated then it needs to be.

if, for example, you input your FQDN and assign it to a varialbe called FQDN
Code:
HOSTNAME=`echo $FQDN |awk -F. '{ print $1 }'`
DOMAIN=`echo $FQDN |awk -F. '{$1="";OFS="." ; print $0}' | sed 's/^.//' `

a few approachs come to mind, for getting variables input for your script.
1. you could use read statements to get all your paramaters, this would let you have as many input varbs as you would ever need.
2. you could put all your paramaters into a seperate file, then simply source that.

parm file contents:
host=somehostname
domain=something.com
bond0ip=whatever
(etc)...
This User Gave Thanks to denn For This Post:
# 6  
Old 10-08-2010
Quote:
Originally Posted by denn
you probably can't get much help without posting your script or at least relevant portions of it... nobody really wants to guess how you're trying to do everthing.

the way you're doing your hostname and domain seems a lot more complicated then it needs to be.

if, for example, you input your FQDN and assign it to a varialbe called FQDN
Code:
HOSTNAME=`echo $FQDN |awk -F. '{ print $1 }'`
DOMAIN=`echo $FQDN |awk -F. '{$1="";OFS="." ; print $0}' | sed 's/^.//' `

a few approachs come to mind, for getting variables input for your script.
1. you could use read statements to get all your paramaters, this would let you have as many input varbs as you would ever need.
2. you could put all your paramaters into a seperate file, then simply source that.

parm file contents:
host=somehostname
domain=something.com
bond0ip=whatever
(etc)...
I used the method you showed above, thank you!
FYI here are the applicable parts:
Code:
#Format:
#network.sh [bond0ip] [bond0snm] [bond0network] [eth1ip] [eth1snm] [eth1network] [FQDN] [DGW] [nameserver]
#Arguments are not optional.

#Split FQDN into hostname and domain
HOSTNAME=`echo $7 |awk -F. '{ print $1 }'`
DOMAIN=`echo $7 |awk -F. '{$1="";OFS="." ; print $0}' | sed 's/^.//' `

Code:
#Configure DGW and Hostname
sed -i '/HOSTNAME/ d' /etc/sysconfig/network
echo HOSTNAME=$HOSTNAME >> /etc/sysconfig/network
echo GATEWAY=$8 >> /etc/sysconfig/network

#Configure DNS Server
echo domain $DOMAIN > /etc/resolv.conf
echo nameserver $9 >> /etc/resolv.conf
echo options timeout:1 attempts:2 >> /etc/resolv.conf
echo hosts:    files    dns >> /etc/nsswitch.conf

Many thanks for your help.
# 7  
Old 10-10-2010
awk? Hmmm... I suppose. If portability is the goal (no bash, for example), then use expr or cut or even sed instead (IMHO).

Code:
#Split FQDN into hostname and domain
HOSTNAME=`expr "$7" : '\([^.][^.]*\)\..*'`
DOMAIN=`expr "$7" : '[^.][^.]*\.\(.*\)'`

Or use cut...

Code:
HOSTNAME=`echo "$7" | cut -f1 -d.`
DOMAIN=`expr "$7" | cut -f2- -d.`

or even sed... you're using sed to doctor things up anyhow...

Code:
HOSTNAME=`echo "$7" | sed  's/\..*//'`
DOMAIN=`expr "$7" | sed -n 's/[^.]*\.//p'`

Just my opinion... awk would be my last choice for this.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Named resolving old fqdn on AIX after change to new fqdn.

Hey All, We have defined abc.this.that in: /etc/named.conf and corresponding files but after the change that we verified through dig -x this.that ptr all is resolving correctly. However in the /var/log/named/named.log file we still see entries for: 4-May-2015 12:15:30.390 queries:... (6 Replies)
Discussion started by: Devyn
6 Replies

2. UNIX for Dummies Questions & Answers

Postfix Configuration:Hostname/Domain Name Doubts

I have a RHEL server where I want to create a mail server so I can send myself alerts whenever X process have problems. Disclaimer: Im a programmer thats been forced to do IT. So I'm trying my best here. The problem: While configuring Postfix it asks for $myhostname which apparently must be... (2 Replies)
Discussion started by: RedSpyder
2 Replies

3. UNIX for Dummies Questions & Answers

nLocal sendmail issues when hostname not set in official domain name

Hi, I'm having issues with sendmail when I try to send host to host mail. I've had to change the "my official domain name" line to mycompany.com to get the mails through the external spam filter, when a mail was sent with hostname.mycompany.com it was blocked. I had to change the Dj macro... (0 Replies)
Discussion started by: elcounto
0 Replies

4. Emergency UNIX and Linux Support

HP UX - ILO Console hostname different than Machine Hostname...

Hi All, So we added a new HP-UX 11.31 machine. Copied OS via Ignite-UX (DVD)over from this machine called machine_a. It was supposed to be named machine_c. And it is when you log in...however when I'm in the ILO console before logging in, it says: It should say: What gives? And how do... (4 Replies)
Discussion started by: zixzix01
4 Replies

5. Shell Programming and Scripting

how to get the FQDN

Suppose I am in one server A .I want to know the FQDN of another host B then how can I get the FQDN of that host B from host A. (1 Reply)
Discussion started by: maitree
1 Replies

6. UNIX for Dummies Questions & Answers

My domain name as my IRC hostname?

When I connect to any IRC server, it's usually my ISP IP address/hostname. I own a domain, but I'm not using it for anything (no web hosting service or server). Is it possible for me to use my domain as my IRC hostname instead of my regular ISP hostname? (0 Replies)
Discussion started by: guitarscn
0 Replies

7. Windows & DOS: Issues & Discussions

How to: Linux BOX in Windows Domain (w/out joining the domain)

Dear Expert, i have linux box that is running in the windows domain, BUT did not being a member of the domain. as I am not the System Administrator so I have no control on the server in the network, such as modify dns entry , add the linux box in AD and domain record and so on that relevant. ... (2 Replies)
Discussion started by: regmaster
2 Replies

8. Solaris

List of Hostname under NIS Domain

How do I find a list of hosts under a domainname on a NIS+ I did check nisls command , I could not find any ??? (5 Replies)
Discussion started by: sriram003
5 Replies

9. UNIX for Dummies Questions & Answers

Solaris - unknown hostname - how can I change hostname?

Hello, I am new to Solaris. I am using stand alone Solaris 10.0 for test/study purpose and connecting to internet via an ADSL modem which has DHCP server. My Solaris is working on VMWare within winXP. My WinXP and Solaris connects to internet by the same ADSL modem via its DHCP at the same... (1 Reply)
Discussion started by: XNOR
1 Replies

10. Programming

FQDN and getdomainname

I have a need to create a connection between an erlang node and my C program. the name of an erlang node looks something like monitor@host1.ipc.co.za. The piece of code I have to construct a node name looks like this: char *hostname, *domainname, *nodename = "monitor", *thisfullnodename; ... (1 Reply)
Discussion started by: NanoSec
1 Replies
Login or Register to Ask a Question