AWS 128-[PF]-Lab - System administration with Python

#AWS 128-[PF]-Lab - System administration with Python
Check out my GitHub Repository - github.com/MFMKURIA/AWS-reStr...
Exercise 1: Using os.system()
1. Importing the os Module:
```python
import os
```
- This line imports the built-in Python module `os`, which provides a way of using operating system-dependent functionality.
2. Running a Bash Command:
```python
os.system("ls")
```
- `os.system()` is used here to execute the Bash command `ls`, which lists the contents of the current directory.
Exercise 2: Using subprocess.run()
1. Importing the subprocess Module:
```python
import subprocess
```
- The `subprocess` module provides more powerful ways to manage and communicate with system processes than `os.system()`.
2. Running a Bash Command with subprocess.run():
```python
subprocess.run(["ls"])
```
- `subprocess.run()` executes the Bash command `ls` in a subprocess. It takes a list of arguments where the first element is the command and subsequent elements are its arguments (if any).
Exercise 3: Using subprocess.run() with Two Arguments
1. Running `ls -l` Command:
```python
subprocess.run(["ls", "-l"])
```
- Here, `subprocess.run()` is used to execute the `ls -l` command, which lists the directory contents in a long listing format (`-l` flag).
Exercise 4: Using subprocess.run() with Three Arguments
1. Running `ls -l README.md` Command:
```python
subprocess.run(["ls", "-l", "README.md"])
```
- This line demonstrates how to use `subprocess.run()` with three arguments: `ls`, `-l` (long listing format), and `README.md` (specific file name). It lists detailed information about the `README.md` file.
Exercise 5: Retrieving System Information
1. Running `uname -a` Command:
```python
command = "uname"
commandArgument = "-a"
subprocess.run([command, commandArgument])
```
- Here, `subprocess.run()` is used to run the `uname -a` command, which prints system information including kernel version, hostname, architecture, etc.
Exercise 6: Retrieving Information About Disk Space
1. Running `ps -x` Command:
```python
command = "ps"
commandArgument = "-x"
subprocess.run([command, commandArgument])
```
- This code runs the `ps -x` command using `subprocess.run()`, which lists information about currently running processes. The `-x` option shows all processes, not just those associated with the current terminal.
General Notes:
- subprocess.run() Arguments: Each exercise demonstrates different ways to use `subprocess.run()`. The function allows flexibility in passing command line arguments, capturing output, setting working directory, and more.
- Output Interpretation: The outputs shown in each exercise correspond to the expected results of running these commands in a Linux environment. They showcase various functionalities and options available when interfacing with the operating system from Python.
By following these exercises, you gain practical experience in using Python to perform system administration tasks, bridging the gap between scripting and administrative tasks in a Linux environment.

Пікірлер