在我們進行自動發(fā)現(xiàn)的時候,總會有一個腳本來獲取端口等信息,并返回json格式,那以redis為例,下邊是redis進行進行自動發(fā)現(xiàn)的腳本
目前創(chuàng)新互聯(lián)公司已為近千家的企業(yè)提供了網(wǎng)站建設、域名、虛擬空間、網(wǎng)站托管運營、企業(yè)網(wǎng)站設計、安多網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
redisport=(`ps aux|grep -w redis-server|grep -v "grep" |awk -F':' '{print $NF}'`)
portnum=${#redisport[@]}
count=`hostname | grep "\-SQ" | wc -l`
if [ $count -eq 1 ];then
address=` /usr/sbin/ip address | grep -w "inet" | awk -F " " '{print $2}' | egrep "^10."| awk -F"/" '{print $1}'`
else
address=` /usr/sbin/ip address | grep "192.168" | awk '{print $2}' | awk -F "/" '{print $1}'`
fi
printf "{\n"
printf '\t'"\"data\":["
for ((i=0;i<$portnum;i++))
do
printf '\n\t\t{\n'
printf '\t\t\t'
printf "\"{#REDIS_PORT}\":\"${redisport[$i]}\"",
printf '\n\t\t\t'
printf "\"{#REDIS_ADDRESS}\":\"${address}"\",
printf '\n\t\t\t'
printf "\"{#REDIS_NAME}\":\"Redis ${address}:${redisport[$i]}\""
printf '\n\t\t}'
if [ $i -lt $[$portnum-1] ];then
printf ','
fi
done
printf "\n\t]\n"
printf "}\n"
返回結(jié)果如下
{
"data":[
{
"{#REDIS_PORT}":"6386",
"{#REDIS_ADDRESS}":"192.168.43.66",
"{#REDIS_NAME}":"Redis 192.168.43.66:6386"
},
{
"{#REDIS_PORT}":"6387",
"{#REDIS_ADDRESS}":"192.168.43.66",
"{#REDIS_NAME}":"Redis 192.168.43.66:6387"
},
{
"{#REDIS_PORT}":"6388",
"{#REDIS_ADDRESS}":"192.168.43.66",
"{#REDIS_NAME}":"Redis 192.168.43.66:6388"
},
{
"{#REDIS_PORT}":"6379",
"{#REDIS_ADDRESS}":"192.168.43.66",
"{#REDIS_NAME}":"Redis 192.168.43.66:6379"
},
{
"{#REDIS_PORT}":"6384",
"{#REDIS_ADDRESS}":"192.168.43.66",
"{#REDIS_NAME}":"Redis 192.168.43.66:6384"
},
{
"{#REDIS_PORT}":"6385",
"{#REDIS_ADDRESS}":"192.168.43.66",
"{#REDIS_NAME}":"Redis 192.168.43.66:6385"
}
]
}