SFTP through php exec() [duplicate] - php

This question already has an answer here:
Convert multiple SFTP command lines to a single line command line
(1 answer)
Closed 4 years ago.
Is it possible to use SFTP linux command, using php exec() or system() function?
I try this:
exec('sftp '.$user.'#'.$server.' && '.$pass.' && get '.$path1.' '.$path2.');
but it doesnt work. Guess because of login at least)
Also tried to split execs, still its useless:
exec('sftp root#'.$server);
exec($pass);
exec('get '.$path1.' '.$path2);
maybe there is any inline command or another syntaxis for this?
And yes, i want to use SFTP, no i dont wanna use libraries.
Also if there is some way to make it work with special bash or etc file, that i can run with exec() it would be nice too.

The solution is:
system('sshpass -p '.$pass.' sftp -o StrictHostKeyChecking=no root#'.$server.':'.$path.' '.$path, $D);

Related

How can I run php commands directly in console? [duplicate]

This question already has answers here:
How can I execute PHP code from the command line?
(6 answers)
Closed 2 years ago.
When I run PHP in my console (any cli application) and I sending a command like below, it not returned anything!
echo 'Hi!';
Is there any way to run PHP commands directly in console or just we can run a .php file?
Note: My environment variables and path is OK.
You may use PHP's interactive shell
php -a
echo 'Hi!';

PHP exec() not running properly [duplicate]

This question already has answers here:
Php exec command not working on Windows, works on command line
(3 answers)
Closed 6 years ago.
When I execute the code below in command line it runs fine:
C:\Users\Shraddha\book ticket\ex1 scrapy crawl bookmyshow
However it doesn't execute in PHP using exec():
exec("C:\Users\Shraddha\book ticket\ex1 scrapy crawl bookmyshow");
You need to escape the blank characters in the path, otherwise they are interpreted as separator between multiple arguments. Also it is much safer to use the forward slash as folder separator as it is used in unixoid systems and the internet in general:
exec("C:/Users/Shraddha/book\ ticket/ex1\ scrapy\ crawl\ bookmyshow");
If you insist on MS-Windows style separators, then you have to escape them too:
exec("C:\\Users\\Shraddha\\book\ ticket\\ex1\ scrapy\ crawl\ bookmyshow");
Also you may prefer to use shell_exec() here to have a well defined and initialized environment for your command to be executed in.

Can PHP execute a terminal command [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Executing linux commands with PHP
I have a PHP file that creates an .xfdf file I am wondering if PHP has a way of executing a command like you would type into the terminal window? I have to run the following command when after the .xfdf file is created to generate the .pdf file
pdftk /var/www/pdf/TimeCard.pdf fill_form /var/www/pdf/results/Brandon_01_23_13.xfdf output /var/www/pdf/results/testNew.pdf flatten
The command would need to be able to accept a PHP variable so its customized for the user. Is there an easy way to do this?
Yes, you can use exec(), pcntl_exec(), system().
$command = 'pdftk /var/www/pdf/TimeCard.pdf fill_form /var/www/pdf/results/Brandon_01_23_13.xfdf output /var/www/pdf/results/testNew.pdf flatten';
exec($command);
Be very careful when using these commands, they are very useful/powerful, but because of this a hacker could potentially gain access to your whole server through it (depending on permissions, but it's more than enough to compromise your site).
If you need to send user uploaded file names/text, check them to the blood (for example check if the file path is in the proper place, and not ../../../something.pdf, check MIME types and extensions).
Yes, you can use:
system
exec
passthru
Depending on your exact requirements, you can use exec(), system() or shell_exec()
Use the system() or eval() functions.
But make sure the user can not inject arbitrary commands!

How can I run a linux command from a PHP script [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php shell_exec() vs exec()
How do I run a linux command from a PHP script? I'm running Linux Debian and PHP5. I want to be able to issue a wget command to the console.
An example of what I'm looking for is something like this:
phpFunction ("wget http://www.example.com/image.jpg /folder");
echo "done";
Also would I be able to echo the output of that function?
Use exec to run any command. Be careful not to exec any user input though, as it can severely compromise your server.
Also, note that most shared servers block off the exec function so you won't be able to use it.
Finally, as a shorthand, you can wrap the command you want to exec in backticks.
You can do what you want with the following code :
system(command);
See http://php.net/system
You can execute linux commands within a php script - all you have to do is put the command line in brackits (`).
And also concentrate on exec() , this and shell_exec() ..
you can use
exec
shell_exec
http://php.net/manual/en/function.shell-exec.php

call python from php [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Call Python From PHP And Get Return Code
probably a very stupid question
I have a python module lets say hi.py which have the following
print "hi"
now I want to execute this file from php
<?php
#code here
?>
So how do I call that python file from php.
Thanks
Use the exec function to invoke Python.
Example:
$output = array();
exec("python hi.py", $output);
var_dump( $output);
There are other commands for executing commands on the machine PHP is running on, see the docs.
you can call php exec function and call
python -c "print 'hi'"
or if you have larger module you can use
-m mod : run library module as a script (terminates option list)
so in the end I'd use
exec("python -m hi");
and don't forget to configure PYTHON_PATH on your server if needed
be careful doing this kind of stuff

Categories