作业
## 优化巡检脚本
--- 机器:web01 web02 nfs backup ----
# 每台机器内存信息:
总内存:
可用内存:
已使用内存:
swap总大小:
swap可用:
swap已使用:
# 每台机器磁盘信息
/分区剩余:
/分区总共:
/分区已使用:
# 机器负载
1分钟:
5分钟:
15分钟
### no.1 ### 先编辑推送密钥文件
#!/bin/bash
test -f ~/.ssh/id_dsa || ssh-keygen -t rsa -P '' -f ~/.ssh/id_dsa >/dev/null 2>&1
for ip in 7 8 31 41;do
ping -c1 -W1 172.16.1.$ip &>/dev/null
[ $? -eq 0 ] &&\
sshpass -p 1 ssh-copy-id -o StrictHostKeyChecking=no -i ~/.ssh/id_dsa.pub [email protected].$ip &>/dev/null &&\
echo "172.16.1.$ip 推送成功..." ||\
echo "172.16.1.$ip 推送失败..."
done
### no.2 ### 编辑巡检脚本
[root@tb ~]# vi xj.sh
swap_total=`free -m|awk '/Swap/{print $2}'`
swap_ava=`free -m|awk '/Swap/{print $NF}'`
swap_used=`free -m|awk '/Swap/{print $3}'`
## disk var
disk_total=`df -h|awk '/\/$/{print $2}'`
disk_ava=`df -h|awk '/\/$/{print $4}'`
disk_used=`df -h|awk '/\/$/{print $3}'`
## sys var
avg_one=`uptime|sed -nr 's#.*:(.*),(.*),(.*)#\1#gp'`
avg_five=`uptime|sed -nr 's#.*:(.*),(.*),(.*)#\2#gp'`
avg_fifteen=`uptime|sed -nr 's#.*:(.*),(.*),(.*)#\3#gp'`
for ip in 7 8 31 41;do
hname=`ssh 172.16.1.$ip hostname`
echo -------- $hname --------
ssh 172.16.1.$ip "echo $hname 总内存: $mem_total"
ssh 172.16.1.$ip "echo $hname 总内存: $mem_ava"
ssh 172.16.1.$ip "echo $hname 总内存: $mem_used"
ssh 172.16.1.$ip "echo $hname 总内存: $swap_total"
ssh 172.16.1.$ip "echo $hname 总内存: $swap_ava"
ssh 172.16.1.$ip "echo $hname 总内存: $swap_used"
ssh 172.16.1.$ip "echo $hname 总内存: $disk_total"
ssh 172.16.1.$ip "echo $hname 总内存: $disk_total"
ssh 172.16.1.$ip "echo $hname 总内存: $disk_total"
ssh 172.16.1.$ip "echo $hname 总内存: $avg_one"
ssh 172.16.1.$ip "echo $hname 总内存: $avg_five"
ssh 172.16.1.$ip "echo $hname 总内存: $avg_fifteen"
done
2.跳板机
- 连接指定机器
- 远程执行命令
编写 4个表格
[root@tb ~]# cat *.txt
1.txt
+-------------------+
| 1.远程连接 |
+-------------------+
| 2.执行命令 |
+-------------------+
| 3.测试SSH |
+-------------------+
| |
+-------------------+
2.txt
+-------------------+
| 1.web01 - 7 |
+-------------------+
| 2.web01 - 8 |
+-------------------+
| 3.nfs - 31 |
+-------------------+
| 4.backup- 41 |
+-------------------+
3.txt
+-------------------+
| 瞎 重 |
+-------------------+
| 鸡 新 |
+-------------------+
| 儿 来 |
+-------------------+
| 输入 过吧 |
+-------------------+
4.txt
+-------------------+
| 1.看磁盘 |
+-------------------+
| 2.看内存 |
+-------------------+
| 3.看IP |
+-------------------+
| 4.看集贸 |
+-------------------+
| 其他命令请随便按 |
+-------------------+
[root@tb ~]# cat nxz.sh
#!/bin/bash
. /etc/init.d/functions
cat 1.txt
web01=172.16.1.7
web02=172.16.1.8
nfs=172.16.1.31
backup=172.16.1.41
read -p "你需要执行的是: " gg
if [ $gg -eq 1 ]; then
cat 2.txt
read -p "选择你要连接的机器: " aa
if [ $aa -eq 1 ]; then
ssh $web01
elif [ $aa -eq 2 ]; then
ssh $web02
elif [ $aa -eq 3 ]; then
ssh $nfs
elif [ $aa -eq 4 ]; then
ssh $backup
else
cat 3.txt
sh nxz.sh
fi
elif [ $gg -eq 2 ]; then
cat 2.txt
read -p "你需要执行的是: " bb
if [ $bb -eq 1 ]; then
target=$web01
elif [ $bb -eq 2 ]; then
target=$web01
elif [ $bb -eq 3 ]; then
target=$nfs
elif [ $bb -eq 4 ]; then
target=$backup
else
cat 3.txt
sh nxz.sh
fi
cat 4.txt
read -p "你需要执行的是: " cc
if [ $cc -eq 1 ]; then
ssh $target df -h
elif [ $cc -eq 2 ]; then
ssh $target free -h
elif [ $cc -eq 3 ]; then
ssh $target ip a
elif [ $cc -eq 4 ]; then
echo "集贸"
else
read -p "你需要执行的复杂命令" dd
ssh $target $dd
fi
elif [ $gg -eq 3 ];then
for ip in $web01 $web02 $nfs $backup;do
ping -c1 -W1 $ip &>/dev/null
[ $? -eq 0 ] && action "ssh $ip hostname" /bin/true || action "ssh $ip hostname" /bin/false
done
else
cat 3.txt
sh nxz.sh
fi