记录远程Python脚本在100台机器上的执行并将结果发送回公共日志文档



我自学了python的工作(这是一个正在进行的工作),我最近的脚本被包装成一个可执行文件。长话短说,我将在全国各地的数百台个人电脑上运行它。它移动一些文件,编辑其中的信息,检查pc的许可证等,并在每一步打印语句。

这些pc都是彼此相同的图像(减去几个关键数据位),但都在自己独特的网络上。我正在寻找一种方法,让我的python脚本发送(日志)打印语句回一个共同的文档。这将使我能够梳理上述文档,并确定哪些pc没有成功执行我正在努力完成的所有任务。

到目前为止,我已经考虑过使用谷歌表和谷歌api(与gsread或其他库)简单地发送回我想要的信息,并将其记录为公共电子表格中的新行。但它让我想知道:

有没有一种更优雅、更像蟒蛇的方法来解决这个问题,我还没有想过。再一次,我是新手,并尽我最大的努力去学习/解释我的问题,所以非常感谢所有的建议。提前感谢!TLDR:在不同网络上的数百台几乎相同的pc需要运行相同的脚本。我想把它们的执行记录在一个共同的文档中,这样我就可以查看并确定我需要仔细查看哪些计算机。

import csv
import os
# define func to find serial number and set to var serial
def get_sn():
command = "wmic bios get serialnumber"
return os.popen(command).read().replace("n","").replace("  ","").replace(" ","").replace("SerialNumber","")
serial = get_sn()
# bring csv data into python dict
with open("marine_data.csv", mode='r') as infile:
reader = csv.reader(infile)
writer = csv.writer(infile)
data = {rows[0]:rows[1] for rows in reader}
# set woo license equal to value pair for serial key and send success message
if data.get(serial) is not None:
license = data[serial]
print("SUCCESS: Match Found For Serial " + serial)
# if key does not exist set woo license equal to generic val and send failure message
else:
license = "generic-T1"
print("FAILURE: No Match Found For Serial " + serial)
infile.close()
# define vars for use in editing set file
user_profile = os.environ['USERPROFILE']
output = os.path.join(user_profile, 'Documents', 'some-program', 'global.set')
file_in = open("global.set", "rt")
file_out = open(output, "wt")
# replace generic license id
for line in file_in:
file_out.write(line.replace('generic-T1', license))
file_in.close()
file_out.close()

你可以有一个TCP或HTTP服务器,监听特定端口上的日志事件,在你控制的服务器上,让各种pc上的代码通过网络连接到它,并将他们的日志事件发送给它,然后它存储到一个公共的地方。关于客户端,请参阅SocketHandler或HTTPHandler,以及使用TCP/IP的服务器端代码示例。

最新更新