John Davidson

linux - php exec('ssh user@server "bash" ... ) to open a remote terminal to do stuff?

0 comments
Message:


I checked some pages here (e.g: How can I use PHP to setup an interactive SSH session? ) php.net/ssh2 i know ..


but no where i found an answer about the problems opening a remote shell (eg: bash) to do stuff at the remote side, closing it an the php script go's on.


php exec('ssh user@server "bash -i -l", $bufferList) works but the output goes to the local buffer.


closing the remote connection and i can work with the output (after/post processing). hmm


But: i was tring to enter custom commands at the remote side. Expecting the php script goes ahead when closing the connection. How to? Or do i have to use something like php/ssh2 and manage all the remote calls and answer posibilities at the local script?


--edit--


currently: i copy a shell script to the target server, login over there and execute it then.


To reproduce:


exec('ssh user@server "ls -al", $bufferList);
-> shows the list of files from the remote server of 'user' $HOME path print_r($bufferList); -> local output, connection closed


exec('ssh user@server "bash -i -l", $bufferList); -> print_r($bufferList);` -> empty array, connection closed.
Expected: Command Prompt.


Something to do with tty, but i have not found an answer yet.



My Answer:
The exec() function in PHP is used to execute an external program or command. It takes a command as a parameter and executes it in the shell. Here is an example of how you can use the exec() function in PHP to run a Linux command:

php
// Run the 'ls' command to list files in the current directory
exec('ls', $output);

// Print the output of the command
foreach ($output as $line) {
echo $line . "\n";
}
?>


In this example, the exec() function is used to run the 'ls' command, which lists the files in the current directory. The output of the command is stored in the $output array, which is then printed out using a foreach loop.

Rate this post

4 of 5 based on 9358 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us