I'm trying to use the below script to perform a certain task.
task is: i have a file that has data that is similar to what is pasted below.
we'll call this file host.conf
define host {
address 10.11.11.14
alias mail-server
host_name mail-server01.sky.net
use generic-host
}
What I just pasted above is what i refer to as a chunk. Now, there are several of these chunks in the host.conf file.
now, i have a separate file that contains a list of host names. we'll call this separate file host.list.
the list of hostnames in host.list is one server per line.
example:
mail-server01.sky.net
mail-server02.sky.net
mail-server03.sky.net
i want to take each host that is in host.list, and run it against the host.conf file. what i'm looking to do is to get the IP of the host from the host.conf file.
in other words, every host that I have in host.list has a configuration set up in host.conf. the configuration set up for each host is the chunk i pasted above.
below is the code im attempting to use to help me pull out what i want.
awk 'BEGIN {
while((getline < "host.list")>0)
S[$0]
FS="\n"; RS="}\n"
}
/define host/ && /}/ {
for(X in D) delete D[X];
for(N=2; N<=NF; N++)
{
split($N, A, " ");
D[A[1]] = A[2]
i = 3;
while (i in A)
D[A[1]] = D[A[1]] "=" A[i++];
}
if (D["host_name"] in S)
printf("%20s -- %50s\n", D["host_name"], D["address"])
}' $1
the output of this script should be:
hostname ---- ip
ex:
mail-server01.sky.net ------ 10.11.11.14
my os is: redhat and sunos
shell: bash