A
A
Alline2020-12-30 11:46:06
linux
Alline, 2020-12-30 11:46:06

Why bash script called from php doesn't work completely?

Hello. I'm trying to drill teeth through the same passage. Doesn't work, need help.

There is a php code that pulls a bash script on a remote server. Puff:

$connection = ssh2_connect(xxx, 22);   //коннектится
if (!ssh2_auth_password($connection, 'root', 'xxx')) {
    die('Не прошла аутентификация');
}
ssh2_scp_send($connection, '/path/file.sql', '/path/file.sql');  //заливает туда заранее подготовленный sql файл
$stream = ssh2_exec($connection, '/path/runner.sh');   //дергает скрипт
(the code is simplified so as not to pull the footcloth here)

Script runner.sh. He must, in turn, connect via ssh to the third server and execute sql there.
#!/bin/bash
ip='x.x.x.x'
user='root'
whoami 
ssh -t $user@$ip  ls  # Для отладки. Эта команда отрабатывает нормально
ssh -t $user@$ip  mysql < file.sql  # А вот тут затык
echo "end"  # это тоже выводится нормально

When I execute ./runner.sh just from the console on the remote server, it will spew the error tput: No value for $TERM and no -T specified into the console . But sql gets into base. When I pull it through php, mysql < file.sql does not work.

Thus ls outputs. Whoami in both cases says that the user is root. The rights to fail.sql and runner.sh have already been set to 777.
If I set it instead , then everything works again. That is, authentication in mysql passes, that's not the point. I know that it would seem easier to copy file.sql to a third server and feed the muscle from there. But there is your own hemorrhoids, it is not copied) Please tell me, what could be the matter, where to look? It's this error tput: No value for $TERM and no -T specified ?ssh $user@$ip mysql < file.sqlssh $user@$ip 'mysql -e "select * from table"'

Or how at least it is possible to debug it normally? I'm not that close to bash

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DollyPapper, 2020-01-01
@DollyPapper

I have not worked with MySQL, but I assume that it uses ncurses to output to the terminal, and when you pass this file to it, it tries to output something to the console using the $TERM variable. By default it is equal to xterm-256color. Login to your server and check if it is installed. If not, write your user under which you are executing the request in .bashrc
export TERM=xterm-256color
And try again.
How it works from PHP itself is hard to say. Maybe it's just that when you call it from the terminal, the error is ignored, and PHP receives some error code and stops working.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question