Skip to main content

Posts

Showing posts with the label Programming

Android Studio view XML code layout

View XML code in Android Studio. By default, in Android Studio Narwhal | 2025.1.1 Patch 1 what is shown is the layout of the XML. Android Studio, provides 3 options for previewing the XML. 1 - View by code Only 2 - View with XML code and the output or the layout 3 - View only the output of the XML or view the design only To select these options once an XML or layout file is opened. The options can be found on the top right of the screen, just beside the current XML file that os being viewed. See this screenshot below on how it looks like: Hope it helps. Life is a gift. Life is a journey. Trust the Almighty, Pray always.

Python example to use zip built-in function

Python code below use zip built-in function to display the list and display the sequence. The code below just display the list, but using subprocess or fabric module it can be expanded to connect or do remote tasks on the specified servers. Here's an example code on how to use zip built-in function. def main_func(): xservers = ["WebServer", "MailServer", "MonitoringServer", "KibanaServer", "DatabaseServer", "TestServer", "NetboxServer"] xips = ["192.168.25.1", "192.168.25.2", "192.168.25.3", "192.168.25.4", "192.168.25.5", "192.168.25.6", "192.168.25.7"] for indexx, itemx in enumerate(zip(xservers, xips), start=1): print(indexx, itemx[0], "=", itemx[1], " ==> VM with platform services") if __name__ == "__main__": main_func() itemx[0] ===> refers to xservers itemx[1...

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...

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 ...

Python Basics - creating virtual environment and activating

 Creating virtual environment in Python is quite important thing to know if you are venturing to the world of Python Programming. Virtual Environment in Python may sound complicated at first if you haven't played around it. Let's delve into it. Why do we need a virtual environment? When we can just installed Python directly to the system. One good thing for creating Python virtual environment, is you can have multiple environments on Python on your system and also different versions. If 2 or 3 people are using the same server or computer, it won't be ideal that each person will use same environment and mess up the work of the other person. And also if your coding is good for a specific version of Python, the syntax may not work on new versions of Python and this is where virtual environment comes into the scenario. How do we create a virtual environment in Python? It's quite straight forward, here's the command. Of course this assumes that Python has been installed ...

Python basic if else and or comparison code

Comparing input or variables is a quite a common task to do when creating programs. In Python it can be done in a lot of ways, this example code below shows how it’s being done using “if else” and “or” statement. Here’s the example code below: #Get user input requires Python 3 input1 = input("1. Type something to test this out : ") input2 = input("2. Type something to test this out : ") input3 = input("3. Type something to test this out : ")   #variable initialization is important or else it will result to NameError: name 'variable_name' is not defined checkinput1 = 0 checkinput2 = 0 checkinput3 = 0 noinput = 1   #int(input1) convert the input1 string to an integer if (int(input1) > 40):   checkinput1 = "1" elif ( int(input2) > 40):   checkinput2 = "1" elif ( int(input3) > 40):   checkinput3 = "1" else:   noinput = 0   if (checkinput1 or checkinput2 or checkinput3 ==...

Python if else pass or continue after evaluating input

Beginning in Python programming? If else statement is quite a basic statement in any programming language. If else is used in evaluating conditions or basically, it will tell the program what to do next if a specified match or input is encountered. In if else statement, depends on the conditional statement that is set then the next logic will determine on what action or execution will be performed. For example, after a match pattern occurs then the next statement will execute an action but sometimes after a match occurs you don't want to do anything instead the else statement will show what action or logic to be executed. In Python "continue" statement does not mean that it will continue to execute the code after the "continue" statement, instead it will break that execution and goes to the top of the code and continue the next course of action. However, "pass" statement in Python will mean literal "pass". So, if "pass" is...

Use PowerShell in Excel VBA

VBA in Excel is very helpful since it can run things without any human intervention, or technically it can run task and automate things and just get the result. VBA coupled with PowerShell can even be more interesting. Of course, there is always some drawback or pros and cons. Bad actor can take advantage of VBA and PowerShell to run malicious software on user’s computer. For most users who are not aware or doesn’t believe that VBA and PowerShell can be used to steal data, one common reaction is; Is it possible? Or you are just trying to exaggerate and scare people? As the odds say, to see is to believe. Or to see it in action is one thing and trying to educate users is another thing. Cyber Security is a task that everyone should be a part of, a chain is useless if one its link is weak. Which is basically, true in digital world. The company may spend thousands of moneys on Firewall, Anti-Virus and other devices or software to thwart attack but just a simple click on a Phishin...

Check Windows Task Scheduler Status and Last Run Time

Task Scheduler is an awesome tool that every Sys Admin should be familiar with it. Task Scheduler in a way can help automate task, if the task is repetitive or a task that has to be done at a certain date and time, or a task has to be done once a week, once a month or even once a year. Task Scheduler is the best tool for this scenario. In this busy world of digital technology, who can keep remembering that a task must be done on a specific date and time. Yes, it might works once but as time goes on and task or jobs of a Sys Admin keep filing up chances are the task might be forgotten and the person-in-charge might just remember the things that needs to be done if there is an issue that happens already. So, Task Scheduler is a tool in which you test once, set if tested okay and forget about it. Test to make sure that the task to be set, works properly and just forgot about it; it will do the task repetitively albeit depends on the settings that was set. Of course, Task Scheduler...

Monitor disk drive space using PowerShell

Monitoring space is quite crucial in a critical system, or basically checking the disk space whether there is enough free space for continual operation is a good thing to consider in a production environment. A simple PowerShell script can save the day by monitoring the drive space of a specific drive that needs to be checked or monitored. It doesn’t need a complex tool to do this kind of task. Here’s a one-liner PowerShell script to check free space on C drive. Get-WmiObject Win32_LogicalDisk -filter "DeviceID = 'c:' " | Select-Object   { $_ . FreeSpace / 1GB }   "DeviceID = 'c:' " = this can be changed to any drive letter Output of the above command: $_.FreeSpace /1GB -----------------   293.042289733887   Or to include the existing size of the drive, the command can be tweaked like this: Get-WmiObject Win32_LogicalDisk -filter "DeviceID = 'c:' " | Select-Object   { $_ . FreeSpace / 1GB } , { $...