1

So, i'm on my ubuntu server and want to execute the following command:

su -c /path/to/command -s /bin/bash -l otheruser

When i type this command in the linux command line, it perfectly asks for the password for the otheruser and executes the command.

However, when i do it like this

exec("su -c /path/to/command -s /bin/bash -l otheruser");

it doesn't do anything. I haven't of course specified a password for it yet, but it doesn't really return anything that could help me solve this problem. I have set the permissions to that command to 777 for testing purposes.

Any suggestions?

asked Nov 7, 2013 at 11:12

1 Answer 1

5

Try this:

<?php
$ret = exec("su -c /path/to/command -s /bin/bash -l otheruser", $out, $err);
var_dump($ret);
var_dump($out);
var_dump($err);
?>

More Info: https://www.php.net/manual/en/function.exec.php

Also, if you are expecting the exec command to ask you for the password for the other user (as it did in the linux command line) - it won't work, exec command isn't interactive. You'll need to pass the password on the command, inline.

answered Nov 7, 2013 at 11:22
Sign up to request clarification or add additional context in comments.

2 Comments

It returns the following: string(0) "" array(0) { } int(1)
that did the trick actually! However, I think that it is not very secure, passing the user's password in a php script?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.