From d7875b953562eaf5e3fb423099e36e8ed2f66b23 Mon Sep 17 00:00:00 2001 From: Kai Wansart Date: Fri, 2 Feb 2018 20:30:04 +0100 Subject: [PATCH] ssh fix --- app/devices/ssh.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/app/devices/ssh.py b/app/devices/ssh.py index 34c567d..0cc9696 100644 --- a/app/devices/ssh.py +++ b/app/devices/ssh.py @@ -13,18 +13,25 @@ class SSH(object): self.devices[device]['ip'] = devices[device]['ip'] ##-----------------------------------------------------------------------------------## - def command(self, device, command): + def command(self, data): ##-----------------------------------------------------------------------------------## - self.client = SSHClient() - self.client.load_system_host_keys() - self.client.connect(self.devices[device]['ip'], self.devices[device]['user']) - - if(command=="shutdown"): - self.shutdown(device) - if(command=="reboot"): - self.reboot(device) - if(command=="upgrade"): - self.upgrade(device) + 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): @@ -53,17 +60,14 @@ class SSH(object): ##-----------------------------------------------------------------------------------## def shutdown(self, device): ##-----------------------------------------------------------------------------------## - self.connect() self.client.exec_command('poweroff') ##-----------------------------------------------------------------------------------## def reboot(self, device): ##-----------------------------------------------------------------------------------## - self.connect() self.client.exec_command('reboot') ##-----------------------------------------------------------------------------------## def upgrade(self, device): ##-----------------------------------------------------------------------------------## - self.connect() self.client.exec_command('apt update && apt upgrade -y') \ No newline at end of file