Using Python to Facilitate Tasks in Linux

September 21, 2015 | Views: 7588

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

Hello, this is a short guide on how to use the Python Scripting language to facilitate Tasks in Linux.

I’ll start by giving a small overview on what Python is:

According to Wikipedia,”Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java.”

 

Now that that’s out of the way, let’s get started!

An example of using Python to facilitate a Tasks in a Linux can be shown through Debian Distribution. Let’s say you want to update and upgrade your system, normally the process goes like this for Debian based Distros:

sudo apt-get update

-y

sudo apt-get upgrade

-y

sudo apt-get dist-upgrade

-y


An example using Python script that would automate all of this follows:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys, traceback

choice = raw_input(“What do you want to do? :”)

if choice == “update”:

                cmd1 = os.system (“sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade -y”)

else:

print “Option ” +choice+ ” Does not exist”


When you run this script, it will ask you what you want to do. If you type “update,” it will run the series of commands you set. If you type anything else, it will tell you the option does not exist.

This very simple script can easily be expanded upon. For example. I’m a sys admin and I work with Cent_OS and Apache a lot. One of the things I do is add Namebased virtual hosts to Apache. This process can also be facilitated with Python.

For example:

choice = raw_input (“What do you want to do?:”)

#SSH INTO  Server
if choice == “ssh”:
cmd1 = os.system (“ssh Server-Net@0.0.0.0”)
#Add Domain
if choice == “add”:
cmd1 = os.system (“sudo nano /etc/httpd/conf/httpd.conf”)
#Start Apache Server
if choice == “sa”:
cmd1 = os.system (“sudo apachectl start”)
#Restart Apache Server
if choice == “ra”:
cmd1 = os.system (“sudo apachectl restart”)
#Stop apachectl Server
if choice == “ea”:


With Python, you can create a central command center packaged with everything you wish to do in your distro: running script.py and typing in “update” or “ssh” is much easier then typing out every single command in a terminal.

With this, I end my submission.

 

But, I’ll leave an example of an expanded script I’m working on for Cent_OS with BASIC NAME/VARIABLE Authentication. I hope this will be useful to you or someone you know:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys, traceback
print”1.0 – ALPHA
-Cent_OS”
print”Dev: The_Chosen_One ”
#End Of Title Area
#Start Of Options
print “~~~~~~~~~~~~~~~~~~~~~~~”
print “Remote Server Access”
print “~~~~~~~~~~~~~~~~~~~~~~~”
print “(ssh) SSH Into Server-Net@interwebs”
print “(uc)Update and Upgrade Server -Cent_OS”
print “(ud)update and Upgrade Server – Debian”
print ”

print “~~~~~~~~~~~~~~~~~~~~~~~”
print “Local Server Actions”
print “~~~~~~~~~~~~~~~~~~~~~~~”
print “(ad) Add Domain Name To Config File”
print “(sa) Start Apache Server”
print “(ra) Restart Apache Server”
print “(ea) Stop Apache Server”
print”

print “~~~~~~~~~~~~~~~~~~~~~~~”
print “Install Server Tools”
print “~~~~~~~~~~~~~~~~~~~~~~~”
print “(fzftp) Install FileZilla FTP”
print “(openssh) Install Openssh Server”
print”____________________________________”
#Admin Authentication
user = raw_input(“What is your Name?:”)
print”____________________________________”
if user == “Chosen”:
print ”
Welcome Admin “+ user
elif user == “chosen2”:
print “Welcome Admin “+ user
else:
print “NO You are not allowed in…. “+user+”!”
sys.exit(0)
print “”
choice = raw_input (“What do you want to do?:”)
#SSH INTO EXEL Server
if choice == “ssh”:
cmd1 = os.system (“ssh Server-Net@0.0.0.0”)
#Add Domain
if choice == “ad”:
cmd1 = os.system (“sudo nano /etc/httpd/conf/httpd.conf”)
#Start Apache Server
if choice == “sa”:
cmd1 = os.system (“sudo apachectl start”)
#Restart Apache Server
if choice == “ra”:
cmd1 = os.system (“sudo apachectl restart”)
#Stop apachectl Server
if choice == “ea”:
cmd1 = os.system (“sudo apachectl stop”)
if choice == “fzftp”:
cmd1 = os.system(“sudo yum Install filezilla”)
elif choice == “fzftpdebian”:
cmd1 = os.system(“sudo apt-get install filezilla”)
if choice == “openssh”:
cmd1 = os.system (“sudo yum install openssh”)
if choice == “uc”:
cmd1 = os.system = (“sudo yum update”)
elif choice == “ud”:
cmd1 = os.system (“sudo apt-get update && sudo apt-get upgrade”)

 

Thank you 🙂

Share with Friends
FacebookTwitterLinkedInEmail
Use Cybytes and
Tip the Author!
Join
Share with Friends
FacebookTwitterLinkedInEmail
Ready to share your knowledge and expertise?
22 Comments
  1. i hate snakes but i like python ha-ha

Page 4 of 4«1234
Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Support Cybrary

Donate Here to Get This Month's Donor Badge

 

We recommend always using caution when following any link

Are you sure you want to continue?

Continue
Cancel