How can I create a PHP script that has the functionality of when i use telnet to:
1. open my domain.
2. type in username at prompt.
3. type in password at prompt.
4. type in a command... mysql -p -u user <file.sql
5. type in the password when prompted.

I've been trying to use fsockopen to do this, but have not gotten very far at all.
Thanx in advance.

    if you're trying to do this on the same box php is running on, consider using the exec(), system(), passthru(), or backtick operators to run the mysql client command and import the sql script.

    if you need to import data to a remote mysql box, then use the local mysql client application to import the data to a remote server.

    i was doing something similiar, but i found it was easier (doesn't mean the code was very clean) to do the same thing using just php and mysql_db_query().

    http://www.php.net/manual/en/function.exec.php

      For remote mysql box. Shall I presume that fsockopen is entirely unsuited for this?

        fsockopen() might be able to do this, but it wouldn't be pretty.

        use something like the php function exec() to call a shell script to load the mysql client application and import the data.

        filename: test.sql
        ###############################
        use test;
        show tables;
        ###############################

        filename: test.sh
        ###############################
        mysql --host=127.0.0.1 -u username --password=password < test.sql
        ###############################

        make the shell script readable to whatever user apache is running at (likely user nobody.nobody) using "chmod o+rx

        in the php file:
        <?
        exec("/path/to/shell/script");
        ?>

        standard security issues apply. if apache can read the password likely everyone who can telnet to the machine php is running on can also read the password. you could move the shell script into the php script and run the commands that way, it'll simplify the files but still has security issues.

          The only problem is that the remote server is a virtual webhost, whereby I have a unique IP, but don't think I'll be able to use any exec() statements without having logged in.

            • [deleted]

            You can do it with fsock, but it will indeed be messy.
            What you have to do is open a connection, wait for the remote side to answer,
            check what the answer was, send your username when the remote side wants it, send your password, send your command, and then the fun part: figuring out
            when the remote side has finished processing the command.

            Remember, while the connection is open, no EOF signals are sent, so you
            have to read each line that the remote side sends, and parse it to see if it's the end of the command.
            But, if you get it wrong and you don't notice the end of the command, your script will wait for all eternity for the next line of text, because the remote side has already finished and will not send anything else... stale mate.

              14 days later

              I did precisely as you directed. If I logon via telnet and fire test.sh, it works as desired. It fails, though from the PHP file and not because of permission (as proof, if I add ls to the end of test.sh, the php script shows the directory contents but doesn't execute the mysql command) I tried passthru, which returned 0 (literally).
              I tried system()... that returned 127.
              Again... thanx.

                10 days later

                did you log into telnet via php for using the ls command. if so pls give me detail. i am trying to use fsockopen for logging to telnet and run who command but in vain!
                advance tnx

                  Write a Reply...