hg clone www.bitbucket.org/blueluna/transmissionrpc
sudo apt-get install mercurial
cd transmissionrpc
sudo python setup.py install
#!/bin/sh
sudo /sbin/shutdown $*
chgrp shutdown /usr/bin/shutdown
chmod g+x /usr/bin/shutdown
%shutdown ALL= NOPASSWD: /sbin/shutdown
shutdown:x:1002:
shutdown:x:1002:user1
shutdown:x:1002:skymanphp
This code was highlighted with SkyManPHP .
- #! / usr / bin / python
- # - * - coding: utf-8 - * -
- # Pay attention to the second line-comment in the script. It allows you to avoid misunderstandings with a unicod encoding in the text of the script.
- # Import the necessary functions from the corresponding modules
- from time import localtime strftime sleep
- from sys import argv
- from os import geteuid, system
- from transmissionrpc import client
- from os.path import isfile
- # It is not necessary for the user to be an administrator. For then running Transmission as root is undesirable. And why is there admin power?
- if geteuid () == 0:
- print "\ 033 [1; 31mYou should not run this script as administrator! \ 033 [1; m"
- exit ()
- # Check if libnotify-bin is installed
- use_libnotify = isfile ( "/ usr / bin / notify-send" )
- # If not, then we will display a recommendation to do this for user-friendly notifications.
- if not use_libnotify:
- # Ask the user
- res = raw_input ( "\ 033 [1; 33mThe libnotify-bin package is not installed. You can install it with \ nsudo apt-get install libnotify-bin \ nDo it now? Type Y if you agree and press Enter: \ 033 [1; m " )
- if res in [ 'Y' , 'y' ]:
- # If he agrees, install the package
- res = system ( "sudo apt-get install -y libnotify-bin" )
- if res == 0:
- # I wonder if the package is installed?
- print "\ 033 [1; 32mPack libnotify-bin successfully installed! \ 033 [1; m"
- else :
- # It seems that the password is incorrectly entered or the problem with the package manager. Although, not the last reason.
- print "\ 033 [1; 31m Error installing libnotify-bin package. Try to do it yourself later. \ 033 [1; m"
- else :
- # Or do not install
- print "\ 033 [1; 33mIn this case, you can install it later or not install it at all. \ 033 [1; m"
- try :
- # Well, let's try to connect to the Transmission RPC service, using the appropriate parameters specified in its settings
- tc = Client ( 'localhost' , port = 9091, user = None, password = None)
- except:
- # Uh-uh .. Transmission, does not seem to be running?
- print "\ 033 [1; 31mError of connection to Transmission. Maybe it is not running or the web interface is not enabled in the program settings. \ 033 [1; m"
- # Inquire if you need to start the program now
- res = raw_input ( "\ 033 [1; 33mStart Transmission now? Enter Y if you agree and press Enter: \ 033 [1; m" )
- # So what did he put in there?
- if res in [ 'Y' , 'y' ]:
- # Well, run, if he agrees. Be sure to in the background!
- system ( "transmission &" )
- # and wait 5 seconds for it to load
- sleep (5)
- print "\ 033 [1; 32mTransmission successfully started. \ 033 [1; m"
- # Let’s try again to connect to the Transmission RPC service
- tc = Client ( 'localhost' , port = 9091, user = None, password = None)
- else :
- # We are leaving. Do not want what you want. Why then was it to run this script at all?
- print "\ 033 [1; 33mCompletion of the script. \ 033 [1; m"
- exit ()
- else :
- # If, after all, we have established a connection with Transmission, then we will rejoice about this user
- print "\ 033 [1; 32mConnection to Transmission established. \ 033 [1; m"
- # We will use the variable-flag of downloading to indicate whether at least one torrent is being downloaded. Initial value = True to start our validation cycle.
- downloading = true
- # While downloads are going (checking the downloading flag)
- while downloading:
- # And immediately reset it to False
- downloading = False
- # How many torrents?
- trc = len (tc.list ())
- # Clear the console for beautiful output
- system ( "clear" )
- # Colorize: \ 033 [brightness; color
- print "\ 033 [1; 36m ==== Download Status ==== \ 033 [1; m"
- # let's start going through all the torrents
- for i in range (1, trc):
- # Get information about the torrent with the index i
- torrent = tc.info (i) [i]
- # Default color
- col = "\ 033 [2; 37m"
- # Every man to his own taste!
- if torrent.status == "seeding" :
- col = "\ 033 [1; 36m"
- elif torrent.status == "downloading" :
- col = "\ 033 [1; 32m"
- elif torrent.status == "checking" :
- col = "\ 033 [1; 34m"
- print col + torrent.status + "(" + str ( "% .3g" % torrent.progress) + "%)" + torrent.name + "\ 033 [1; m"
- # Is it downloaded / checked? If not, this cycle will be interrupted, since the downloading flag will be set to False
- downloading = downloading or (torrent.status == "downloading" ) or (torrent.status == "checking" )
- print "\ 033 [1; 36m ==== Total torrents:" + str (trc) + "==== \ 033 [1; m"
- # Breathe before the next check.
- sleep (10)
- # Since we have reached here, it means downloading = False. Well, let's start the computer shutdown procedure.
- try :
- # If the libnotify-bin package is installed, let's say this to the user beautifully.
- if use_libnotify:
- system ( 'notify-send "All downloads are complete" "The system is shutting down. You can interrupt the script by pressing Ctrl + C in the script window." -i /usr/share/icons/gnome/scalable/actions/exit.svg' )
- # Or not quite beautiful ...
- print "\ 033 [1; 31mAll downloads are complete. The system is shutting down. You can interrupt the script by pressing Ctrl + C in the script window. \ 033 [1; m"
- # Give the user the last chance - 30 seconds to interrupt the script
- sleep (30)
- # if, after all, he interrupted him by pressing the key combination Ctrl + C
- except KeyboardInterrupt:
- # If the libnotify-bin package is installed, let's say this to the user beautifully.
- if use_libnotify:
- system ( 'notify-send "Shut down stopped" "Interrupted by user." -i /usr/share/icons/gnome/scalable/actions/redo.svg' )
- # Or not quite beautiful ...
- print "\ 033 [1; 31mCompletion stopped" "Interrupted by user. Exit script \ 033 [1; m"
- # We leave away from the script
- exit ()
- # And here it is, in fact, the function that initiates the shutdown of the system. Shutdown!
- system ( "/ usr / bin / shutdown -h now" )
- # If suddenly it will be interesting to the falling asleep user, when the script has started shutting down, open the file for this in the record mode
- f = open ( '/tmp/shutdownctl.time' , 'a' )
- # And write down our event there, specifying the time.
- f.write ( 'Shutdown was started at' + strftime ( "% a,% d% b% Y% H:% M:% S" , localtime ()) + '\ n' )
- # And finally, close the log file.
- f.close
- # That's all!
Source: https://habr.com/ru/post/93004/
All Articles