Skip to main content

Posts

Showing posts from June, 2022

Python Basics Read Text File and placed into variable

Python code below is a simple and basic code, that will open a text file in read only mode. Code below has been tested using Python3. It just demonstrates using a “print” statement to show the logic; how to open a file in read only mode and place the line that was read to a variable. The logic can be used on advance methods, such as replacing the line with IP Address and supplying a shutdown command to the IP Address. Here’s the code: #Open the file as Read Only with open('mysampledata.txt','r') as file_objHandler: #'mysampledata.txt' <--file name of the text file to be read #'r' <-- tell Python to open file as read only     #Read all lines in the file.     TxtLines = file_objHandler.readlines()     line_counter = 0   for Oneline in TxtLines:         #print(Oneline.strip())     line_counter += 1     print(line_counter, "<-- Line# / This is the data on the text file -->", Oneline)     #line_counter

Ping device using Python

Below is a simple Python code that accepts IP Address as input and check or ping the IP Address and shows an output whether its up or down. Some network disable ICMP request, so no matter how you try the system will not reply to the ping request. Code below is great when you're getting hands wet on simple Python script. Here's the code: #!/usr/bin/python3  import subprocess as subps  import sys  def ipcheck(check_ice):    status,result = subps.getstatusoutput("ping -c1 -w2 " + str(check_ice))    if status == 0:       print("Device " + str(check_ice) + " is UP!")       print(status,result)   else:       print("Device " + str(check_ice) + " is DOWN!") ipcheck(str(sys.argv[1]))  #================ Works great on Linux system. For Windows, run on WSL environment. Type: wsl to enable WSL Then change directory to: /mnt/c/Users/<username>/Documents  Or cd /mnt/c/<browse to where the python script is located> On the locatio