0

I'm trying to make a program that allows me whenever the user want, shutdown or restart the computer. I'll send this to my friends so I don't know which operating system. I'm using tkinter too,here is an example codes;

import subprocess
import os

time = "10"

if os.name == "posix":
    subprocess.call(["shutdown", "-h", time])
elif os.name == "nt":
   subprocess.call(["shutdown", "-s", "-t", time]) 

However, on linux, it's asking password to user before shutting down. So this program is useless if asking password before shutting down. I tried to use %admin ALL = NOPASSWD: /sbin/shutdown after if statement but it doesn't work either. How to pass this password thing on linux?

GLHF
  • 3,835
  • 10
  • 38
  • 83
  • How exactly do you want this program to be run? From the command line, or from a GUI? Why does a password prompt make the program useless? – texasflood Feb 16 '15 at 15:39
  • @texasflood because this idea came from laziness :-) When I too lazy to stand up and shut down the computer, I want to set a timer. So asking password make this useless. – GLHF Feb 16 '15 at 23:54
  • You could run the python program with sudo, then you only need to enter the password as soon as you run it but not when it actually shuts down. Sudo should be inherited for the children – texasflood Feb 17 '15 at 01:15
  • it seems your real question is "how do I shutdown a computer without a password". *"I tried to use %admin ALL = NOPASSWD: /sbin/shutdown after if statement but it doesn't work either."* -- there should be no `if` statement, you should save this line to `/etc/sudoers.d/shutdown` (on Debian) or similar. Make sure the file mode is `0440` and your `/etc/sudoers` file contains `#includedir /etc/sudoers.d` at the end. Use `visudo` to edit it if necessary. – jfs Feb 17 '15 at 03:42

3 Answers3

1

I don' t think that's possible, you do have to disable the password or your program is like you said going to be useless. But I could be wrong ;)

1

A direct call of subprocess or Popen could NOT possibly handle this, as this is an interactive to wait for the user input, you can use pexepct for this kind of interactive mode. http://pexpect.readthedocs.org/en/latest/examples.html

Cui Heng
  • 1,265
  • 8
  • 10
  • it can if you use `pty` module but `pexpect` is (much) more convenient. Here's a [code example that uses `subprocess`, `pty` modules and may read directly from the terminal (e.g., to read the password prompt)](http://stackoverflow.com/a/22253472/4279). – jfs Feb 17 '15 at 03:28
0

You should configure a passwordless /sbin/shutdown via your sudoers settings instead of hardcoding the password in your script.

It is not recommended but you can pass the password to sudo (based on sudo mount example):

#!/usr/bin/env python
from getpass import getpass
from subprocess import Popen, PIPE

command = 'shutdown -P now'.split()
sudo_password = getpass('Sudo password: ')

p = Popen(['sudo', '--stdin'] + command, stdin=PIPE, stderr=PIPE,
          universal_newlines=True)
sudo_prompt = p.communicate(sudo_password + '\n')[1]

# should not get here
print('sudo return code = {}'.format(p.returncode))
print('sudo stderr = ' + sudo_prompt)
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670