Friday, December 27, 2024
Google search engine
HomeLanguagesMenu driven Python program to execute Linux commands

Menu driven Python program to execute Linux commands

Linux is one of the most popular operating systems and is a common choice for developers. However, it is difficult to remember the vast range of commands that Linux supports, and hence a Python program that can run these commands easily is demonstrated below. 

In this article, we will deal with a Python program that can be used to run complex Linux commands. This program is a drop-down menu that gives the user a choice list and the user can proceed with his/her required option. Python has an OS module that can be used to run or execute Linux commands. The os module helps interact with the Operating System.

Here are the functionalities implemented in this program:

  1. Displaying the current date.
  2. Displaying the calendar.
  3. Configuring the web.
  4. Configuring docker.
  5. Adding a user.
  6. Deleting a user.
  7. Creating a file.
  8. Creating a folder.
     

Python3




# importing the module
import os
  
# sets the text colour to green 
os.system("tput setaf 2")
  
print("Launching Terminal User Interface")
  
# sets the text color to red
os.system("tput setaf 1")
  
print("\t\tWELCOME TO Terminal User Interface\t\t\t")
  
# sets the text color to white
os.system("tput setaf 7")
  
print("\t-------------------------------------------------")
print("Entering local device")
while True:
    print("""
        1.Print date
        2.Print cal
        3.Configure web
        4.Configure docker
        5.Add user
        6.Delete user
        7.Create a file
        8.Create a folder
        9.Exit""")
  
    ch=int(input("Enter your choice: "))
  
    if(ch == 1):
        os.system("date")
  
    elif ch == 2:
        os.system("cal")
  
    elif ch == 3:
        os.system("yum install httpd -y")
        os.system("systemctl start httpd")
        os.system("systemctl status httpd")
  
    elif ch == 4:
        os.system("yum install docker-ce -y")
        os.system("systemctl start docker")
        os.system("systemctl status docker")
  
  
    elif ch == 5:
        new_user=input("Enter the name of new user: ")
        os.system("sudo useradd {}".format(new_user))
        os.system("id -u {}".format(new_user) )   
          
    elif ch == 6:
        del_user=input("Enter the name of the user to delete: ")
        os.system("sudo userdel {}".format(del_user))
  
    elif ch == 7:
        filename=input("Enter the filename: ")
        f=os.system("sudo touch {}".format(filename))
        if f!=0:
            print("Some error occurred")
        else:
            print("File created successfully")
             
    elif ch == 8:
        foldername=input("Enter the foldername: ")
        f=os.system("sudo mkdir {}".format(foldername))
        if f!=0:
            print("Some error occurred")
        else:
            print("Folder created successfully")
              
    elif ch == 9:
        print("Exiting application")
        exit()
    else:
        print("Invalid entry")
  
    input("Press enter to continue")
    os.system("clear")


Running the application 
The application must be run on a Linux terminal. 
Steps to run the application: 

  1. Open the root account from the computer. If one does not have access to the root account then some of the root privileged commands may not work properly.
  2. Open the Linux terminal
  3. Go to the location where the .py file is saved using the cd command
  4. Run the .py file using python3 tui.py command

Output 
 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments