How to execute pythonscript in php? PHP Python SSH2
To use php ssh to connect to a remote server, execute a python file on the server to obtain the result and print it out from php.
My code is as follows:
$connection = ssh2_connect('remote-server',22);ssh2_auth_password($connection, 'root','pwd');$string = ssh2_exec($connection, 'python pythonfile.py; sleep 20');stream_set_blocking($string,true);echo stream_get_contents($string);
However, php prints a null value.
If
$ String = ssh2_exec ($ connection, 'Python pythonfile. py; sleep 20 ')
Replace
$ String = ssh2_exec ($ connection, 'ls-L ')
The program outputs normally.
It usually takes 15 seconds for the python file in the remote server to run, so I set sleep 20, but still cannot get the result.
Are there any mistakes in the code? How can we correct it?
Reply to discussion (solution)
Why sleep? Does the remote sleep make sense?
Remove sleep because stream_set_blocking ($ string, true); has been set to blocking mode, that is, the execution result will be obtained after the shell is executed.
Remove sleep because stream_set_blocking ($ string, true); has been set to blocking mode, that is, the execution result will be obtained after the shell is executed.
The problem in the post has been solved. the returned result is null because the path of the python file is incorrect. python path/to/python/file, I added a/before path /, so the program cannot find the file for execution.
Now the program can return the result I want, but there is still a small problem, I hope you can help solve it, you can append the score.
$ String = ssh2_exec ($ connection, 'CD a/B/c; python discovery. py>/root/output.txt ;');
Stream_set_blocking ($ string, true );
Sleep (20)
$ Stream = fopen ("ssh2.sftp: // $ sftp/root/output.txt", 'r ');
$ Contents = fread ($ stream, filesize ("ssh2.sftp: // $ sftp/root/output.txt "));
Echo $ contents;
Fclose ($ stream );
Running.
But as I mentioned above, the execution of this python file usually takes 15 seconds to return the result, so a sleep (20) must be added here. Otherwise, the program will report an error saying "fopen (): Unable to open ssh2.sftp: // Resource id #5/root/output.txt on remote host in/var/www/html/test/ssh. php on line 20 ", I guess it is because the time is too short, so the program has not been able to generate the" output.txt "file.
However, if hnxxwyq, as you said, stream_set_blocking () will get the result after shell execution is complete, why has the output file been executed to fopen before the program is generated?
Which line does fopen () be executed immediately after the token is generated?
It seems that stram_set_blocking () does not have this function .. I am not very familiar with php.
Solved the problem. I changed the code again.
$ Stream = ssh2_exec ($ connection, 'CD DCA_automation/main/QAUtilities/Utils; python cluster_discovery.py ');
Stream_set_blocking ($ stream, true );
$ String = stream_get_contents ($ stream );
Echo $ string;
No longer output python results to files, and get $ stream directly because "In non-blocking mode an fgets () call will always return right away while in blocking mode it will wait for data to become available on the stream ". if the previous code is used, the output.txt file is generated, even if the result is not written, $ stream has obtained data, therefore, stream_set_blocking () is automatically terminated. However, if I write the python Generation result directly to the stream, the process will be blocked until the result is completely generated.
I don't know what I explained, right? If this is not the case, please let us know ..
Solved the problem. I changed the code again.
$ Stream = ssh2_exec ($ connection, 'CD DCA_automation/main/QAUtilities/Utils; python cluster_discovery.py ');
Stream_set_blocking ($ stream, true );
$ String = stream_get_contents ($ stream );
Echo $ string;
No longer output python results to files, and get $ stream directly because "In non-blocking mode an fgets () call will always return right away while in blocking mode it will wait for data to become available on the stream ". if the previous code is used, the output.txt file is generated, even if the result is not written, $ stream has obtained data, therefore, stream_set_blocking () is automatically terminated. However, if I write the python Generation result directly to the stream, the process will be blocked until the result is completely generated.
I don't know what I explained, right? If this is not the case, please let us know ..
No error. When stream receives the data returned by the shell, it triggers the stream return result and runs stream_get_contents.