def sync_node_time(cluster): hosts = C_Host.objects.filter( Q(project_id=cluster.id) & ~Q(name='localhost') & ~Q(name='127.0.0.1') & ~Q(name='::1')) data = [] times = [] result = {'success': True, 'data': []} for host in hosts: ssh_config = SshConfig(host=host.ip, port=host.port, username=host.username, password=host.password, private_key=None) ssh_client = SSHClient(ssh_config) res = ssh_client.run_cmd('date') gmt_date = res[0] GMT_FORMAT = '%a %b %d %H:%M:%S CST %Y' date = time.strptime(gmt_date, GMT_FORMAT) timeStamp = int(time.mktime(date)) times.append(timeStamp) show_time = time.strftime('%Y-%m-%d %H:%M:%S', date) time_data = { 'hostname': host.name, 'date': show_time, } data.append(time_data) result['data'] = data max = builtins.max(times) min = builtins.min(times) # 如果最大值减最小值超过5分钟 则判断有错 if (max - min) > 300000: result['success'] = False return result
def is_host_connected(ip, port, credential): config = SshConfig(host=ip, username=credential.username, password=credential.password, private_key=credential.private_key, port=port) client = SSHClient(config) return client.ping()
def to_ssh_config(self): return SshConfig( self.ip, self.port, self.username, self.password, self.private_key, )
def to_ssh_config(self): return SshConfig( self.ip, self.port, self.username, self.password, 10 )
def parse_host_to_ssh_config(host): return SshConfig(host.ip, host.port, host.username, host.password, host.private_key)