Pytool - Linux
- In this tool to be created for the main purpose to linux command using python programming
- And once the program created we can shutdown,lock.reboot,wifi on/off,bluetooth on/off,airplane on/off,suspend,logout out system via python programming to spawn the process
Python libraries
- argparse - The argparse module makes it easy to write user-friendly command-line interfaces. The argparse module also automatically generates help and usage messages. The module will also issue errors when users give the program invalid arguments.
- subprocess - The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes
- time - This module provides various time-related functions
- os - The OS module in Python provides functions for interacting with the operating system. OS comes under Python's standard utility modules
Prerequisites package in linux
- sudo apt install network-manager
- sudo apt install blueman
- sudo apt install gnome-screensaver
- sudo apt install tlp
Python tool for linux - code
#!/usr/bin/python3
# importing libraries
import argparse
import subprocess
import time
import os
# time
localTime = time.localtime()
dateTime = time.strftime("%m/%d/%Y,%H:%M:%S",localTime)
currentTime = dateTime.split(',')[1]
# shutdown our system
def shutdown():
print("System has shutdown now {}\nShutdowning...".format(currentTime))
time.sleep(1)
subprocess.run(["shutdown", "now"])
# lock the screen
def lock():
print("System has lock now {}\nLocking...".format(currentTime))
time.sleep(1)
subprocess.run(["gnome-screensaver-command", "--lock"])
# reboot our system
def reboot():
print("System has reboot now {}\nRebooting...".format(currentTime))
time.sleep(1)
subprocess.run(["sudo", "reboot"])
# logout our system if system can run task it will destroy and open system freshly after logout
def logout():
print("System has logout now {}\nLogouting...".format(currentTime))
time.sleep(1)
subprocess.run(["gnome-session-quit", "--logout", "--force"])
# suspend is similar to lock the screen
def suspend():
print("System has suspend now {}\nSuspending...".format(currentTime))
time.sleep(1)
subprocess.run(["systemctl", "suspend"])
# wifi on
def wifiOn():
print("System wifi turn on {}\nLoading...".format(currentTime))
time.sleep(1)
subprocess.run(["nmcli","radio","wifi","on"])
# wifi off
def wifiOff():
print("System wifi turn off {}\nLoading...".format(currentTime))
time.sleep(1)
subprocess.run(["nmcli","radio","wifi","off"])
# bluetooth on
def bluetoothOn():
print("System bluetooth turn on {}\nLoading...".format(currentTime))
time.sleep(1)
subprocess.run(["rfkill","unblock","bluetooth"])
# bluetooth off
def bluetoothOff():
print("System bluetooth has turned off {}\nLoading...".format(currentTime))
time.sleep(1)
subprocess.run(["rfkill","block","bluetooth"])
# turn on airplane mode
def airplaneOn():
print("System airplane mode has turned on {}\nLoading...".format(currentTime))
time.sleep(1)
subprocess.run(["rfkill","block","all"])
# turn off airplane mode
def airplaneOff():
print("System airplane mode has turned off {}\nLoading...".format(currentTime))
time.sleep(1)
subprocess.run(["rfkill","unblock","all"])
# main function
def main():
parser = argparse.ArgumentParser(description="Control system actions")
parser.add_argument("--type","-t", required=True, choices=["shutdown", "lock", "reboot", "logout", "suspend","wifiOn","wifiOff","bluetoothOn","bluetoothOff","airplaneOn","airplaneOff"], help="Type of action")
parser.add_argument("--type","-t", required=True, choices=["shutdown", "lock", "reboot", "logout", "suspend","wifiOn","wifiOff","bluetoothOn","bluetoothOff","airplaneOn","airplaneOff"], help="Type of action")
args = parser.parse_args()
action = args.type
if action == 'shutdown':
shutdown()
elif action == 'lock':
lock()
elif action == 'reboot':
reboot()
elif action == 'logout':
logout()
elif action == 'suspend':
suspend()
elif action == 'wifiOn':action
wifiOn()
elif action == 'wifiOff':
wifiOff()
elif action == 'bluetoothOn':
bluetoothOn()
elif action == 'bluetoothOff':
bluetoothOff()
elif action == 'airplaneOn':
airplaneOn()
elif action == 'airplaneOff':
airplaneOff()
else:
help()
if __name__ == "__main__":
main()
How to save a file and give a permissions
- filename - pytool (save a file without .py extension, because first line am using #! shebang symbol to used where the python located in linux)
- permissions - sudo chmod +x pytool (executable permissions to the python file)
How to create linux command using our own code
- echo $PATH
usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
- export PATH=$PATH:/your/python/code/directory/folder/
- echo $PATH
usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/your/python/code/directory/folder/
- If you want to remove our own linux command - *directory
- echo PATH=$PATH: usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
- NOTE: remember copy the PATH variable content without your own code directory
- echo $PATH usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
Another way to create own python code for a command using alias
- nano .bashrc (in home folder to have .bashrc file)
- alias pytool="python3 /path/to/directory/<filename>.py" (add to end of .bashrc file and save .bashrc file [ctrl + o and enter and ctrl + x])
- source .bashrc
- exit
- Again open terminal or ctrl + alt + t
- pytool -t <type-data>
How to run this tool via linux terminal
- pytool --help or pytool -h
- pytool -t or --type <shutdown,lock,reboot,suspend,logout,wifiOn,wifiOff,BluetoothOn,BluetoothOff,airplaneOn,airplaneOff>