List,<br><br> First I'm very new to Python. I usually do this kind of thing with shell scripts, however, I'm trying to move to using python primarily so I can learn the language. I'm attempting to write a script that will check a server for various configuration settings and report and/or change based on those findings. I know there are other tools that can do this, but for various reasons I must script this. I have come up with two ways to accomplish what I'm trying to do and I'm looking for the more elegant solution -- or if there is another solution that I have not thought of. Any input would be greatly appreciated and thanks for your time.<br>
<br>method 1:<br>=============<br>[master script]<br><br>def execute_script(cscript):<br> process = subprocess.Popen(cscript, stdout=subprocess.PIPE, stderr=subprocess.PIPE)<br> output_data = process.communicate()<br><br>
if process.returncode == 0:<br> return "OK"<br> elif process.returncode == 90:<br> return "DEFER"<br> elif process.returncode == 80:<br> return "UPDATED"<br> else:<br> return "ERROR"<br>
<br>if __name__ == '__main__':<br><br> # Find all configuration scripts<br> print "Searching for config scripts"<br> cscripts = []<br> for config_script in os.listdir(BASE_DIR + "/config.d"):<br>
cscripts.append(BASE_DIR + '/config.d/' + config_script)<br> print "Found %d configuration scripts" % (len(cscripts))<br><br> # Loop through scripts and execute<br> for config_script in cscripts:<br>
action_title = "Checking %s" % (config_script[len(BASE_DIR + '/config.d/'):])<br> print "%-75.75s [ %s ]" % (action_title, execute_script(config_script))<br><br>[config script template]<br>
#!/usr/bin/env python<br><br>def check():<br> # do some checking<br> return results<br><br>return check()<br><br><br><br>method 2:<br>============<br>[master script]<br>if __name__ == '__main__':<br>
<br>
# Find all configuration scripts<br>
print "Searching for config scripts"<br>
cscripts = []<br>
for config_script in os.listdir(BASE_DIR + "/config.d"):<br>
cscripts.append(BASE_DIR + '/config.d/' + config_script)<br>
print "Found %d configuration scripts" % (len(cscripts))<br>
<br>
# Loop through scripts and execute<br>
for config_script in cscripts:<br> import config_script<br>
action_title = "Checking %s" % (config_script[len(BASE_DIR + '/config.d/'):])<br>
print "%-75.75s [ %s ]" % (action_title, config_script.check())<br><br><br>[config script template]<br>#!/usr/bin/env python<br><br>def check():<br> # do some stuff<br> return result # return str OK, DEFER, UPDATED or ERROR<br>
<br>