質問

I'm making a program so that if the user enters a password in a python program incorrectly,it shuts down ubuntu. Example:

>password=input()
    >>if password=="ThePassword":
        >>>print ("Welcome")
    >>>>else:
        >>>>>(shutdown code here)

So if anyone can point me in the right direction, I would be very grateful.

By the way, I'm using Python 3.3 and running Ubuntu 13.10 64 bit

役に立ちましたか?

解決

What you want is to execute the shell command sudo shutdown -h now.

import subprocess
>password=input()
    >>if password=="ThePassword":
        >>>print ("Welcome")
    >>>>else:
        >>>>>subprocess.call(["sudo", "shutdown", "-h", "now"])

Most likely you will be prompted to enter your administrator password for this to work. You will need to run the program in sudo mode with root permissions.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top