Reputation: 141
I'm studying MySQL, and every time I have to
ssh XXX@XXX
command, and enter my password to the school server.mysql -u XXX -p
command, and enter MySQL password.I want to create a Bash script for performing the steps above automatically.
I can accomplish the first step with this code:
#!/usr/bin/expect -f
set address xxx.com
set password xxx
set timeout 10
spawn ssh xxx@$address
expect { "*yes/no" { send "yes\r"; exp_continue} "*password:" { send "$password\r" } }
send clear\r
interact
But I don't know how to automatically input the next command (mysql -u xxx -p
) and the password.
How can I do this?
Upvotes: 1
Views: 934
Reputation: 1
Use my.cnf to store your password securly like ssh keys.
https://easyengine.io/tutorials/mysql/mycnf-preference/
Same way ssh is also possible through ssh -i parameter and passing the private key path of the remote host.
Best of luck!
Upvotes: -1
Reputation: 21492
You don't need such a complex script to just enter the MySQL console on remote machine. Use the features of the ssh
tool:
ssh -tt user@host -- mysql -uuser -ppassword
The -t
option forces pseudo-terminal allocation. Multiple -t
force tty allocation, even if ssh
has no local tty (see man ssh
). Note the use of -p
option. There must be no spaces between -p
and password (see the manual for mysql
).
Or even connect via mysql
directly, if the MySQL host is accessible from your local machine:
mysql -hhost -uuser -p
Don't forget to adjust the shebang:
#!/bin/bash -
Upvotes: 5