from paramiko import SSHClient ##---------------------------------------------------------------------------------------## class SSH(object): ##---------------------------------------------------------------------------------------## ##-----------------------------------------------------------------------------------## def __init__(self, devices): ##-----------------------------------------------------------------------------------## self.devices = {} for device in devices: self.devices[device] = {} self.devices[device]['user'] = devices[device]['user'] self.devices[device]['ip'] = devices[device]['ip'] ##-----------------------------------------------------------------------------------## def command(self, data): ##-----------------------------------------------------------------------------------## for device in data: if device in self.devices: ip = self.devices[device]['ip'] user = self.devices[device]['user'] try: client = SSHClient() client.load_system_host_keys() client.connect(ip = ip, user = user) except: print("Server nicht erreichbar") finally: if(data[device]=="shutdown"): client.exec_command('poweroff') if(data[device]=="reboot"): client.exec_command('reboot') if(data[device]=="upgrade"): client.exec_command('apt update && apt upgrade -y') ##-----------------------------------------------------------------------------------## def config(self): ##-----------------------------------------------------------------------------------## config = { "device" : { "name" : "", "ip" : "", "user" : "" }, "shutdown" : { "device" : "name", "state" : "button" }, "reboot" : { "device" : "name", "state" : "button" }, "upgrade" : { "device" : "name", "state" : "button" } } return config ##-----------------------------------------------------------------------------------## def shutdown(self, device): ##-----------------------------------------------------------------------------------## self.client.exec_command('poweroff') ##-----------------------------------------------------------------------------------## def reboot(self, device): ##-----------------------------------------------------------------------------------## self.client.exec_command('reboot') ##-----------------------------------------------------------------------------------## def upgrade(self, device): ##-----------------------------------------------------------------------------------## self.client.exec_command('apt update && apt upgrade -y')