def copy_file_and_exec(self, host): if not self.check_file(): self.msg = '{} Not Fount'.format(self.file_name) return False if not isinstance(host, list): raise ValueError() ip = host[0][0] port = host[0][1] user = host[0][2] # 这里已经推送玩publick key后就不再使用管理用户了,直接使用本机的Key登陆 with DBContext('r') as session: cmdb_key = session.query( SSHConfigs.id_rsa).filter(SSHConfigs.name == 'cmdb').first() if not cmdb_key: return False, '{}, 认证失败,cmdb key不存在'.format(ip) else: cmdb_key = cmdb_key[0] cmd = 'python /tmp/sysinfo.py' ssh_key_file = get_key_file(cmdb_key) if ssh_key_file: try: # res_info,获取到的资产信息, error_info res_info, error_info = remote_upload_file( ip, user, ssh_key_file, cmd, self.file_path, '/tmp/sysinfo.py', port) if res_info: self.msg = { 'status': True, 'data': { ip: json.loads(res_info) } } return self.msg else: # self.msg = '{}获取资产信息失败,错误信息:{}'.format(ip, error_info) self.msg = {'status': False, 'data': {ip: error_info}} return self.msg except paramiko.ssh_exception.AuthenticationException: # self.msg = False, '{}, 认证失败,请检查Key是否正确'.format(ip) self.msg = {'status': False, 'data': {ip: '认证失败,请检查Key是否正确'}} except Exception as e: print(e) return self.msg
def sync_key(self, server_list): """ 批量下发server端公钥到client端 :param server_list: 主机信息,IP端口用户密码 :return: """ if not isinstance(server_list, list): raise ValueError() ip = server_list[0][0] port = server_list[0][1] user = server_list[0][2] user_key = server_list[0][3] cmd = '[ ! -d ~/.ssh ] && mkdir ~/.ssh && chmod 700 ~/.ssh ; ' \ '[ ! -f ~/.ssh/authorized_keys ] && touch ~/.ssh/authorized_keys; ' \ 'grep -c "`cat /tmp/id_rsa.pub`" ~/.ssh/authorized_keys >> /dev/null;' \ '[ $? == 0 ] || cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys && echo ok' # 将key写到本地 ssh_key_file = get_key_file(user_key) if ssh_key_file: try: res = remote_upload_file(ip, user, ssh_key_file, cmd, PUBLIC_KEY, '/tmp/id_rsa.pub', port) if res[0] == 'ok': self.msg = { 'status': True, 'ip': ip, 'port': port, 'user': user, 'msg': '推送成功' } else: # 状态改为False self.msg = {'status': False, 'ip': ip, 'msg': '推送失败'} except paramiko.ssh_exception.AuthenticationException: self.msg = { 'status': False, 'ip': ip, 'msg': '认证失败,请检查管理用户Key是否正确' } except Exception as e: print(e) self.msg = {'status': False, 'ip': ip, 'msg': '{}'.format(e)} ins_log.read_log('info', self.msg) return self.msg
def sync_key(self, host): """ 批量下发server端公钥到client端 :param host: 主机信息,IP端口用户密码 :return: """ if not isinstance(host, list): raise ValueError() ip = host[0][0] port = host[0][1] user = host[0][2] user_key = host[0][3] cmd = '[ ! -d /root/.ssh ] && mkdir /root/.ssh ; ' \ '[ ! -f /root/.ssh/authorized_keys ] && touch /root/.ssh/authorized_keys; ' \ 'grep -c "`cat /tmp/id_rsa.pub`" ~/.ssh/authorized_keys >> /dev/null;' \ '[ $? == 0 ] || cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys && echo ok' # 将key写到本地 ssh_key_file = get_key_file(user_key) if ssh_key_file: try: # print('CMD-->',cmd) res = remote_upload_file(ip, user, ssh_key_file, cmd, PUBLIC_KEY, '/tmp/id_rsa.pub', port) # print('res--->',res) if res[0] == 'ok': # self.msg = True, '{}, 推送完成'.format(ip) self.msg = {'status': True, 'ip': ip, 'msg': '推送成功'} else: # 状态改为False # self.msg = False, '{}, 推送失败'.format(ip) self.msg = {'status': False, 'ip': ip, 'msg': '推送失败'} except paramiko.ssh_exception.AuthenticationException: # self.msg = False, '{}, 认证失败,请检查管理用户Key是否正确'.format(ip) self.msg = { 'status': False, 'ip': ip, 'msg': '认证失败,请检查管理用户Key是否正确' } except Exception as e: # self.msg = False, '{}, {}'.format(ip,e) self.msg = {'status': False, 'ip': ip, 'msg': '{}'.format(e)} # print(self.msg) return self.msg