Skip to main content

Control your PC from Python Code

Hey guys, One of the easiest programming languages in the World is our Python. Here this post is intended for those who want to learn the hacking techniques! Here I would like to share simple 4lines of Python hack tricks that make shutdown or restart your PC. The logic here is that have the function contains the CMD Command line 'shutdown' with forceful(/s) timeout as 1 minute.


Shutdown logic is here...
import os
def shutdown():
    os.system("shutdown /s /t 1")
shutdown()

Restart logic is here
import os
def restart():
    os.system("shutdown /r /t 1")
restart()

Jump to Linux VM and same thought to execute the same script then it failed! "Must be root." The solution there you need to be superuser to shutdown your Linux machine.

To have better understand about the shutdown command in Linux operating system, used the --help option.
[vagrant@mydev ~]$ sudo shutdown --help
shutdown [OPTIONS...] [TIME] [WALL...]

Shut down the system.

     --help      Show this help
  -H --halt      Halt the machine
  -P --poweroff  Power-off the machine
  -r --reboot    Reboot the machine
  -h             Equivalent to --poweroff, overridden by --halt
  -k             Don't halt/power-off/reboot, just send warnings
     --no-wall   Don't send wall message before halt/power-off/reboot
  -c             Cancel a pending shutdown

On Linux the script will be as follows:
import os
def shutdown():
    os.system("shutdown -h")
shutdown()

The output is as follows for the Linux platform.
Linux Python Execution

Enhancement of the script
import os
import sys

def shutdown(platform):
    if platform=='linux2':
        os.system("shutdown -s")
    elif platform=='win32':
        os.system("shutdown /s /t 1")

def restart(platform):
    if platform=='linux2':
        os.system("shutdown -r")
    elif platform=='win32':
        os.system("shutdown /r /t 1")

# The main program starts here
platform=sys.platform
print platform
opt=sys.argv[1]
if opt=='s':
    print 'Shutting down your PC!!'
    shutdown(platform)
elif opt=='r':
    print 'Rebooting...'
    restart(platform)
else:
    print 'Invalid option'

Comments

Popular posts from this blog

Python Sets and Dictionaries

Python Sets Python Maps examples A mapping object maps hashable values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the dictionary. Dictionaries consist of pairs (called items) of keys and their corresponding values. Dictionaries can be created by placing a comma-separated list of key: value pairs within curly braces Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples. Python Set Operations and methods Python Set methods continueed >>> SET={'new','old','list','new'} >>> SET set(['new', 'old', 'list']) Lets play with set operations, it is more like school math here... >>> s={1,2,3,4} >>> k={3,4,5,6} >>> len(s) 4 >>> len(k) 4 >>> 1 in s True >>...

Python Interview Questions -Coding Snipets

 This post is dedicated for all DevOps Engineer, Software Engineers who are preparing for Coding Interviews,  Overview of Coding Interviews Most Companies looking for People with minimum Coding knowledge. In a coding interview, you will be given a small problem to solve within 10 - 20 minutes online screen or in-person on their system. In the question, you might be having some part of the code framed and you might be asked to write a snippet of code in between. You need to understand the code comments and proceed to build the expected snippet of code. Bigger companies look for the General purpose questions, where small companies look for specific questions. The General questions would be like this: Determine if the given word is a palindrome or not. (Example madam) Determine given number is prime or not. How to prepare for a coding interview? Now we have the flexibility to choose the programming language on which you are comfortable.  In general DevOps Infra guys will be ...

Introduction to Python Orientation

Python Building Blocks Hello everyone, Python enthusiast let me start about how we began this blog, Started learning Python from Pavan we (Sujith, Purushotham) and started noting the class and then looking into several web stuff and also into Python e-books. Once we got summary thought of sharing the idea of what we had learned about each topic as one blog post. This can continue for the next students as well anyone can share this and comment on this post. this blog posts are specially intended for newly started Python  Programming.  Re-editing : 06-Mar-2022, As I've looked at PCEP Practice exam some of the basic info that I recollected updated here in this below section. Re-edited it on 21-03-3023  Little about Python History Python came from British comedy group "Monty Python" TV show. In Feb1991 Python first release version came. Python was designed and developed by Guido Van Rossum . The Python major implemented languages are:  CPython ,  PyPy ...