I want to run a local shell script that have SSH commands on the server using PHP. And inside the script i am using ssh
to run a command like ls -lart
and save the result on a log file in the server, and then using scp
to copy the remote file to my local host. Something like this:
/// my_local_shell.sh
#!/bin/bash
host=$1
user=$2
port=$3
ssh -p $port $user@$host 'ls -lart >> /home/remote/file.log'
scp -P $port $user@$host:/home/remote/file.log /home/local/file.log
If i run the script using the terminal user@local_host:~$ ./my_local_shell.sh
everything works just fine. But if i use shell_exec()
to execute the script using PHP like this:
/// index.php
$output = shell_exec("my_local_shell.sh 192.168.1.1 root 2222");
echo <pre>$output</pre>;
Nothing is printed on screen and the SSH commands inside the file are not executed.
I know I can use ssh2_shell()
, but by using it I would have to send the commands inside the PHP, and it's not what i want.
I already gave the permissions needed to index.php
and my_local_shell.sh
Any ideas how I can do this?