Skip to main content

Posts

Python example to use zip built-in function

Recent posts

Python example using multiprocessing module

With the era of multi-core CPU, parallel, concurrency or multiprocessing is quite possible now. Below is an example of a Python code that shows how to use multiprocessing module to run multiple commands. import multiprocessing import subprocess # Shell command/s Function def multi_run_command(command): try: result = subprocess.run(command, shell=True, capture_output=True, text=True) return f"Command: {command}\nOutput:\n{result.stdout}" except Exception as e: return f"Command: {command}\nError: {e}" if __name__ == "__main__": # List of dig commands to run / Replace the commands as desired commands = ["dig @1.1.1.1 google.com", "dig @8.8.4.4 yahoo.com", "dig @9.9.9.9 bing.com"] # Create and specify limit of pool worker processes with multiprocessing.Pool(processes=3) as pool: #limit to 3 cores or workers results = pool.map(multi_run_command, commands) #use the m...

Python Fabric module run local commands

Fabric is a Python library that simplifies the use of SSH for system administration tasks by running remote commands on remote system. It can be used as well to run local commands on the system. Example code below shows on how to use Fabric module to execute local commands. Below is an example on how to use Fabric. from fabric import Connection # Create a connection to the localhost connx = Connection('localhost') # Run local commands def run_local_commands(): print(f"Executing on {connx.host} as {connx.user}") # Command 1: Get the system name result_uname = connx.local("uname -s", hide=True) print("Output of 'uname':") print(result_uname.stdout) # Command 2: Get memory on the system result_mem = connx.local("free -h", hide=True) print("Output of 'system memory':") print(result_mem.stdout) # Command 3: Display ip route result_ipr = connx.local("ip r", ...

Python subprocess run local commands

Python subprocess module is quite helpful in running local commands. While Fabric module can also be used to run local commands, Fabric module is overkill to run local commands on the system. Fabric module is quite useful in running commands on remote system via SSH. Whereas subprocess mnodule is quite ideal to execute local commands on the VM, device or local server. Python code example below to run local commands with try exception to capture any errors. Code below will run the "xz" command to zip the file on the specified path. xz command will replace the specified file as an xz file. xz file won't create a new file. import subprocess try: # Run the xz command subprocess.run(['xz', '/tmp/app_log.log'], check=True) print("File successfully compressed.") except subprocess.CalledProcessError as e: print(f"Error: Command failed with exit status {e.returncode}") except FileNotFoundError: print("Error: xz comma...

Notepad++ browse to all opened notes

Notepad++ comes in handy when taking quick notes, reading readme files, or just simply browsing all notepad or text files on the system. However, if multiple tabs or notes are open it can be overwhelming to go back to the previous tab or notes. In Notepad++ version 8.7.1, there's a drop down arrow button on top right of the Notepad++ Window which will display all the notes that are currently opened. And simply click the notes to make it active as the current window. Image below shows the drop down button on the top right of Notepad++ window. Having a descriptive name of notes when saved will be helpful using the above method. Since by clicking the title or the name of the notes it will be shown as the current window. There's another method also that can be used to browse or select notes. Notepad++ provides also a left and right button to move the current window. The left and right button to navigate between notes is also on the top right window of Notepad++ and is just...

Basic Python ask user to continue or exit

In a user interactive program there are times that a program might need to ask the user to proceed or exit to determine whether the program needs to continue or not. For this a prompt is necessary, and ask a user to decide and press some keys and depending on the user input, it will determine whether the code or program will continue running or not. Below is a simple Python code that will ask a user to press y or n. The code only shows how to detect the key that was pressed, of course the logic or the code can be inserted to other existing Python codes. Python code snippet, works in Python 3: import sys print("Make a choice press [y] to continue else [n] to exit") proceed_or_exit = input() if proceed_or_exit == 'y' or proceed_or_exit == 'Y': print("Y was pressed") #put some code here pass elif proceed_or_exit == 'n' or proceed_or_exit == 'N': print("N was pressed") #put some code here if necessary ...

PowerShell kill all notepad services or using pid

It's easy to fall into the habit of using multiple Notepad windows for various note-taking and reading needs. The convenience of having separate windows for different types of information can be surprisingly addictive. At the end of the day, you might have quite a few notepad and closing them one by one is not practical or ideal. Scripting or using PowerShell comes in handy in such scenario. Example of a PowerShell code snippet to kill all running notepad on the system. Get-Process -Name "*notepad*" | Stop-Process Above code snippet, get all notepad processes and is pipe to stop-process that will close the notepad. To just list the process of all running processes on the system, run this PowerShell code snippet. Get-WmiObject -Class Win32_Process | select "ProcessName", "ProcessID" To kill or close the process using a PID, run the command beloww: Stop-Process -Id 4365 -Force Or use -whatif parameter to see what will happen. Example: stop-pr...

PowerShell check Windows OS version home or professional

Windows OS version also comes in different editions or releases. The edition that has always been a part of Windows OSes releases are Home or Professional version. Home version is good for personal use, as it name suggested that its a Home version. Pro or Professional version is ideal if the laptop or device is used in a company or corporate environment. Professional version has the ability to join a domain or the device can be enrolled to an Active Directory. Windows 11 also comes with SE version, which is aim for low-end devices sold in the education market. There are other editions or releases such as enterprise, workstation and others. To get or check the Windows OS version via PowerShell, run the following PowerShell code snippet. Example: (Get-WmiObject -Class Win32_OperatingSystem).Caption Sample output: Microsoft Windows 11 Home gwmi -class Win32_OperatingSystem | select caption Sample output: caption ------- Microsoft Windows 11 Home Look forward what is bey...