I’m trying to connect to a mysql database through an SSH connection using NET_SSH. I’m using the interactive shell with read and write.
My code looks like this:
echo '<pre>';
$ssh -> setTimeout(10);
echo $ssh -> read('');
$ssh -> write('ls');
echo $ssh -> read('');
echo '<br />';
$string = 'mysql -u '. $database -> database_user . ' -h ' . $database -> datbase_host . ' --password=' . $database -> database_password . ' ' . $database -> database_name. '; show databases;';
$ssh -> write($string);
echo $ssh -> read('mysql>');
echo '<br />';
$this -> write('show databases;');
echo $ssh -> read('');
echo '<br />';
But my output looks like this:
lsubuntu@ip-xxxxxx:~$ ls
Last login: Fri Dec 28 16:08:39 2012 from ool3.dyn.optonline.net
mysql -u db_user -h xxx.us-east-1.rds.amazonaws.com --password=abc123 my_database; show databases;ubuntu@ip-xxxxxx:~$ mysql -u db_user -h xxx.rds.amazonaws.com --password=abc123 my_database; show databases;
Last login: Fri Dec 28 16:08:49 2012 from ool3.dyn.optonline.net
show databases;ubuntu@ip-10-151-4-222:~$ show databases;
My question is, why is not being interactive like I thought it would? I should say the expected results was a mysql connection or at least a mysql prompt. But I just get that. Am I doing something wrong?
Honestly, I would totally rewrite that. eg.
Only time I’d ever use read(”) is to figure out what the prompt is.
Also, note how I’m ending every write request with a \n. Linux isn’t some AJAXified server. You don’t just type the command and expect it to be ran. You have to hit enter.
I mean, sometimes you can hit keys and they’ll just do things without enter being pressed. Like in some situations in vi. You’re in normal mode (as opposed to insert mode) and you hit dd on some open text file and the whole line you’re on gets deleted even though you didn’t hit enter.