import paramiko from update.models import UpdateCommand from django.db.models import Q class UpdateCommandUtil: def __init__(self, cinema_ip): self.cinema_config = {'hostname': cinema_ip, 'port': 22, 'username': 'root', 'password': 'cine123456'} self.client = paramiko.SSHClient() self.model = UpdateCommand def connect(self): self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.client.connect(**self.cinema_config) def disconnect(self): self.client.close() def get_all_cmd(self): return self.model.objects.order_by().all() def get_sys_cmd(self): return self.model.objects.filter(is_sys='1').order_by(id).all() def get_no_sys_cmd(self): return self.model.objects.exclude(is_sys='1').order_by(id).all() def get_checked_cmd(self, cmd_list): sys_cmd = self.get_sys_cmd() checked_cmd = self.model.objects.filter(id__in=cmd_list).order_by(id).all() return sys_cmd + checked_cmd def exec_cmd(self, cmd_list, _release): exec_cmd_list = self.get_checked_cmd(cmd_list) for cmd in cmd_list: if '' in cmd: cmd = cmd.replace('', _release) exec_cmd_list.append(cmd) elif cmd.startswith(''): cmd = cmd.replace('', _release) for c in cmd.split('###'): exec_cmd_list.append(c) else: exec_cmd_list.append(cmd) self.connect() for exec_cmd in exec_cmd_list: stdin, stdout, stderr = self.client.exec_command(exec_cmd) print(stdout.read().decode('utf-8')) self.disconnect()