Python OS Module

Introduction

Python is a powerful scripting language. So, why not use it to script Linux? The os module is Python’s answer to handling many Linux file operations.

The os module allows Python to perform many of the file and folder operations that you’d typically carry out in the Linux command line. It enable you to begin swapping out Bash for Python, which makes for a much cleaner and friendlier scripting experience.

Loading OS

The os module is a Python module like any other. In any script where you want to use it, you can use an import statement to pull it in.

import os

getcwd()

The getcwd() method returns the current working directory in the form of a string. You don’t need to pass it anything. It’s roughly the equivalent of pwd.

print(os.getcwd())

Read more

Python Boolean Operators

Introduction

If you’ve been following along, you’re probably tired of hearing about lists right about now. Well, this guide has nothing to do with lists! Remember back when you first leaned about variables; how there was one that just held True or False called a Boolean? There hasn’t been a guide on them because Booleans are actually that simple. However, there are symbols called Boolean Operators that are used to evaluate whether a statement is true or false. They compare statements and return either true or false. It sounds simple, and in a way it is, but it can get more complex when more comparisons get added.

And

The first comparison operator is and. You can use and to test in one statement and another statement are both true.

is_it_true = (3 * 4 > 10) and (5 + 5 >= 10)
print(is_it_true)

The code prints out True because both 3 * 4 > 10 and 5 + 5 >= 10 are true.

Read more

How to use argparse to parse python scripts parameters

Objective

Learn to use the argparse module to easily parse python scripts parameters

Requirements

  • Basic knowledge of python and object oriented concepts

Difficulty

EASY

Conventions

  • # – requires given linux commands to be executed with root privileges either
    directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

In a previous article we have seen how to parse command line arguments using getopts in the context of bash scripts (you can find the article here). Now we will see how to accomplish the same task, in a more powerful way, when writing a python script.

Read more

Learn the basics of the ZSH shell

Objective

Install ZSH with Oh My ZSH and learn the basic features.

Distributions

ZSH is available in the repositories of nearly every distribution.

Requirements

A working Linux install with root privileges.

Difficulty

Easy

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

Bash isn’t bad. It gets the job done just fine, but have you ever considered what it’d be like if Bash had some extra features to make it more convenient to work with? That’s more-or-less what ZSH is.

It includes all of the features that you’d expect from Bash, but it also has some really nice additions to make your life easier. Actually, you’ll be amazed at how much easier they make working in the command line.

Read more

prime numbers python script fine result

Function to check for a prime number with python

Below is a simple function to check for a prime number. The function is_prime_number() returns False if the number supplied is less than 2 and if the number is equally divisible with some other number different than 1 and itself. If none of the previous conditions apply the function will return True. The below python script will let user to decide how many numbers needs to be check to see whether the number is prime number:

#!/usr/bin/env python

prime_numbers = 0

def is_prime_number(x):
    if x >= 2:
        for y in range(2,x):
            if not ( x % y ):
                return False
    else:
	return False
    return True
	        

for i in range(int(raw_input("How many numbers you wish to check: "))):
    if is_prime_number(i):
        prime_numbers += 1
        print i

print "We found " + str(prime_numbers) + " prime numbers."

Read more

How to Play Audio With VLC In Python

Objective

Play audio with VLC in Python.

Distributions

This will work on any Linux distribution

Requirements

A working Linux install with Python and VLC.

Difficulty

Easy

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

There are plenty of ways to play audio files with Python. It really depends on your application, but the easiest way, by far, is to use the bindings for VLC to control VLC with Python, and play your files.

With VLC, you don’t need to worry about codecs and file support. It also doesn’t require too many complicated methods, and/or objects. So, for simple audio playback, VLC is best.

Read more

How to disable Bash shell commands history on Linux

Commands history is a great feature of the bash shell. However, there are times when it is best to disable it. One good example when you might prefer your bash shell commands history to be disabled is on the production server accessible from the external network where potential attacker might gain an access to your server and re-read your history file to search for useful commands, services in use or accidentally inserted passwords. Below you can find bunch of commands to help you disable history from being stored or how to remove all currently stored commands.

Where are history commands stored

All commands your enter on the shell are stored within your local directory into a file called .bash_history. This is a default history file defined by HISTFILE variable:

# echo $HISTFILE
/root/.bash_history

Read more

Tutorial on how to write basic udev rules in Linux

Objective

Understanding the base concepts behind udev, and learn how to write simple rules

Requirements

  • Root permissions

Difficulty

MEDIUM

Conventions

  • # – requires given linux commands to be executed with root privileges either
    directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

In a GNU/Linux system, while devices low level support is handled at the kernel level, the management of events related to them is managed in userspace by udev, and more precisely by the udevd daemon. Learning how to write rules to be applied on the occurring of those events can be really useful to modify the behavior of the system and adapt it to our needs.

Read more

How to automatically execute shell script at startup boot on systemd Linux

The following config will discuss a basic example on how to execute shell script during a boot time on systemd Linux. There maybe various reason why you might want to execute shell script during Linux startup like for example to start a particular custom service, check disk space, create a backup etc.

The following example below will serve as a basic template to be later modified to suit your specific needs. In the example below we will check a disk space of a /home/ directory during a boot time and write a report to /root/ directory.

Systemd service unit

First, we need to create a systemd startup script eg.disk-space-check.serviceand place it into /etc/systemd/system/ directory. You can find the example of such systemd startup script below:

[Unit]
After=mysql.service

[Service]
ExecStart=/usr/local/bin/disk-space-check.sh

[Install]
WantedBy=default.target

Read more