6

I am writing a pyQt client-server application which restarts/shutdowns PCs remotely.
The receivers are listening to the network for incomming messages, and the sender sends a restart/shutdown message to the selected receiver.

The following part of code is running on a receiver:

import os

self.currentOS = calling a function to determine the current OS

if self.currentOS == "Win":
    os.system("shutdown -r -f -t 1")
elif self.currentOS == "Lin":
    os.system("shutdown -r now")

I have 2 virtual machines acting as receivers, one on Windows and the other on Linux.

When i send a restart message to the Windows receiver, the machine restarts.
When i send a restart message to the Linux receiver, it asks for password

Incoming:EXEC_OP_RESTART
[sudo] password for jwalker: 

What do i have to change to overcome this?
Is shutdown -r now the only way, or can i do this another way (more directly)?

EDIT: In this question, something called dbus was used, and it was done without a password, i am searching about dbus, as an alternative.

Community
  • 1
  • 1
kafedakias
  • 537
  • 1
  • 6
  • 10
  • For reference, both Linux and Windows can use `shutdown -r -t 1`. On Windows, `-t 1` implies `-f`, so you don't have to specify that option (which means something totally different on Linux). – cHao Nov 11 '11 at 03:59
  • @cHao incorrect . `-f` in Windows imply **forced shutdown**. does not equal to `-t 1`. See: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/shutdown.mspx?mfr=true – Raptor Nov 11 '11 at 04:07
  • @Shivan: I'm looking at `shutdown -?` right now. It explicitly says for /t, "If the timeout period [`-t`] is greater than 0, the /f parameter is implied." It *also* says for /f, "The /f parameter is implied when a value greater than 0 is specified for the /t parameter." – cHao Nov 11 '11 at 04:12

3 Answers3

8

It takes root privileges to restart a Linux machine. Some desktop environments use a daemon to get around this.... but I suggest editing the sudoers file

https://help.ubuntu.com/community/Sudoers for a howto. Basically you'll want to allow the restart command - and only the restart command - to be run without a password.

Something like:

ALL ALL=(ALL) NOPASSWD: /usr/sbin/shutdown

will let any user on the machine restart it without using a password. You'll probably need to prefix the 'shutdown' in your system command with 'sudo', though it looks like it's being called automatically somehow. If this isn't secure enough, you can make a group, make your program run as that group, then allow that group to restart.

EDIT: Apparently this can be done with DBus (note: I haven't tested this):

dbus-send --system --print-reply --dest="org.freedesktop.Hal" /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.Restart int32:0

This works because dbus runs as root (or has root privs) and can therefore accept requests to restart from nonpriveleged processes and act on them. I still think the sudo way is cleaner, and so will anyone who maintains this code.

Robert
  • 6,412
  • 3
  • 24
  • 26
  • Thank you Robert. About the use of a password, editing sudoers looks promising. Also, i just found out about something called `dbus` (see edit), that someone used to do the same without even using a password. – kafedakias Nov 11 '11 at 04:23
  • That's the daemon I mentioned. – Robert Nov 11 '11 at 04:24
  • Sorry, my knowledge in Linux is very limited. Is there a library to include in my program, or must i install something on the target machine? I am asking because i get an error when i try to execute the dbus-send command. – kafedakias Nov 11 '11 at 04:27
  • Both of these methods shouldn't require any additional work. Both are simply executing a particular command. The sudo method is more likely to be portable, and definitely more straightforward, while the dbus method works if you don't have root priveleges (to edit the sudoers file). – Robert Nov 11 '11 at 04:29
1

Run the script with sudo python. For example, if your file were named remotesd.py, sudo python remotesd.py

john
  • 3,043
  • 5
  • 27
  • 48
0

Other than sudoers file change which gives the user extra (sometimes unwanted) privilege on the machine, you may use setuid. i.e. Change the owner of the executable to root by calling sudo chown root:<caller's username> thefile Then change the permission of the file to 4750. This way whoever has the permission to execute the file will be considered as root on execution.

ahj
  • 745
  • 1
  • 6
  • 13