0P3N Blog Blog Post

Using Python to Facilitate Tasks in Linux

By: Krintoxi
September 21, 2015

Using Python For Task Facilitation in Linux - Cybrary

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-ysudo apt-get upgrade -ysudo apt-get dist-upgrade-y
An example using Python script that would automate all of this follows:#!/usr/bin/python# -*- coding: utf-8 -*-import osimport sys, tracebackchoice = 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  Serverif choice == "ssh":cmd1 = os.system ("ssh Server-Net@0.0.0.0")#Add Domainif choice == "add":cmd1 = os.system ("sudo nano /etc/httpd/conf/httpd.conf")#Start Apache Serverif choice == "sa":cmd1 = os.system ("sudo apachectl start")#Restart Apache Serverif choice == "ra":cmd1 = os.system ("sudo apachectl restart")#Stop apachectl Serverif 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 osimport sys, tracebackprint"1.0 - ALPHA-Cent_OS"print"Dev: The_Chosen_One "#End Of Title Area#Start Of Optionsprint "~~~~~~~~~~~~~~~~~~~~~~~"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 Authenticationuser = raw_input("What is your Name?:")print"____________________________________"if user == "Chosen":print "Welcome Admin "+ userelif user == "chosen2":print "Welcome Admin "+ userelse:print "NO You are not allowed in.... "+user+"!"sys.exit(0)print ""choice = raw_input ("What do you want to do?:")#SSH INTO EXEL Serverif choice == "ssh":cmd1 = os.system ("ssh Server-Net@0.0.0.0")#Add Domainif choice == "ad":cmd1 = os.system ("sudo nano /etc/httpd/conf/httpd.conf")#Start Apache Serverif choice == "sa":cmd1 = os.system ("sudo apachectl start")#Restart Apache Serverif choice == "ra":cmd1 = os.system ("sudo apachectl restart")#Stop apachectl Serverif 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 :)

Build your Cybersecurity or IT Career

Accelerate in your role, earn new certifications, and develop cutting-edge skills using the fastest growing catalog in the industry