Sunday, February 26, 2012

Shell script to (1) log into remote box, (2) connect to MySQL db, and (3) show all tables

Requires expect (on RedHat: "su", "yum install expect")

Run as follows: expect /path/to/file/

set timeout -1
spawn ssh
expect "*?assword:*"
send -- "password"
expect "*#*"
send -- "mysql -u db_username -p\r"
expect "*?assword:*"
send -- "db_password"
expect "*>*"
send -- "use my_database;\r"
expect "*>*"
send -- "show tables;\n"
interact +++ return
expect eof

to exit MySQL
press ctrl+c

to close connection
type "+++"
press ctrl+c

No comments:

Post a Comment