I have been struggling with this issue for a few days now. I have scoured the internet for a solution and tried many different methods but none seems to be fruitful so far. I am passing a password variable which contains special characters to expect block and it always error out at the password.
Here's my script:
#!/usr/bin/bash
server_List=("MySQLServer1" "MySQLServer2")
osUser='mysql'
osUserPwd='H3htg#41fRth!'
newRootPass='prnH4xRJSMwbWn9ht!M5t'
oldRootPwd='prnH4x^JSMwbWn9h!M6J'
for host in ${server_List[@]}
do
echo "Processing host $host"
/usr/bin/expect -c '
spawn ssh -o "StrictHostKeyChecking no" '$osUser'@'$host';
expect "password: "
send "'$osUserPwd'\r"
expect "$ "
send -- "mysqladmin --user root -p password '$newRootPass'"
expect "Enter password: "
send -- "'$oldRootPwd'\r"
expect "$ "
send "exit\r" '
done
Here's the Error:
[mysql@centralserver myself]$ ./test2.sh
Processing host MySQLServer1
spawn ssh -o StrictHostKeyChecking no mysql@MySQLServer1
Password:
Last login: Tue Jan 24 10:00:43 2023 from 10.14.20.18
[mysql@MySQLServer1 ~]$ mysqladmin --user root -p password prnH4xRJSMwbWn9ht!M5tprnH4x^JSMwbWn9h!M6J
-bash: !M5tprnH4x: event not found
I would appreciate if someone can point me where I am making the mistake.
expect
command. Replace the'
with<<EOT
in theexpect
command, remove'
from the end of the lastsend
command, and add a line that starts withEOT
in the first column after that lastsend
command.