Shell远程巡检交互式脚本

作者: admin 分类: Linux,Shell脚本 发布时间: 2022-12-14 13:41 浏览:2,162 次    
#!/bin/bash
for ip in `cat ip.txt`
do
echo "----------<主机${ip}系统资源使用率>----------"
ssh -T root@$ip << \EOF
printf "CPU\t\tMEM\t\tDisk\n"
#硬盘使用率
disk_use=`df -h | awk '$NF=="/"{printf "%s\t\t\n",$5}'`
#内存使用率
mem=$(free -m | awk 'NR==2{printf "%.2f%\t\t\n",$3/$2*100}')
#CPU负载率
cpu=$(uptime | awk '{printf "%.2f%\t\t\n",$(NF)*100}')
echo "$mem$disk_use$cpu"
exit
EOF
done
  • :建议执行前做ssh免密或修改相关参数
  • Shell脚本远程执行提示“Pseudo-terminal will not be allocated because stdin is not a terminal”
%title插图%num

原因:以上提示是因为ssh远程执行时不会分配伪终端,因为stdin不是终端

解决方法:

ssh -T user@serverip <\EOF或ssh user@serverip /bin/bash <'EOF'


温馨提示:如无特殊说明,本站文章均为作者原创,转载时请注明出处及相应链接!

发表评论