I'm trying to execute a command as su from php.
When I put the command in terminal or with php /var/www/script.php it works perfect, but when I open script.php through browser it returns - sudo: no tty present and no askpass program specified
Thanks.
According to this:
ssh does not allocate a tty by default when running a remote command.
Without a tty, sudo cannot disable echo when prompting for a password.
You can use ssh's "-t" option to force it to allocate a tty.
Alternately, if you do not mind your password being echoed to the
screen, you can use the "visiblepw" sudoers option to allow this.
I recently published a project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS
You can get a real bash shell as root. You can then either trigger your php script or execute the commands directly in bash.
After downloading you would simply use the following code:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd('yourFirstCommand');
$return2 = $shell->exeCmd('yourSecondCommand');
//the return will be a string containing the return of the script
echo $return1;
echo $return2;
//or if you want to trigger your script as root
$return3 = $shell->exeCmd('php /my/script.php');
echo $return3;
//If your script contains code to make an SSH connection to another server
//there is a much easier way to do that using the project:
$shell = \MTS\Factories::getDevices()->getRemoteHost('ip_address')->getShellBySsh('username', 'secretPassword');
Related
I'm developing a code which uses ldap_search Shell Script Command for extracting user information from Active Directory using user id and by proper LDAP Server Authentication. I am getting accurate result from ldap_search script.
But, whenever I put the shell script inside exec or shell_exec PHP command, I'm not getting anything.
All the other shell scripts are working fine with the help of PHP exec command except ldap_search.
Is there some additional task left for me to do?
Is ldap_search and exec/shell_exec not compatible with each other?
You must use echo exec('your command or script');
Make sure to have permissions to run it. I mean, the web user must have permissions to execute that.
May seem obvious, but I think your failure is in something basic like this. You must put echo to show the result of the command.
EDIT After reading your new comments about it and using that new info... I saw you are trying to redirect the output to a file... but maybe you have 2 different problems.
Have the user which is executing php (usually www-data) permission to write on the folder where the php is?
Your code has quotes inside quotes that must be escaped using . Try this:
<?php exec("ldapsearch -x -v -h 'LDAP://server' -p '389' -D 'uid=\"domain_user_id\",ou=users,ou=internal,o=\"organization\"' -w 'domain_password' -b 'ou=users,ou=internal,o=organization' 'uid=person's_user_id' >> result.txt"); ?>
So you don't need echo if you want the output in a file. And the redirection >> can be inside the command executed, not in php.
Remember that > replaces de file and what you have >> add at the end of the file.
I need to ssh to the server using the username user has entered on my webform.
How can this be done?
If what you mean is, "How do I connect via SSH from my website (to another server)", then you can do this with the PECL ssh2 library.
See:
http://pecl.php.net/package/ssh2
Walkthrough (untested): http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/
At first, there are no PuTTy commands. These are shell commands.
To run PHP script in shell, you need to use php-cli:
maybe you could use Command Line Scripting in PHP, it depends on what you want. http://php.net/manual/en/features.commandline.php
I am not sure but I think(correct me if I am wrong) that you want to click somewhere on a link on the webpage and open putty(on the user's computer) to connect to a server.
You can configure Putty to handle ssh:// links. How to do it you can find out here.
When that is configured all you have to do is to have a link similar to this:
Click here to connect
Have in mind that this will work only on systems that are configured to handle the ssh:// link type
I hope that this answers your question.
This is how you use putty via PHP (not dependent of cli). Note that the passwords are not protected and that an interactive ssh session would be much more involved. However, HTTPS and mcrypt (if needing to store passwords and/or bash scripts) can make this a safe solution.
<?php
// EDIT: added escapeshellcmd() to following vars
$user = escapeshellcmd($_POST['user']); // username
$host = escapeshellcmd($_POST['host']); // domain
$pass = escapeshellcmd($_POST['pass']); // password
// create a string that will be loaded into a bash file for putty
// String can easily be made dynamically.
$bash_sh = <<<EOF #START OF BASH
\#!/bin/bash
echo "BASH ON SSHD SIDE"
for (( i=1; i<=5; i++ )) # BASH FOR LOOP
do
echo "echo \$i times in bash" #\$i is BASH not PHP, so have to escape
done
EOF; #END OF BASH
// creates a temp file called 'bash.sh' using the bash script above
file_put_contents("bash.sh", $bash_sh);
// executes putty using the args -ssh, -pw, -t, -m
// -ssh tells putty to use ssh protocol
// -pw tells putty to enter the password automaticaly
// -t tells putty to use a psudo terminal.
// -m tells putty read and execute bash.sh once logged in
exec("putty.exe -ssh ".$user."#".$host." -pw ".$pass." -t -m bash.sh");
// delete bash file since it has been sent
unlink('bash.sh');
?>
I would like to execute a cronjob for a routine task every X hours. The cronjob basically executes a shell script which in turn uses a WGET command to download files from a remote server. However, before I run this shell script I want the cronjob to execute a php script which will check whether the update's available (there's no point in wasting BW and downloading the same file over and over again) and if it is, it should pass on the update URL to the shell script which in turn uses the WGET command.
The cronjobs are set from the hosts Admin Panel. There is no other way around it. Being a shared hosting service, I am not allowed access to other functions on PHP which might do the task for me either.
Is this possible? I am Linux illiterate. I have installed a few RPM's on Fedora but that's about it. Please bear with me. Thanks!
Just pass --timestamping to your wget command.
Alternatively if you are more familiar with PHP's ways you can check this question for a usable method.
Use a curl HEAD request to get the file's headers and parse out the Last-Modified: header.
To use a php script as a regular command line executable use this as a starting point:
#!/bin/env php
<?php
echo "Hello World\n";
Save the file without the .php and tuck it somewhere that your server won't serve it.
Next, set the executable bit so that you can execute the script like a regular program
(u+x in the following command means grant the [u]ser e[x]ecute privileges for helloworld, and chmod is the command that unix variants use to set file permissions)
Omit the $ in the following sequence, as it represents the command prompt
$ chmod u+x helloworld
now you can execute your commandline script by calling it in the bash prompt:
$ ls
helloworld
$ ./helloworld
Hello World
$
From here you can get the full path of the executable script:
$ readlink -f helloworld
/home/SPI/helloworld
And now you can install the cronjob using the path to your executable script.
how can I execute command wkhtmltopdf http://google.com /tmp/test.pdf from server ie http://localhost/test.php, when I do it from command line it works. I tried system() and exec() functions but did not work. When I use system('touch /tmp/test') file is created. What stops wkhtmltopdf? Is it php, apache?
Make sure that the user the script is running as knows where wkhtmltopdf bin is.
You can find out where it is with the which command.
which wkhtmltopdf
Also you can get the return status of a command by setting a variable equal to it
e.g.
$last_line = system('ls');
echo $last_line
I would like to create a php script to execute a shell command and return its output. The server requires a private key. When I first decided to test this out I created this:
<?php
$command = "ls";
$output = shell_exec($command);
echo "<pre>$output</pre>";
?>
That worked just fine. But when I changed $command to the command I really wanted to run:
$command = "/etc/init.d/mycontrollerd status /etc/mycontrollerconfig";
it gave me this output:
You need root privileges to run this script
My guess is I need to use sudo. Of course that will require putting the pem file somewhere on the server. Assuming I do that, what exactly should $command be? Should I use shell_exec(), exec(), system() or something else?
It does not matter which php function you use to start the script - what lacks is the authorization of your user account.
Either use sudo (preconfigure the web server user to run the exact command without password via visudo, and prefix the command with sudo) or set up a setuid script that executes the command on itself.
What you really need to do is set your web server to run as a specific user (other than 'nobody' for example), or give that user permissions to what you want to execute.
See also: PHP shell_exec() and sudo: must be setuid root