Menu Program for Automation of RedHat Linux using Python.

Gursimar Singh
6 min readNov 15, 2020

--

Automation is the technology by which a process or procedure is performed with minimal human assistance. Performing the common tasks quite frequently can be quite boring and time consuming so to avoid that we can automate all the boring procedure and directly skip to the final part, this is he beauty of automation. Python is a great programming language to automate tasks. In this blog I’ll be explaining a Menu program which is a python project created to automate tasks.

Creating a user with permissions

What Is Sudo?

Sudo is a Linux program meant to allow a user to use root privileges for a limited timeframe to users and log root activity.

Create a new Linux user

Step 1: Login to your server as root.

Step 2: Create a user using useradd command. Replace username with your custom user.

sudo adduser username

Step 3: Set a password for the user.

sudo passwd username

You will be prompted for updating the new password. Enter the required password.

Changing password for user jenkins.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

Add sudo Privileges to a User

Now lets make our new user or an exiting user a sudo user.

Step1: Add the user to wheel group.

usermod -aG wheel username

Note: If a user is part of wheel group, he can run any command as a super user.

Step 2: Execute visudo command to open /etc/sudoers file.

visudo

Step 3: Make sure the following line is uncommented in the file.

%wheel  ALL=(ALL)       ALL

By default, even if a user is part of wheel group, you need to provide the password every time you run a command as sudo. If you need password less sudo access, you need uncomment the following where it has NOPASSWD and save the file using ESC + w + q + !

%wheel  ALL=(ALL)       NOPASSWD: ALL

Step 4: Now lets test the sudo user by logging in as the user.

su username

Now, try running sudo commands. It should work based on your password preferences (with or without password) you set for wheel group.

Adding sudo privileges for specific command execution.

There are scenarios where you might want only specific commands to be run a sudo privileges for a specific user. Lets see how we can achieve it.

You can group the set of commands to be run under Cmnd_Alias

For example, if you open the /etc/sudoers file, you can find the following aliases.

## Command Aliases
## These are groups of related commands...
## Networking
# Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dhclient, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /sbin/iwconfig, /sbin/mii-tool
## Installation and management of software
# Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum
## Services
Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig, /usr/bin/systemctl start, /usr/bin/systemctl stop, /usr/bin/systemctl reload, /usr/bin/systemctl restart, /usr/bin/systemctl status, /usr/bin/systemctl enable, /usr/bin/systemctl disable

Lets say, you want a user to have only permissions to run commands under the SERVICES alias, you need to have the following entry in the /etc/sudoers file

username            ALL = SERVICES

Note: You can have custom commands in aliases the you create under Cmnd_Alias

Python program

First, we start by entering our password to the program. Obviously we need to keep our program safe.

Since we might not always work on the local system, with this program we can perform tasks on both local and remote systems. We select local or remote by typing in the prompt. If we choose remote, we need to enter the IP address of remote system we want to work on and provide the password when prompted.

We can perform quite a big range of operations with the program. Below are the screenshots of few from the list.

Basic Linux Operations

Partiton Operation

Installation Of Software

Networking commands

Docker Operation

Working with LVM

AWS Operation

Hadoop Operation

Apache Webserver

Cron

Fstab (included with LVM)

Basic Linux Operations:

Docker Operations:

LVM operations:

AWS Commands:

EC2 Services Menu:

AWS CloudFront:

S3 Services Menu:

AWS EBS Menu:

Hadoop operations:

Some of the remote system operations:

Added cron command functionality,

Cron:

from os import systemdef editcron(preCmndStr):
choice = str(input("\nEdit cron for Current User or Other User ([CU]/OU): ").lower() or "cu")
print()
if choice in ["ou", "o", "other", "other user", "otheruser"]:
user = str(input("\nEnter Username: "))
print()
while True:
if user != "":
print()
cmnd = "crontab -u {} -e".format(user)
break
else:
user = str(input("\033[91mPlease Enter Username: "))
system("tput sgr0")
else:
cmnd = "crontab -e"
system("{} {}".format(preCmndStr, cmnd))
def listcron(preCmndStr):
choice = str(input("\nList cron for Current User or Other User ([CU]/OU): ").lower() or "cu")
print()
if choice in ["ou", "o", "other", "other user", "otheruser"]:
user = str(input("\nEnter Username: "))
print()
while True:
if user != "":
print()
cmnd = "crontab -u {} -l".format(user)
break
else:
user = str(input("\033[91mPlease Enter Username: "))
system("tput sgr0")
else:
cmnd = "crontab -l"
system("{} {}".format(preCmndStr, cmnd))
def delcron(preCmndStr):
choice = str(input("\nDelete cron for Current User or Other User ([CU]/OU): ").lower() or "cu")
print()
if choice in ["ou", "o", "other", "other user", "otheruser"]:
user = str(input("\nEnter Username: "))
print()
while True:
if user != "":
print()
cmnd = "crontab -u {} -r".format(user)
break
else:
user = str(input("\033[91mPlease Enter Username: "))
system("tput sgr0")
else:
cmnd = "crontab -r"
system("{} {}".format(preCmndStr, cmnd))
def cronversion(preCmndStr):
cmnd = "crontab -V"
system("{} {}".format(preCmndStr, cmnd))
def svcRestart(preCmndStr):
cmnd = "systemctl restart crond"
system("{} {}".format(preCmndStr, cmnd))
def svcStart(preCmndStr):
cmnd = "systemctl start crond"
system("{} {}".format(preCmndStr, cmnd))
def svcStop(preCmndStr):
cmnd = "systemctl stop crond"
system("{} {}".format(preCmndStr, cmnd))
def svcStatus(preCmndStr):
cmnd = "systemctl status crond"
system("{} {}".format(preCmndStr, cmnd))

Refer:

--

--

Gursimar Singh
Gursimar Singh

Written by Gursimar Singh

Google Developers Educator | Speaker | Consultant | Author @ freeCodeCamp | DevOps | Cloud Computing | Data Science and more

Responses (1)