ansible_public/scripts/computer.py

53 lines
1002 B
Python
Raw Normal View History

2022-06-14 18:53:16 +02:00
# @brief
import os
import string
import subprocess
# Error thrower
def ThrowError(error_text):
raise Exception("[Error]: " + error_text)
def ExecAnsibleCommand(command):
output = os.popen(command).read()
if output.find("SUCCESS") != -1:
return 0
else:
return output
# Comupter informations
class Computer:
id = 0
name = ""
mac = "0000.0000.000"
status = False
logged_users = []
def __init__(self, name):
if name[:6] != "lab15-":
ThrowError("Bad computer name: '" + name + "'")
if (name[6:].isnumeric()) == False:
ThrowError("Bad computer number: '" + name[6:] +"'")
self.name = name
self.id = int(name[6:])
def GetStatus(self):
command = "ansible -i inventory/hosts.ini -m ping " + self.name + " -k"
if ExecAnsibleCommand(command):
return 0
return 1
# Tester
lab = Computer("lab15-10")
print(lab.GetStatus())