需求:查看多臺機器看多個系統CPU,指定的進程CPU,主備機狀態,內存使用狀況;並在一臺機器上顯示;bash
第一:先設置ssh免密碼登陸ssh
ssh-keygen -t rsa P'' -f ~/.ssh/id_rsa 不提示直接生成祕鑰
ssh-copy-id -i ~/.ssh/id_rsa.pub root@ipide
第二:查詢腳本,分別拷貝到各個機器上code
scp /usr/local/src/cpu_men.sh root@ip: /usr/local/src/
腳本內容
#!/bin/bash
cpu=top -bn1|awk -F "[,:%]" '/Cpu/{print $2}'
men_total=free -m | grep Mem|awk '{print $2}'
men_used=free -m | grep Mem|awk '{print $3}'
men_av=awk 'BEGIN{printf"%.0f\n",('${men_used}'/'${men_total}')*100}'
num=ifconfig|awk '/eth.:./{print $1}'|wc -l
syseth="主"
[ ${num} -eq 0 ] && syseth="備"
IP=ifconfig|awk -F"[: ]+" 'NR==2{print $4}'
pronum=ps aux|grep tdci|grep -Ev "$$|grep"|awk '{print $3}'|wc -l
procpu=ps aux|grep tdci|grep -Ev "$$|grep"|awk '{print $3}'|xargs
#awk 'BEGIN{printf"%-20s%-10s%-10s%-10s%-20s\n","IP------------------","status---------","CPU%------","men%------","procpu%-----"}'
awk 'BEGIN{printf"%-20s%-13s%-12s%-10s","'${IP}'","'$syseth'","'"${cpu}"'",'"${men_av}"'}'
echo "$procpu (${pronum} "tdic")"進程
第三:整體查詢並在一臺主機顯示腳本:ssh_exec.ship
#!/bin/bash
awk 'BEGIN{printf"%-20s%-10s%-10s%-10s%-20s\n","IP------------------","status---------","CPU%------","men%------","procpu%-----"}'br/>arry=(
192.168.50.5
192.168.50.6
192.168.50.9
)
for((i=0;i<${#arry[@]};i++))
do
ssh root@${arry[i]} "/bin/bash /usr/local/src/cpu_men.sh"
done
/bin/bash /usr/local/src/cpu_men.sh內存
執行結果ci
> [root@INTSVR-B src]# sh ssh_exec.sh > IP------------------status---------CPU%------men%------procpu%----- > 192.168.50.5 備 3.6 10 1.3 0.5 (2 tdic) > 192.168.50.6 主 8.4 12 24.8 2.4 (2 tdic) > 192.168.50.9 主 4.5 30 0.7 (1 tdic) > 192.168.5010 備 1.4 10 (0 tdic) > [root@INTSVR-B src]#