I am very new to python but not programming. I am trying to automate a ‘certification check’ with regards to new servers but have a few roadblocks due to my newness here. In a nutshell I need to run my script on Server A (utility server) and have it retrieve the information of the target server (newly built server) and write it all out to the file. It works to get all the local information but how do I point it at the target server? Code pasted below.
Import the needed module
import os
import platform
import sys
import psutil
“”"
The class LoggingPrinter allows the displaying of messages and
sending them off to a log file from within the code ratehr than
the back hole of the stdout. it does this by using the stdout
and an open file.
“”"
class LoggingPrinter:
def init(self, filename, xt):
self.out_file = open(filename, xt)
self.old_stdout = sys.stdout
# this object will take over stdout
's job
sys.stdout = self
# executed when the user does a `print`
def write(self, text):
if text:
self.old_stdout.write(r'Console' + text + '\n')
self.out_file.write(text)
# executed when `with` block begins
def __enter__(self):
return self
# executed when `with` block ends
def __exit__(self, type, value, traceback):
# we don't want to log anymore. Restore the original stdout object.
sys.stdout = self.old_stdout
uname = platform.uname()
directory1 = r’c:\Geeks\forGeeks\dingGeeks\liking stuff’
target1 = r"C:\Paskiet\Python\logs\PythonTestFile4.txt"
path1 = r’c:\Paskiet\Python\logs’
file1 = r"\PythonTestFile4.txt"
isPathExist = os.path.isdir(path1)
isFileExist = os.path.isfile(path1+file1)
os.makedirs(directory1, mode=0o777, exist_ok=isPathExist)
print (isPathExist)
print (isFileExist)
get server/system information
with LoggingPrinter(target1, ‘wt’):
print (f"Machine: {uname.machine}")
print (f"Node: {uname.node}")
print (f"CPU Count: {psutil.cpu_count()}")
print(f"Version: {sys.version}")
with LoggingPrinter(target1, ‘at’):
print(“Here is a new line to print and log.”)
print(“Checking whatever needs to be checked in this section.”)
print(“Entering section of program that will be logged.”)
with LoggingPrinter(target1, “at”):
print(“I’ve got a lovely bunch of coconuts.”)
print(“Exiting logged section of program.”)