0

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?

1 Answer 1

1

Honestly, I would totally rewrite that. eg.

echo '<pre>';
        echo $ssh -> read('[prompt]');

        $ssh -> write("ls\n");
        echo $ssh -> read('[prompt]');
        echo '<br />';

        $string = 'mysql -u '. $database -> database_user . ' -h ' . $database -> datbase_host . ' --password=' . $database -> database_password . ' ' . $database -> database_name. '; show databases;';
        $ssh -> write("$string\n");
        echo $ssh -> read('mysql>');
        echo '<br />';

        $this -> write("show databases;\n");

        echo $ssh -> read('mysql>');
        echo '<br />';

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Same result with the prompt
I updated my post. Long story short... add \n to the end of all your write() calls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.