Change MARIADB database user password using ssh connection

Requirements:

  • a root credential - or registered on sudoers - will access a terminal and will execute the password change.

Create a template using the following script:

#Template: Change MARIADB database user password using ssh connection
#Brand: SSH
#Date: 18/03/2021

# Access database 
expect-regex "/[$|#|>]/"
exec "mysql"

# Execute password exhange on localhost
expect-regex "/[$|#|>]/"
exec "SET PASSWORD FOR '[#USERNAME#]'@'localhost' = PASSWORD('[#NEW_PASSWORD#]');"

# Execute password exhange on %
expect-regex "/[$|#|>]/"
exec "SET PASSWORD FOR '[#USERNAME#]'@'%' = PASSWORD('[#NEW_PASSWORD#]');"

# Exit database
expect-regex "/[$|#|>]/"
exec "exit"

# Check if password was changed correctly
expect-regex "/[$|#|>]/"
exec "mysql -u [#USERNAME#] -p"

# Check if password was changed correctly, doing 
expect "*password:"
exec "[#NEW_PASSWORD#]"

# Database exit
expect-regex "/[$|#|>]/"
exec "exit"

# Terminate the connection to the remote SSH server
expect-regex "/[$|#|>]/"
exec "exit"