SHELL脚本:

基于shell实现不同服务器root密码的统一

遇到的问题:

发现新密码,还是旧密码如果含有一些特殊符号,如"}","{"等,使用expect的话就会出现问题,我们需要对这种情况对特殊符号进行转义。

#!/bin/bash
#多个服务器修改密码的脚本
file=./10-bt.txt
new_passwd='12\}2231321'
while read ip passwd
do
echo $passwd
       expect<<-EOF 
       spawn ssh $ip
       expect {
              "(yes/no)?" {send "yes\r";exp_continue}
              "password:" {send "$passwd\r";}
       }
       expect "#" {send "echo $new_passwd | passwd --stdin root\r "}
       expect "#" {send "exit\r"}
       expect eof
EOF
done<$file
存在问题:旧密码如果含有一些特殊符号,如"}","{"等,该脚本就会出现问题

[root@ecs-shell shell]# cat 10-bt.txt 
192.168.1.185 123456
192.168.1.224 123456
#优化
#!/bin/bash
#这里的密码如果有特殊符号,需要转义。
new_passwd="\(\)123123"
#定义ip数组
declare -a ip_array
#定义passwd数组
declare -a passwd_array
while read ip passwd
do
ip_array+=("$ip")
passwd_array+=("$passwd")
done<passwd.txt
#获取ip和密码的总数
sum=$(cat passwd.txt | wc -l)
let sum=sum-1
for i in $(seq 0 1 $sum)
do
echo "${ip_array[i]}"
sshpass -p ${passwd_array[$i]} ssh root@${ip_array[$i]} "echo -n $new_passwd | passwd --stdin root" -o StrictHostChecking=no
done


基于shell实现元宝消耗或者获得的总数

#!/bin/bash
id=131072022
date_log=*11-0[6-9]*
fri_date=2023-11-09
file="./yuanbaoxiaohao.txt"
cat $date_log | grep $id | grep gold | grep -v "$fri_date 2[2-3]:"  | grep "|[0-9]\{4\}$" | awk -F"|" '{print $12-$13 }' > $file

# 设置初始总和为0
sum=0
# 逐行读取文档,将每行的数相加
while read line
do
  sum=$((sum + line))
done < $file  # 替换成你的输入文件名
# 打印总和
echo "总和为: $sum"

基于shell查询各个渠道服上的node进程状态

注意点:当执行时间过长时,我们需要去nohup sh test_node.sh & 将其挂在后台执行

#!/bin/bash
while read ip 
do
expect <<-EOF 
spawn ssh $ip
expect  { 
	"$" {
        send "ps -ef | grep node | wc -l\r"
	send "exit\r"
}
}
expect eof
EOF
done < ip.txt

#!/bin/bash
file=/data/script/ip.txt
#定义ip数组
declare -a ip_array
#定义游戏服区号数组
declare -a server_id_array
while read server_id ip
do
ip_array+=("$ip")
server_id_array+=("$server_id")
done<$file
#获取ip和区号的总数
sum=$(cat $file | wc -l)
let sum=sum-1
for i in $(seq 0 1 $sum)
do
echo "游戏服${server_id_array[i]}的node状态为"
ssh  sgsm@${ip_array[i]} "ps -ef | grep node  "    
done

基于shell实现磁盘的清理(删除到只剩磁盘空间剩余70%,mongo备份文件的清理)

#!/bin/bash
limit_disk=70

#获取当前磁盘利用率(若有/data/backups/mongo,则获取/data/backups/mongo利用率,否则获取/目录的利用率)
get_disk() { 
df -h | grep -w "/data/backups/mongo"
if [ $? -eq 0 ]; then
disk=$(df -h | grep /data/backups/mongo | awk -F" " '{print $5}' | awk -F "%" '{print $1}')  
else
disk=$(df -h | grep -w "/" | awk -F" " '{print $5}' | awk -F "%" '{print $1}')
fi
echo "当前磁盘空间为$disk"
}
get_disk
#存放要删除文件的文件夹
deletefile=/data/script/deletefile.txt
deletefiledir=/data/script/deletedir
#存放mongo备份的目录
mongodir=/data/backups/mongo/

[ ! -d $deletefiledir ]&&mkdir $deletefiledir

#获取当前/data/backups/mongo的最早的文件产生日期
old_file_time=$( ls -lt /data/backups/mongo | grep -E .tar.gz$ |awk -F" " '{print $NF}' | tail -1 | awk -F"-"  'BEGIN { OFS="-" } {print $3,$4,$5}')
#将这个时间转化为时间戳
old_file_timestamp=$(date -d "$old_file_time" +%s)
#获取现在的时间戳
now_timestamp=$(date +%s)
#获取两个时间戳相差的天数
day_diff=$(( ($now_timestamp - $old_file_timestamp) / 86400 ))
echo "当前最早的备份是$day_diff天前的备份"
let day_diff=day_diff-1

#循环删除
while [ $disk -gt $limit_disk ]&&[ $day_diff -gt 7 ]
do
find $mongodir -ctime +$day_diff > $deletefile
#存储被删除的文件
find $mongodir -ctime +$day_diff >> $deletefiledir/file.txt
while read file
do
rm $file
done < /data/script/deletefile.txt
let day_diff=day_diff-1
get_disk
done

echo "磁盘清理已完成"
#获取在我们删除过文件后的disk值
get_disk

基于shell向局域网其他主机推送普通用户公钥

#!/bin/bash
user=jerry
pub_key=/home/$user/.ssh/id_rsa.pub
key=/home/$user/.ssh/id_rsa
[ -f $key ]&&rm $key
[ -f $pub_key ]&&rm $pub_key
#非交互式生成密钥对
ssh-keygen -P '' -f ~/.ssh/id_rsa
#从文件中拿取ip,密码
while read ip passwd
do
echo $ip
echo $passwd 
expect<<-EOF
                spawn ssh-copy-id $user@$ip
                expect {
                        "yes/no" { send "yes\r";exp_continue }
                        "password:" { send "$passwd\r" }
                }  
expect eof   
EOF
done<11-bt.txt

cat 11-bt.txt
192.168.1.185 jhhjhg
192.168.1.224 jhhjhg

基于shell实现磁盘清理(删除某个以时间命名文件夹)只能去处理以日期格式命名的文件夹

#!/bin/bash
delete_dir=/data/backups/mongo
day=6
if [ $? -eq 0 ]; then
disk=$(df -h | grep /data/backups/mongo | awk -F" " '{print $5}' | awk -F "%" '{print $1}')  #获取当前占用磁盘空间大小
else
disk=$(df -h | grep -w "/" | awk -F" " '{print $5}' | awk -F "%" '{print $1}')
fi 
if [ $disk -gt 70 ];
then
       find $delete_dir -type d | grep $(echo $(date -d "$day days ago " +%Y-%m-%d )) |  xargs  rm -r >> /dev/null
fi

检测ansible主机清单的主机数和分组数是否一致的脚本

#第一版:定义ansible变量来获取某个分组中的主机数量。
#打印该分组的主机数的ansible-playbook剧本
---
- name: test node count
  hosts: "{{ansible_hosts}}"
  vars:
    HOST_COUNT: "{{ groups[ansible_hosts] | length }}"
  tasks:
    - name: print count
      debug:
        var: HOST_COUNT

#!/bin/bash
#定义分组名称
little_group=game
#获取某个分组中的主机数
declare -i ansible_game_count=$(ansible-playbook -i hosts/hosts host_count.yaml --extra-vars "ansible_hosts=$little_group" | grep '"HOST_COUNT":' | awk -F"\"" '{print $4}' | sort -u | tail -1) 
#获取主机清单里该分组中主机的数量
declare -i host_num=$(cat hosts/hosts  | grep "sgsm_video_ip=" | wc -l) 
if [ $ansible_game_count -eq $host_num ]; then
echo -e "主机数量和game分组主机数量一致"
echo -e "数量都为$host_num"
else
echo -e "hosts中主机数量和game中不一致"
echo -e "hosts中主机数量为$host_num"
echo -e "$little_group分组中主机数量为$ansible_game_count"
fi

#执行过程中,发现问题:使用该脚本,如果game分组中主机数量过多,那么该脚本就会很慢,因为分组中每个主机都需要去执行这个playbook。

#优化使用jq工具获取。

#!/bin/bash
#安装所需的软件。
little_group=game
#检测jq是否安装
rpm -q jq.x86_64 >> /dev/null
if [ $? -ne 0 ]; then
read -p "我们需要安装jq工具,请输入y进行安装:"  yn
if [ $yn == "y" ]; then
#在服务器上获取用户密码
passwd_file=$( find ~/ -name all -type f )
passwd=$(cat $passwd_file | grep "sgsm_password" | awk -F":" '{print $2}'| tr "\"" " " | head -1)
echo "passwd_file:$passwd_file,passwd:$passwd"
echo $passwd |sudo -S yum install -y jq.x86_64
else 
exit
fi
fi

#进行数目的比对
#declare -i ansible_game_count=$(ansible-playbook -i hosts/hosts host_count.yaml --extra-vars "ansible_hosts=$little_group" | grep '"HOST_COUNT":' | awk -F"\"" '{print $4}' | sort -u | tail -1) 
declare -i ansible_game_count=$(ansible-inventory -i hosts/hosts  --list | jq -r '.game.hosts | length ')
declare -i host_num=$(cat hosts/hosts  | grep "sgsm_video_ip=" | wc -l) 
if [ $ansible_game_count -eq $host_num ]; then
echo -e "主机数量和game分组主机数量一致"
echo -e "数量都为$host_num"
else
echo -e "hosts中主机数量和game中不一致"
echo -e "hosts中主机数量为$host_num"
echo -e "$little_group分组中主机数量为$ansible_game_count"
fi

这样就能高效的执行该脚本完成功能。

 ansible-inventory -i ~/hosts/hosts --list | jq -r '.game.hosts | length'  #获取某个分组的主机数量
posted on 2024-02-28 15:11  Insomnia_ki_higher  阅读(4)  评论(0编辑  收藏  举报