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' #獲取某個分組的主機數量
浙公網安備 33010602011771號