相关业务SHELL脚本

发布时间 2023-12-27 17:22:33作者: Insomnia_ki_higher

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
time=$(date +%H:%m:%S)

limit_disk=70
#获取当前磁盘利用率(若有/data/backups/mongo,则获取/data/backups/mongo利用率,否则获取/目录的利用率)
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"
#存放要删除文件的文件夹
deletefile=/data/script/testdir/deletefile.txt
#deletefile=/data/script/testdir/
#存放mongo备份的目录
mongodir=/data/backups/mongo/

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

#获取当前/data/backups/mongo的最早的文件产生日期
old_file_time=$( ls -lt /data/backups/mongo | 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 3 ]
do
find $mongodir -ctime +$day_diff > $deletefile#/file$day_diff
while read file
do
rm $file 
done < /data/script/deletefile.txt
let day_diff=day_diff-1
done

基于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