Open In App

Menu driven Python program to execute Linux commands

Last Updated : 29 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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 
 



Previous Article
Next Article

Similar Reads

Python | Execute and parse Linux commands
Prerequisite: Introduction to Linux Shell and Shell Scripting Linux is one of the most popular operating systems and is a common choice for developers. It is popular because it is open source, it's free and customizable, it is very robust and adaptable. An operating system mainly consists of two parts: The kernel and the Shell. The kernel basically
6 min read
How to Execute Shell Commands in a Remote Machine in Python?
Running shell commands on a Remote machine is nothing but executing shell commands on another machine and as another user across a computer network. There will be a master machine from which command can be sent and one or more slave machines that execute the received commands. Getting Started We will be using Websocket protocol to send shell comman
3 min read
How to Execute Shell Commands in a Remote Machine using Python - Paramiko
Paramiko is a Python library that makes a connection with a remote device through SSh. Paramiko is using SSH2 as a replacement for SSL to make a secure connection between two devices. It also supports the SFTP client and server model. Authenticating SSH connection To authenticate an SSH connection, we need to set up a private RSA SSH key (not to be
4 min read
Menu Driven Python program for opening the required software Application
In this article, we will create a menu-driven Python program which will execute the required user tool when user will give the input as a text. We can interact to OS through GUI (graphical user interface) and CLI (command line interface). We can also give the instructions to OS through programming language. In this program, you can instruct to OS u
3 min read
Python - Measure time taken by program to execute
This article aims to show how to measure the time taken by the program to execute. Calculating time helps to optimize your Python script to perform better. Approach #1 : A simple solution to it is to use time module to get the current time. The following steps calculate the running time of a program or section of a program. Store the starting time
2 min read
How to use Python Pexpect to Automate Linux Commands?
Pexpect is a Python library for spawning child processes and controlling them automatically. Pexpect can be used to automate interactive applications such as SSH, FTP, password, telnet, etc. Pexpect works by spawning child processes and responding to expected patterns. Installation: Pexpect can be installed by using the following command. pip3 inst
4 min read
How to execute a 11-digit instruction using different addressing modes in Python?
Here, we have an 11 bit instruction which has first 2 bits for representing addressing mode, next 3 bits for the opcode and the last 6 bits are for the two operands, 3 bits each. We will execute this 11 bit Instruction using four different addressing modes:- Direct mode: In this mode, the addresses of two operands are specified in the instruction.
4 min read
How to Execute a Script in SQLite using Python?
In this article, we are going to see how to execute a script in SQLite using Python. Here we are executing create table and insert records into table scripts through Python. In Python, the sqlite3 module supports SQLite database for storing the data in the database. Approach Step 1: First we need to import the sqlite3 module in Python. import sqlit
2 min read
How to Execute many SQLite Statements in Python?
In SQLite using the executescript() method, we can execute multiple SQL statements/queries at once. The basic execute() method allows us to only accept one query at a time, so when you need to execute several queries we need to arrange them like a script and pass that script to the executescript() method. executescript() can be able to execute seri
3 min read
How to Execute a SQLite Statement in Python?
In this article, we are going to see how to execute SQLite statements using Python. We are going to execute how to create a table in a database, insert records and display data present in the table. In order to execute an SQLite script in python, we will use the execute() method with connect() object: connection_object.execute("sql statement") Appr
2 min read
three90RightbarBannerImg