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条件修改不同的匹配密码

执行之后会生成这些目录

原文地址:https://www.cnblogs.com/chenglee/p/10338286.html