9. Python scripting in Linux

  • Python in Linux

  • Conda environment

  • Python programming environment: Jupyter notebook

  • Python libraries for interaction with Linux

  • Multiple account creation example

  • Python scripts in command line


9.1. Python in Linux

  • Python comes with Linux distro, however, the version maybe too old.

which python3

/usr/bin/python3

  • Python modules can be installed with apt, for example

sudo apt install python3-numpy
  • Alternatively, modules can be installed with pip3 command in the user’s home directory:

sudo apt install python3-pip

See installed modules:

pip3 list

Install module numpy:

pip3 install numpy

Check out the package description:

pip3 show numpy

Uninstall module numpy:

pip3 uninstall numpy

9.2. Conda environment (Exercise)

mkdir -p ~/miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm -rf ~/miniconda3/miniconda.sh

After installing, initialize your newly-installed Miniconda:

~/miniconda3/bin/conda init bash

Activate the Conda environment:

source .bashrc

See what Python is used now:

which python3

It should be in th Conda environment now.

To deactivate the Conda environment, run:

conda deactivate

To activate it again:

conda activate

Search Python modules:

conda search jupyter

9.3. Python programming environment: Jupyter notebook (Exercise)

Install Jupyter notebook on your desktop as user hostadm:

conda install jupyter

Assign the password to jupyter notes by running command:

jupyter notebook password

Create directory for python projects, and step into the directory:

mkdir Python
cd Python

Start jupyter notes by command below:

jupyter notebook &

Access jupyter notebook through the browser on your virtual desktop by navigating to URL below:

jupyter

http://localhost:8888

Start a new python3 project in the jupyter notebook.

This is a Jupyter Notebook.

This computational tool allows for Python programming in a web browser without having to use a multi-window environment. You can write and run your code all in one sitting.


9.4. Interaction with operating system

There are various python standard libraries utilized for interaction with the Linux environment, file systems, and processes.

We review only a few most commonly used:

  • sys — System-specific parameters and functions

  • os — Miscellaneous operating system interfaces

  • shutil - File operations

  • subprocess — Process management

  • pysftp - File transfer via SFTP

  • re - Regular expressions


9.5. Run Jupyter notebook in server mode (Exercise)

Check if jupyter is already running:

ps -ef | grep jupyter

If it shows up running without using port 8888, terminate it:

killall -9 jupyter-notebook

Start jupyter notebook in the server mode with listening on port tcp/8888:

cd Python
jupyter-notebook --no-browser --port=8888 --ip=* &

Find out the IP address and the port number on the Ravada server for your jupyter notebook by login to the vdi portal and starting the desktop.

Navigate the browser on your laptop or desktop to the IP address and port, for example

http://172.16.26.112:60000

Login with the password you assigned to the jupyter notebook in the previous exercises.


9.6. sys — System-specific parameters and functions - path, argv, … (Exercise)

Identify the OS platform within Python:

import sys
sys.platform

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

Example how it can be used:


if sys.platform.startswith('linux'):
    # Linux-specific code here...
    print('we are working in Linux environment')
else:
    print('different OS')

Get the current PYTHONPATH:

print(sys.path)

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

Tp see what Pythom we are using:

sys.executable

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

Get size of a file:

size_hosts = sys.getsizeof(/etc/hosts)
print(size_hosts)

Second input parameter for the python script:

sys.argv[2]

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

Check what methods are in sys:

dir(sys)

Press <shift>-<Enter> keys to execute the script in the jupyter cell.


9.7. os — Miscellaneous operating system interfaces - chdir, rmdir, system, … (Exercise)

In your Jupyter notebook, copy and paste the python code from the cell below:

#Current working directory
import os
os.getcwd()

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

Try the following in the new cell:

#Change the directory
os.chdir("/etc")
os.getcwd()

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

Try listing the files and directories in /etc:

# see the list of files and directories
os.listdir()

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

Check if file hosts exists:

os.path.isfile("hosts")

Press <shift>-<Enter> keys to execute the script in the jupyter cell. Check if file no_hosts exists:

os.path.isfile('no_hosts')

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

Get the file system statistics on a file

os.stat('/etc/hosts')

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

Get the directory path separator:

os.sep

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

Generate the file names in a directory tree by walking the tree either top-down or bottom-up.

top='/etc'
for dirpath, dirnames, filenames in os.walk(top):
    path = dirpath.split(os.sep)
    print(path)

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

Derive the directory path from a full path:

full_path="/etc/libvirt/qemu/kvm1.xml"
os.path.dirname(full_path)

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

Create, rename, and remove directories:

os.chdir('/home/hostadm')
os.mkdir('DIR1')
os.rename('DIR1','DIR2')
os.rmdir('DIR2')

Execute a system command without controlling the stdout and stderr:

os.system('cd /etc; pwd')

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

The output number is the last command error code.

Check what methods are included in os

dir(os)

Press <shift>-<Enter> keys to execute the script in the jupyter cell.


9.8. shutil - file operations - copy, chown, … (Exercise)

import shutil

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

Copy file /etc/hosts into list2.txt:

shutil.copy('/etc/hosts','list2.txt')

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

See what files we have in the current working directory:

os.listdir()

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

Copy directory tree /etc/network into directory NEW_DIR1

shutil.copytree('/etc/network','NEW_DIR1')

Press <shift>-<Enter> keys to execute the script in the jupyter cell.


9.9. Calling Linux commands from Python (Exercise)

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. Example of class call

# Create directory DIR1 and check the disk usage in the directory
import subprocess
s=subprocess.run("mkdir DIR1; du -sh DIR1", shell=True)

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

For full control of the standard input and the standart output to the command, we use class Popen:

import subprocess

s=subprocess.Popen("du -sh /etc", shell=True, 
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,)
    
stdout_value,stderr_value = s.communicate()

print("std output=", stdout_value) 
print("std error=", stderr_value) 

Press <shift>-<Enter> keys to execute the script in the jupyter cell.


9.10. Scripts runnable from command line (Exercise)

Choose what version of Python you want the script to use: either the system python, /usr/bin/python3, or Anaconda, /home/hostadm/miniconda3/bin/python3

Say, we want to use Anaconda python. Create, for example, file call.py with the first line containing:

#!/home/hostadm/miniconda3/bin/python3

The snippets from the jupyter cells can be placed in file call.py, for example:

#!/home/hostadm/miniconda3/bin/python3

# see the list of files and directories
import os
listf = os.listdir()
print(listf)

Make the file executable:

chmod a+x call.py

Run the script:

./call.py

9.11. Creating multiple user accounts with python (Exercise)

In directory Python, create a new file, users.txt, with editor nano:

users.txt:

mike 2000
jerry 2001
sam 2003
mary 2004

In jupyter notebook, run the following script:

import os

def user_cmd(user, uid, group, hdir):
    s = f'useradd -m -d {hdir}/{user} -s /bin/bash -u {uid} -g {group} {user}'
    return s
    
os.chdir('/home/hostadm/Python')
HDIR = '/NFS/home'

f = open('users.txt','r')

account = {}
for line in f:
    key, value = line.rstrip().split()
    account[key] = value

f.close() 

for keys in account:
    grp_cmd = f'groupadd -g {account[keys]} {keys}'
    print(grp_cmd)
    comm = user_cmd(keys, account[keys], account[keys], HDIR)
    print(comm)

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

Function print() above is a placeholder for function os.system() that would create accounts.


9.12. Transferring files with pysftp (Exercise)

On your desktop install pysftp:

conda install pysftp

Start run01 VM and find out its IP address:

virsh start run01
virsh domifaddr run01

For example, IP is 192.168.122.134

ssh to the VM to accept the public host key, and disconnect.

ssh 192.168.122.134
exit

In jupyter notebook, run the following steps to upload users.txt onto scripts VM. Put the correct credentials into username and password:

import pysftp
with pysftp.Connection('192.168.122.134', username='...', password='...') as sftp:
        sftp.put('/home/hostadm/Python/users.txt')  	

Press <shift>-<Enter> keys to execute the script in the jupyter cell.


9.13. Regular expressions (RE) for pattern search/replacement (Exercise)

Files can be read line by line into python lists. The lists can be searched for patterns.

Here we use re library with its methods.

Copy file list.txt from the shell scripting exercises into directory Python:

cd
cp bash_scripts/list.txt Python

Run the python script below to extract the lines containing “Reboot”, “reboot” or “support” strings (aka grep -E '[Rr]eboot|support' list.txt command):

import re

f=open('list.txt','r')
    
for line in f:
    match = re.search(r'[Rr]eboot|support', line)
    if match:
        print(line)
            
f.close()

Press <shift>-<Enter> keys to execute the script in the jupyter cell.

Substitute underscore for any number of spaces wherever they occur on the line (aka sed 's/ */_/g' list.txt):

import re

f=open('list.txt','r')
    
for line in f:
    newline = re.sub(r' *', r'_', line)
    print(newline)
        
f.close()

Press <shift>-<Enter> keys to execute the script in the jupyter cell.


9.14. Splitting lines into fileds (Exercise)

Display user names, home directories, and login shell (fields 1, 6 and 7) in /etc/passwd file.

Aka awk -F: '{ print $1, $6, $7 }' /etc/passwd command:

f=open('/etc/passwd','r')

for line in f:
    f1,f2,f3,f4,f5,f6,f7 = line.split(':')
    print(f1, f6, f7)
    
f.close()

Press <shift>-<Enter> keys to execute the script in the jupyter cell.


9.15. References

Python standard library