1

I am writing a test suite for a web application using Selenium.

In the course of which I need to test behaviour of the app in case a certain service is running or not.

I wanted to create a cgi call to a Python script turning that service on and off.

I know that the cgi call is in the context of the webserver (Apache) however thought that issuing sudo calls like so:

import subprocess
import os
command = 'sudo -S launchctl unload /Library/LaunchAgents/com.my.daemon.plist'
pwd = 'pwd123'
test1 = subprocess.Popen( command, shell=True, stdin=subprocess.PIPE)
test1.communicate(input=pwd)
test2 = os.system( 'echo %s|%s' % (pwd,command) )

would do the trick, well they don't I get return code 256.

What can I do to have this call be executed w/o touching the context in which Apache runs?

As for security: this will only run on a test machine.

4 Answers 4

1

The user that Apache runs as needs to be in the /etc/sudoers file, or belong to the sudo group, which I guess it usually doesn't. You also need to make it not ask for a password, which is configured in /etc/sudoers

For Ubuntu, check these out: https://askubuntu.com/questions/7477/how-can-i-add-a-new-user-as-sudoer-using-the-command-line

https://askubuntu.com/questions/147241/execute-sudo-without-password

Sign up to request clarification or add additional context in comments.

1 Comment

Bingo, the user running apache wasn't in the sudoer group. That did the trick. Thanks a lot
1

It could potentially be a pathing issue..

Have you tried writing out the full path like this:

command = '/usr/bin/sudo -S launchctl unload /Library/LaunchAgents/com.my.daemon.plist'

1 Comment

Thanks for the reply, the command just worked fine on command line so it wasn't that.
1

command should be a list, not a string. Try with:

command = ['sudo', '-S', 'launchctl', 'unload', '/Library/LaunchAgents/com.my.daemon.plist']

1 Comment

Thanks for the reply, the command just worked fine on command line so it wasn't that.
0

Cant run sudo this way -- sudo needs a controlling terminal to run.

Comments

Your Answer

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.