shell批量修改mysql用戶密碼


需求

現在有這么一個需求, 需要大批量修改用戶的密碼, 需要注意的規則是:

必須添加的字符: *$#sjdKw%

用戶名的第一位+*$#sjdKw%+用戶名的最后一位,比如用戶名chenglee,密碼:c*$#sjdKw%e

下面開始吧, 我sql不好, 沒法實現, 只能寫shell了.

#!/bin/bash
user="root"
pass="lizhenghua"
ip="192.168.137.174"

function My(){
#ifirst
mysql --protocol=TCP -u ${user} -p''${pass}'' -h ${ip} --connect-expired-password -e "select LEFT(id, 1) from chengleedb.chenglee_user" > first.txt
#last
mysql --protocol=TCP -u ${user} -p''${pass}'' -h ${ip} --connect-expired-password -e "select SUBSTRING(id, -1) from chengleedb.chenglee_user" > last.txt
#export
#mysql --protocol=TCP -u ${user} -p''${pass}'' -h ${ip} --connect-expired-password -e "SELECT (@i:=@i+1) i,id from chengleedb.chenglee_user,(SELECT @i:=0) AS i" > user.logs
mysql --protocol=TCP -u ${user} -p''${pass}'' -h ${ip} --connect-expired-password -e "select id from chengleedb.chenglee_user" > user.logs
}

function Re(){
    exec 3<"first.txt"
    exec 4<"last.txt"
    while read line1<&3 && read line2<&4
    do
        echo "${line1}%do1pMt6Q*${line2}" >> first2.txt
    done
}

function Pot(){
    exec 5<"first2.txt"
    exec 6<"user.logs"
    while read line5<&5 && read line6<&6
    do
    #UPDATE chenglee_user SET PASSWORD = '3FWdPV5K' WHERE 1=1
    #mysql --protocol=TCP -u ${user} -p''${pass}'' -h ${ip} --connect-expired-password -e "UPDATE chengleedb.chenglee_user SET PASSWORD = '${line5}' WHERE id = '${line6}'"
    mysql --protocol=TCP -u ${user} -p''${pass}'' -h ${ip} --connect-expired-password -e "UPDATE chengleedb.chenglee_user SET PASSWORD = md5'${line5}' WHERE id = '${line6}'"
    #echo "${line6} ${line5}" >> first3.txt
    done
}

function main(){
    My
    Re
    Pot
}
main

模塊說明

My:這個模塊, 主要導出password字段的第一個字符和最后一個字符以作參考寫入, 以及用戶id的導出以作匹配.

Re:組合密碼復雜度

Pot:實現批量修改密碼, 沒一行根據id條件修改不同的匹配密碼

執行之后會生成這些目錄

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM