这个文章是转载的come from
如果你的hosts 不是很多,你可以手动添加,如果hosts很多的话,你不得不手动一个一个添加,这样很麻烦,今天在找问题的时候,看见这个shell脚本,很实用。
首先你应该有个模板文件。例如这个(和下面英文的重复了):
/tmp/cluster-nagios.skel
define host
{
host_name HOSTNAME
alias ALIAS
address IP
use generic-host
}
脚本如下:
#!/bin/bash
# Short script to generate nagios host entries
# Assumes you have a file called /tmp/hosts with format
# Hostname IP Fully qualified name
#
# Also assumes you have a /tmp/cluster-nagios.skel file that looks like this
# define host {
# host_name HOSTNAME
# alias ALIAS
# address IP
# use generic-host
# }
for x in `cat /tmp/hosts`
do
y=`echo $x | cut -d . -f1`
z=`host $x | awk '{print $3}'`
echo "Creating entry host_name = $x , alias = $y, IP = $z"
sed "s/HOSTNAME/$x/g" /tmp/cluster-nagios.skel | sed "s/IP/$z/g" | sed "s/ALIAS/$y/g" >> /tmp/cluster-nagios.cfg
done
阅读(393) | 评论(0) | 转发(0) |