Linux Shell高级编程几个问题总结

问题一:怎样通过域名或者Linuxhostname获取相应的服务器IP

比如:

域名为:sharebook.site

服务器的名称为:spark001

返回的结果为:192.168.12.167

 

参考脚本:

[root@spark001 others]# dig +short sharebook.site | grep -Eo '[0-9\.]{7,15}' | head -1

110.29.27.205

[root@spark001 others]# dig sharebook.site | awk '/^;; ANSWER SECTION:$/ { getline ; print $5 }'

110.29.27.205

[root@spark001 others]# ping -q -c 1 -t 1 spark001 | grep PING | sed -e "s/).*//" | sed -e "s/.*(//"

192.168.12.167

[root@spark001 others]# ping -q -c 1 -t 1 sharebook.site | grep PING | sed -e "s/).*//" | sed -e "s/.*(//"

192.168.12.167

 

实际应用:

比如,将Zookeeper的连接地址,

格式为:spark001:2821,spark002:2821,spark003:2821

转变成:192.168.12.167:2821, 192.168.12.168:2821, 192.168.12.169:2821

其中IP和hostname的映射关系为:

IP为:192.168.12.167   对应的hostname为:spark001

IP为:192.168.12.168   对应的hostname为:spark002

IP为:192.168.12.169   对应的hostname为:spark003

 

参考方法:

function kafka_hostname_url_to_ip_url(){

echo "Running the method of kafka_hostname_url_to_ip_url......."

echo "acquire Kafka hostname url"

#zookeeper.connect=spark003:2181,spark002:2181,spark001:2181

kafka_hostname_url_total=`grep 'zookeeper.connect' /home/spark/face.properties`

export kafka_hostname_url=`echo ${kafka_hostname_url_total#*=}

 

# spark003:2181,spark002:2181,spark001:2181

cho "${kafka_hostname_url}"

i=1

while((1==1))

do

split=`echo $kafka_hostname_url|cut -d "," -f$i`

if [ "$split" != "" ]

then

((i++))

single=`echo $split`

echo "${single}"

single_host=`echo ${single%%:*}`

echo "${single_host}"

single_ip=`ping -q -c 1 -t 1 $single_host | grep PING | sed -e "s/).*//" | sed -e "s/.*(//"`

echo "${single_ip}"

kafka_hostname_url=`echo ${kafka_hostname_url//$single_host/$single_ip}`

echo "${kafka_hostname_url}"

else

break

fi

done

}

 

问题二:怎样获取服务器的CPU个数。

linux_max_cores=`cat /proc/cpuinfo| grep "processor"| wc -l`

echo "The number cores of linux is ${linux_max_cores}"

 

问题三:用Shell脚本进行浮点数运算。

方法一:

application_single_node_cores=`expr ${linux_max_cores} \* 3 / 4`

echo " ${ application_single_node_cores }."

方法二:

application_single_node_cores=`gawk -v x=$linux_max_cores -v y=0.75 'BEGIN{printf "%.0f\n",x*y}'`

echo " ${ application_single_node_cores }."

 

问题四:怎样用Shell对带/的字符串进行替换

比如原来的字符串为:

application_path=#APPLICATION_PATH#

将#APPLICATION_PATH#用/home/application/param.properties进行替换

之后的结果为:

application_path=/home/application/param.properties

 

参考脚本:

application_path=${ APPLICATION_PATH }

application_path_change=`echo ${ application_path//\//\\\/}`

sed -i "s/#APPLICATION_PATH#/${application_path_change}/g" /home/timed-task

 

问题五:怎样获取Hadoop机器的服务器hostname

NODES=`cat /etc/hosts|grep -v -E 'localhost|mirrors'|awk '{print $2}'`

 

参考网址:

https://serverfault.com/questions/170706/easy-way-to-get-ip-address-from-hostname-using-a-unix-shell

http://compgroups.net/comp.unix.shell/how-to-get-the-hostname-from-an-unix-ip-addre/498965

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: