-1

I know this question has been asked several times before, but the solutions posted have not worked for me. I have tried all of them.
So here is my question

Q) I need to login to a @server using a username @username and password @password. On that server i have to use another password for mysql @sql_password and then start querying.

PUTTY
I start putty connect to @server, give @username and @password and i get connected. Then i issue mysql -p@sql_password and i'm connected to mysql. Then i can use databases and start my queries.
But Now I need to do this in PHP!

PHP

  1. First method :-
    Use phpseclib to establish an SSH connection and then try connecting to mysql. Unfortunately when i issue the mysql -p@sql_password command my code just keeps running and i can see nothing on the screen. Now some solutions talked about using different syntaxes but nothing seems to work for me.
    Here is the simple code

    set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
    include('phpseclib/Net/SSH2.php');
    
    $server=@server;
    $username=@username;
    $password=@password;
    $sql_password=@sql_password;
    
    
    $ssh = new Net_SSH2($server);
    if (!$ssh->login($username, $password)) {
      exit('Login Failed');
    }
    
    echo $ssh->exec('whoami');
    
    
    echo $ssh->exec('mysql -p@sql_password');
    
  2. Second Method
    use PHP Shell_exec command and plink.
    From the command line i can issue plink -ssh @username@@server -pw@password and it seems to work properly. From there onwards its the same as putty. Now when i try using shell_exec in php it does'nt seem to work. For instance echo shell_exec('plink'); gives me all the options of plink but when i run echo shell_exec ('plink -ssh @username@@server -pw@password'); it gives me nothing. I do a whoami to try and figure out somethings and i get nt/authority system ,while on command line i see my proper username.

    I am really lost here. Any help would be greatly appreciated.
    thanks

neubert
  • 15,947
  • 24
  • 120
  • 212
zeemav
  • 1
  • 5

2 Answers2

0

EDIT : my fisrt solution cannot work if you can't acces to the server:

Second solution, correct the code for ssh connection :

if (!$ssh->exec('mysql -u root -puserpass \r\n')) {
    exit('Login to MySQL Failed');
} else {
    echo "Login to MySQL Success";
}

No space between -p and the pass.


First solution :

I propose you this solution :

On your local machine

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('phpseclib/Net/SSH2.php');

$server=@server;
$username=@username;
$password=@password;


$ssh = new Net_SSH2($server);
if (!$ssh->login($username, $password)) {
  exit('Login Failed');
}

echo $ssh->exec('php script.php');

On the server

script.php

mysql -p@sql_password


Then you avoid the security issue of showing the password of mysql in the local machine. I also recommend you to use public/private key for ssh.

mpgn
  • 7,121
  • 9
  • 67
  • 100
  • thanks for the reply. it says that login to mysql success, and it gives the same message even if i give it the wrong password!. Anyways i cant seem to read anything after the login so i'm guessing i'm still at square one. – zeemav Aug 12 '14 at 18:17
  • @user3618663 you also need to allow remote accces to mysql on the server. If you can't touch the server there is no way – mpgn Aug 12 '14 at 18:36
  • I can log on the server with server and username and password and then there is mysql on it which needs another sql_password. When you say remote access to mysql server do you mean going inside mysql and allowing privilidges or allowing server privilidges. How do i check if mysql allows remote access option or not? @martialdidit – zeemav Aug 12 '14 at 21:16
0

You could do something like...

echo $ssh->exec('echo "select * from table where company_id=\"15\";" | mysql -u username -password=password database');

You could also do...

$ssh->write("mysql -p@sql_password\n");
echo $ssh->read("some prompt");
$ssh->write("SQL\n");

...etc...

This might also work:

$ssh->enablePTY();
$ssh->exec("mysql -p@sql_password");
$ssh->read("some prompt");
$ssh->write("SQL\n");
neubert
  • 15,947
  • 24
  • 120
  • 212
  • Well that's not super helpful. Which one didn't work? Did you try all three? If you tried the first one what was the output? If you tried the second one.. are you sure you were using the right prompt? See http://stackoverflow.com/a/24272665/569976 for how I generally go about figuring out what prompts I need to check for. Also, you may need to do a final `$ssh->read()` after you've actually sent the SQL. In my experience some servers won't actually run the command if the connection is closed before the command had a chance to run hence the need for a final `$ssh->read()`. – neubert Aug 15 '14 at 16:26
  • sorry for the late reply, but i did'nt pursue this problem anymore. I tried all three and in the first one, it does'nt grant access while the next two the system just times our after the php 30sec delay.@neubert – zeemav Aug 19 '14 at 06:49
  • Well if you do want to pursue it try doing something similar to what's described at http://stackoverflow.com/a/24272665/569976 to figure out the prompt's. I can't give you the exact commands to run as I don't know the prompts your server is using. And for the first one it'd help if you posted the error instead of just saying "it doesn't grant access".. – neubert Aug 19 '14 at 12:55