def send_list(message): if botname_checker(message): logger.write_log(message, logger.BLOB_TEXT) try: file = open("langlist.txt", "r") utils.bot.send_document( message.chat.id, file, message.id, locales.get_text(message.chat.id, "langList")) except FileNotFoundError: logging.warning("trying to re-create removed langlist file") interlayer.list_of_langs() if not os.path.isfile("langlist.txt"): utils.bot.reply_to( message, locales.get_text(message.chat.id, "langListRemakeErr")) return file = open("langlist.txt", "r") utils.bot.send_document( message.chat.id, file, message.id, locales.get_text(message.chat.id, "langList")) except Exception as e: logging.error("langlist file isn't available") logging.error(str(e) + "\n" + traceback.format_exc()) utils.bot.reply_to( message, locales.get_text(message.chat.id, "langListReadErr"))
def do_deal_raid(self,host_disk_num,host_raid_info): ''' 重做raid ''' if host_raid_info == "NA": return if host_raid_info =="raid-0": r_info = "0" elif host_raid_info =="raid-1": r_info = "1" elif host_raid_info =="raid-5": r_info = "5" elif host_raid_info =="raid-10": r_info = "10" cmd = "/usr/local/sbin/sogou-raid -t create -r %s -n %s" % (r_info,host_disk_num) result,error = self.ssh_con.do_remote_by_passwd_exec(cmd) if result == "wrong": logger.write_log("host: %s,do raid failed! detail: %s" % (self.host_ip,error)) return False else: return True
def data_handler(rawdata): global mongoNEST #currentSession = MyDataStore.getSession(rawdata['session']) #currentUser = MyDataStore.getUser(rawdata['session']) sessionId = rawdata['session']['sessionId'] userId = rawdata['session']['user']['userId'] print userId currentRequest = rawdata['request'] #Check for user in database if len([a for a in mongoNEST.find({'nest_amazonEchoID':userId})]) == 0: print "Need to add user into database" currentUser = {'_id':userId,'nest_amazonEchoID':userId,'authenticated':False} mongoNEST.update({'nest_amazonEchoID':userId},currentUser,True) #Check Timestamp of Request timestamp = currentRequest['timestamp'] if utils.datecheck(timestamp,5): response = request_handler(sessionId, userId, currentRequest) else: response = utils.alexaInvalidDate('Nest Control') if response is None: response = utils.alexaDidNotUnderstand('Nest Control') if debug: print json.dumps({"version":appVersion,"response":response},sort_keys=True,indent=4) logger.write_log(str(json.dumps({"version":appVersion,"response":response},sort_keys=True,indent=4))) return json.dumps({"version":appVersion,"response":response},indent=2,sort_keys=True)
def switch(userId, deviceId, state): ''' This is used to chnage the state of a switch. State = "ON" or "OFF" ot "TOGGLE" ''' global mongoST clientInfo = mongoST.find_one({'st_amazonEchoID': userId}) if state.lower() == "toggle": state = "OFF" if getSwitchState(clientInfo, deviceId) == "on" else "ON" switch_uri = clientInfo['st_api_location'] + clientInfo[ 'st_url'] + "/switch" switch_json = {"deviceId": deviceId, "command": state.lower()} switch_header = { "Authorization": clientInfo['st_token_type'] + " " + clientInfo['st_access_token'] } response = requests.post(switch_uri, headers=switch_header, json=switch_json) if debug: print "Switch Response: " + str(response.json()) logger.write_log("Switch Response: " + str(response.json())) return state if response.json( )['error'] == 0 else "Unknown Error. See Logs"
def translate_query(inline_query): lang = utils.extract_arg(inline_query.query, 0) try: inputtext = inline_query.query.split(' ', 1)[1].lstrip() except IndexError: return "Введите код языка и текст" logger.write_log("LOG: user " + logger.username_parser(inline_query) + " sent an INLINE: " + inputtext) if lang is None: return "Укажите код языка/название страны" try: translated = utils.translator.translate(inputtext, lang).text if translated == inputtext: return ( "Исходный и итоговый текст совпадают. Возможно, Google Api отклонил запрос. " "Если вы уверены, что так быть не должно, повторите попытку позже" ) return translated except Exception as e: if str(e) in "invalid destination language": return "Указан неверный код языка/название страны" else: logger.write_log("ERR: " + str(e) + "\n" + traceback.format_exc()) return ("Ошибка перевода. Обратитесь к авторам бота\n" "Информация для отладки сохранена в логах бота.")
def switch(userId,deviceId,state): ''' This is used to chnage the state of a switch. State = "ON" or "OFF" ot "TOGGLE" ''' global stData currentClient = stData.getUser(userId) clientInfo = currentClient.getClientInfo() if state.lower() == "toggle": state = "OFF" if getSwitchState(clientInfo, deviceId) == "on" else "ON" switch_uri = clientInfo.api_location + clientInfo.url + "/switch" switch_json = { "deviceId":deviceId, "command":state.lower() } switch_header = { "Authorization": clientInfo.token_type + " " + clientInfo.token } response = requests.post(switch_uri, headers=switch_header, json=switch_json) if debug: print "Switch Response: " + str(response.json()) logger.write_log("Switch Response: " + str(response.json())) return state if response.json()['error'] == 0 else "Unknown Error. See Logs"
def go(): n_classes = 4 subset = 'medium' fma_pca.init_mel(n_classes, subset=subset, reuse=True, pca_on=False) #fma_pca.init_mel(n_classes, subset=subset, reuse=True) test_id = 'Accuracy/5hr_' # for the log file name training_epochs = 100000000 batch_size = 700 minutes = 5 * 60 n_dim = fma_pca.n_dim() sd = 1 / np.sqrt(n_dim) learning_rate = .01 ############################################################################## test_id1 = '{}cnn_c4_medium'.format(test_id) net = cnn2.build_net([128, 128], n_classes, learning_rate) test_accuracy, train_accuracy, cost_hist, val_hist, epochs, time = train_net( net, test_id1, training_epochs, batch_size, minutes) logger.write_log(test_id1, n_classes, epochs, batch_size, time, learning_rate, subset, train_accuracy, test_accuracy, cost_hist, val_hist) show_plot(cost_hist, val_hist) show_plot(cost_hist, []) show_plot(val_hist, [])
def smartThingsToken(altId, userId, authCode): global stData currentClient = stData.getUser(userId) clientInfo = currentClient.getClientInfo() if debug: print 'ST Token: ' + userId logger.write_log('ST Token: ' + userId) token_uri = settings.auth_uri_2.replace('CODE',authCode).replace('CLIENTID',clientInfo.clientId).replace('CLIENTSECRET',clientInfo.clientSecret).replace('CALLBACK',quote(settings.callback_url + altId)) if debug: print 'Token URL: ' + token_uri logger.write_log('Token URL: ' + token_uri) response = requests.get(token_uri).json() print response clientInfo.setFromOauth(response) if debug: print "Response: " + str(response) logger.write_log("Response: " + str(response)) #Get Endpoints endpoints_params = { "access_token": clientInfo.token } if debug: print "Endpoints URL: " + str(clientInfo.api) logger.write_log("Endpoints URL: " + str(clientInfo.api)) response = requests.get(clientInfo.api, params=endpoints_params).json() #[0]['url'] print "Response: " print response response = response[0]['url'] if debug: print "Endpoints: " + str(response) logger.write_log("Endpoints: " + str(response)) clientInfo.url = response pickle.dump(stData,open(picklefile,"wb")) return True
def st_switch(userId, switchId, state): ''' This is used to chnage the state of a switch from SmartThings. State = "ON" or "OFF" ot "TOGGLE" ''' global stData currentClient = stData.getUser(userId) clientInfo = currentClient.getClientInfo() switches = clientInfo.switches selectedSwitch = [a for a in switches if a.lower() == switchId.lower()] if len(selectedSwitch) < 1: switch_uri = clientInfo.api_location + clientInfo.url + "/switch" switch_header = { "Authorization": clientInfo.token_type + " " + clientInfo.token } clientInfo.switches = requests.get(switch_uri, headers=switch_header).json() switches = clientInfo.switches if debug: print "Switchs: " + str(switches) logger.write_log(userId + ' - Switches: ' + str(switches)) selectedSwitch = [a for a in switches if a.lower() == switchId.lower()] if len(selectedSwitch) > 1: return "Too many switches matched the switch name I heard: " + switchId if len(selectedSwitch) < 1: return "No switches matched the switch name I heard: " + switchId selectedSwitch = selectedSwitch[0] return switch(userId,selectedSwitch,state)
def transfer_handler(cursor, method, p_list): write_log("dbHandler.py:\ttransfer_handler: " + str(method), "[DEBUG]") if method == AppConstants.GET_ALL: answer = get_transfer_list(cursor) elif method == AppConstants.GET_ONE: answer = get_transfer(cursor, p_list) elif method == AppConstants.GET_ACTIVE: answer = get_active_transfers(cursor) elif method == AppConstants.INSERT: answer = insert_transfer(cursor, p_list) elif method == AppConstants.DELETE: answer = delete_transfer(cursor, p_list) elif method == AppConstants.UPDATE_SIP_UUID_TRANSFER: answer = update_sip_uuid_transfer(cursor, p_list) elif method == AppConstants.UPDATE_STATUS_TRANSFER: answer = update_transfer_status(cursor, p_list) elif method == AppConstants.UPDATE_DELETE_DATE: answer = update_transfer_delete_date(cursor, p_list) elif method == AppConstants.COUNT_FAILED_TRANSFER: answer = count_failed_transfer(cursor, p_list) else: write_log( 'dbHandler.py\t\"' + str(method) + '\" is not a supported method!', "[ERROR]") return answer
def search(self, input, size): if input == 'all': res = helpers.scan(self.Es, query={"query": { "match_all": {} }}, index=self.Index, doc_type=self.Type) else: res = helpers.scan(self.Es, query=self.get_query(input), index=self.Index, doc_type=self.Type) result = dict() result_ids = [] for item in res: if len(result) == size: break result[item.get('_id')] = item result_ids.append(item.get('_id')) if len(result) == 0: return {"Items": 0} else: logger.write_log(input, result_ids[0:10]) return result
def get_machine_status(self): """ 获取服务器进程,无任何进程即可安装机器 """ dangerous_list = [] server_list = {} import re port = re.compile("(.*):(80|8080|3306|1521)(.*)") process = re.compile("(.*)(httpd|mysql|nginx|java|oracle)(.*)") process_cmd = "netstat -tnpl" result_id = "" result, error = self.ssh_con.do_remote_by_passwd_exec(process_cmd) if result == "wrong": logger.write_log("host: %s . get host info failed." % self.host_ip) buf = StringIO.StringIO(result) for line in buf.readlines(): line = line.strip() if line.startswith("Proto"): continue elif process.match(line): dangerous_process = process.match(line).group(2) dangerous_list.append(dangerous_process) server_list[self.host_ip] = dangerous_list return server_list
def detect(message): if not botname_checker(message): return inputtext = utils.textparser(message) if inputtext is None: logger.write_log(message, "none") return logger.write_log(message, inputtext) try: lang = interlayer.lang_list.get(interlayer.extract_lang(inputtext)) if locales.get_chat_lang(message.chat.id) != "en": translated_lang = " (" + interlayer.get_translate( lang, locales.get_chat_lang(message.chat.id)) + ")" else: translated_lang = "" utils.bot.reply_to( message, locales.get_text(message.chat.id, "langDetectedAs").format(lang, translated_lang)) except (interlayer.BadTrgLangException, interlayer.UnkTransException): utils.bot.reply_to(message, locales.get_text(message.chat.id, "langDetectErr"))
def download_file(service, file_id, sagah): request = service.files().get_media(fileId=file_id) fh = io.FileIO(sagah['tgt_path'] + file_id + '.xlsx', 'wb') downloader = MediaIoBaseDownload(fh, request) done = False while done is False: status, done = downloader.next_chunk() write_log('download_file ' + file_id + ' finished successfully', False)
def premium(message): if not botname_checker(message): return logger.write_log(message, logger.BLOB_TEXT) status_premium(message)
def rm_task(message): if not botname_checker(message): return logger.write_log(message, logger.BLOB_TEXT) module_rem_task(message)
def display_all(rep): """ Affiche tout le répertoire :param rep: Le répertoire qu'on veut afficher :return: Rien """ logger.write_log("display_all") for k in rep: print(rep[k])
def refresh_source_db(list_new_source): for key in list_new_source: for value in list_new_source[key]: success = db_handler(AppConstants.SOURCE, AppConstants.INSERT, value, key) if success: write_log("Insert in DB from " + str(key) + "/" + str(value) + " was successful", "[INFO]") else: write_log(str(key) + "/" + str(value) + " already exist in DB", "[DEBUG]") return
def create_base64_path(target_path): if target_path == "": path = str(AppConstants.SOURCE_PATH) else: path = target_path base_path = base64.b64encode( os.fsencode(TS_LOCATION_UUID) + b':' + os.fsencode(path)) write_log("apiHandler.py:\t" + str(base_path), "[DEBUG]") return base_path
def add_contact(rep, contact): """ Ajoute le contact dans le répertoire :param rep: Le répertoire dans lequel on veux mettre le contact :param contact: Le contact qu'on veut mettre dans le répertoire :return: Rien """ logger.write_log("add_contact") number = contact["Numéro"] rep[number] = contact
def compare_processing_file(procFile): diffPath = "bash " + str(AppConstants.HANDLER_PATH) + str(AppConstants.LOCAL_PROCESS_DIR) + \ "diffProcessing.sh " + str(AppConstants.HANDLER_PATH) + str(AppConstants.LOCAL_PROCESS_DIR) + procFile write_log("processingHandler.py:\t" + str(diffPath), "[DEBUG]") diff = subprocess.check_output(diffPath, shell=True) write_log("processingHandler.py:\t" + str(diff), "[DEBUG]") if len(diff) > 0: change_processing_file(procFile) return return
def del_relation_server(init_server_info): ''' 删除信任关系 ''' command = "sed -i '/root@tc_202_117/d' /root/.ssh/authorized_keys" result,error = deal_ssh.remote_ssh_password_exec(init_server_info,command) if result == "wrong": logger.write_log("删除key BIZOP_KEY 失败.") else: logger.write_log("删除key BIZOP_KEY 成功")
def delete_ingested_source(): done_dir = listdir(str(AppConstants.DONE_SOURCE_PATH)) for d_dir in done_dir: path = str(AppConstants.DONE_SOURCE_PATH) + "/" + str(d_dir) if isdir(path): if check_delete_dates(path, done_dir): rmtree(path) db_handler(AppConstants.CLEANING, None, d_dir) write_log("sourceHandler.py:\tDeleted " + str(path), "[DELETE]")
def process_response(r): if r.status_code == 200: write_log( "apiHandler.py:\tSuccess! - Status Code: " + str(r.status_code) + "Response: " + str(r.text), "[DEBUG]") else: write_log( "apiHandler.py:\tFailed! - Status Code: " + str(r.status_code) + "Response: " + str(r.reason), "[DEBUG]") return
def send_welcome(message): if botname_checker(message): logger.write_log(message, logger.BLOB_TEXT) chat_info = sql_worker.get_chat_info(message.chat.id) if not chat_info: chat_settings_lang(message, "start") return utils.bot.reply_to(message, locales.get_text(message.chat.id, "startMSG"))
def set_phrase(userId,phraseId): ''' This is used to chnage current phrase ''' print "NEW PHRASE" print phraseId global stData currentClient = stData.getUser(userId) clientInfo = currentClient.getClientInfo() phrases = clientInfo.phrases print phrases selectedPhrase = [a for a in phrases if a.lower().replace('!','') == phraseId.lower()] print selectedPhrase if len(selectedPhrase) < 1: phrase_uri = clientInfo.api_location + clientInfo.url + "/phrase" phrase_header = { "Authorization": clientInfo.token_type + " " + clientInfo.token } request = requests.get(phrase_uri, headers=phrase_header).json() clientInfo.phrases = request phrases = clientInfo.phrases if debug: print phrases logger.write_log(userId + " - Phrases: " + str(phrases)) selectedPhrase = [a for a in phrases if a.lower().replace('!','') == phraseId.lower()] if len(selectedPhrase) > 1: return "Too many phrases matched the phrase name I heard: " + phraseId if len(selectedPhrase) < 1: return "No phrase matched the phrase name I heard: " + phraseId selectedPhrase = selectedPhrase[0] phrase_json = { "phrase":selectedPhrase } phrase_header = { "Authorization": clientInfo.token_type + " " + clientInfo.token } phrase_uri = clientInfo.api_location + clientInfo.url + "/phrase" response = requests.post(phrase_uri, headers=phrase_header, json=phrase_json) if debug: print "Phrase Response: " + str(response.json()) logger.write_log("Phrase Response: " + str(response.json())) return phraseId if response.json()['error'] == 0 else "Unknown Error. See Logs"
def delete_transfer(cursor, transfer_id): cursor.execute(str(AppConstants.DELETE_TRANSFER), (transfer_id, )) if cursor.rowcount == 1: write_log( "dbHandler.py:\tdelete_transfer:\tDeletion was successful for transfer_id " + str(transfer_id), "[INFO]") return True else: write_log( "dbHandler.py:\tdelete_transfer:\tSomething went wrong: Rowcount = " + str(cursor.rowcount), "[ERROR]")
def get_contact(phone_number, rep): """ Retrouve le contact en fonction du numéro donné par l'utilisateur :param phone_number: Numéro du contact qu'on veut retrouver :param rep: Répertoire dans lequelle on veut chercher :return: Les coordonées du contact """ logger.write_log("get_contact") for k in rep: if phone_number == k: print(f"Le numéro {phone_number} correspond à : {rep[k]}")
def move_source_to_done(path, uuid): if is_ingest_complete(uuid): move(path, str(AppConstants.DONE_SOURCE_PATH)) write_log("sourceHandler.py:\tMoved " + str(path) + " to DONE Folder", "[INFO]") create_delete_date(path, uuid) else: write_log( "sourceHandler.py:\tIngest " + str(uuid) + " is not completed", "[DEBUG]") return
def do_remote_by_passwd_exec(self,command): ''' 默认账号密码 ''' try: stdin,stdout,sterr = self.ssh.exec_command(command,timeout = 15) result = stdout.read() error = sterr.read() except Exception,e: logger.write_log(e) return "wrong",e
def create_delete_date(path, uuid): delete_date = (datetime.now() + timedelta(days=30)).strftime('%Y-%m-%d %H:%M:%S') db_handler(AppConstants.TRANSFER, AppConstants.UPDATE_DELETE_DATE, delete_date, uuid) write_log("sourceHandler.py:\t" + str(delete_date), "[DEBUG]") delete_name = "delete_" + str(path).split("/")[-1] + "_" + str(delete_date) write_log("sourceHandler.py:\t" + str(delete_name), "[DEBUG]") subprocess.call( ['touch', (str(AppConstants.DONE_SOURCE_PATH) + "/" + delete_name)]) return
def get_source_list(cursor): cursor.execute(str(AppConstants.ALL_SOURCES)) tuple_list = cursor.fetchall() if len(tuple_list) > 0: s_list = list(tuple_list) write_log("dbHandler.py:\tget_source_list:\t" + str(tuple_list), "[DEBUG]") return s_list else: write_log("dbHandler.py:\tget_source_list:\t" + str(tuple_list), "[DEBUG]") return tuple_list
def restart_transfer_api_db(t_uuid): failed_transfer = db_handler(AppConstants.TRANSFER, AppConstants.ONE_TRANSFER_UUID, t_uuid) failed_counter = db_handler(AppConstants.TYPE_TRANSFER, AppConstants.COUNT_FAILED_TRANSFER, failed_transfer[1]) if failed_counter <= 2: start_transfer_api(failed_transfer) else: failed_source = db_handler(AppConstants.SOURCE, AppConstants.ONE_SOURCE_ID, failed_transfer[1]) path_of_failed_source = str(AppConstants.SOURCE_LIST[failed_source[2]]) + "/" + str(failed_source[1]) if exists(path_of_failed_source): write_log("Ingest " + t_uuid + " failed too often. Please check source!", "[FAILED]") move(path_of_failed_source, str(AppConstants.FAILED_SOURCE_PATH)) return
def delete_source(cursor, source_id): cursor.execute(str(AppConstants.DELETE_SOURCE), (source_id[0], )) if cursor.rowcount == 1: write_log( "dbHandler.py:\tdelete_source:\tDeletion was successful for source_id " + str(source_id), "[INFO]") return True else: write_log( "dbHandler.py:\tdelete_source:\tSomething went wrong: Rowcount = " + str(cursor.rowcount), "[ERROR]") return False
def run_cmd(header, cmd, logfd): sys.stdout.write(header + " ... ") sys.stdout.flush() cmdstr = "# " + ' '.join(cmd) logger.write_log(logfd, cmdstr) rc = execute_log(logfd, cmd) if (rc == 0): print "done" else: print "failed" return rc
def set_mode(userId,modeId): ''' This is used to chnage current mode ''' global stData currentClient = stData.getUser(userId) clientInfo = currentClient.getClientInfo() modes = clientInfo.modes selectedMode = [a for a in modes if a.lower() == modeId.lower()] if len(selectedMode) < 1: mode_uri = clientInfo.api_location + clientInfo.url + "/mode" mode_header = { "Authorization": clientInfo.token_type + " " + clientInfo.token } #get list of modes clientInfo.modes = requests.get(mode_uri, headers=mode_header).json() modes = clientInfo.modes if debug: print modes logger.write_log(userId + " - Modes: " + str(modes)) selectedMode = [a for a in modes if a.lower() == modeId.lower()] if len(selectedMode) > 1: return "Too many modes matched the mode name I heard: " + modeId if len(selectedMode) < 1: return "No modes matched the mode name I heard: " + modeId selectedMode = selectedMode[0] mode_json = { "mode":selectedMode } mode_header = { "Authorization": clientInfo.token_type + " " + clientInfo.token } mode_uri = clientInfo.api_location + clientInfo.url + "/mode" response = requests.post(mode_uri, headers=mode_header, json=mode_json) response = requests.post(mode_uri, headers=mode_header, json=mode_json) if debug: print "Mode Response: " + str(response.json()) logger.write_log("Mode Response: " + str(response.json())) return modeId if response.json()['error'] == 0 else "Unknown Error. See Logs"
def control_key(self,init_server_info,tag): ''' 添加信任关系 ''' BIZOP_KEY = '''ssh-dss AAAAB3NzaC1kc3MAAACBAO+yhNAkOZbhvK+CTx1zn6C0CfabaoAZLd2P4OZBi1GR+5S3HUnn9DkfMFXWfvrTBgyUJYWzg76Ymp4hlsZFLV/TR0yhPdIPWYSqAXgerzhx8u3TxxPVpgXzGlXSV424a+6n5XzeQFfj59DvKV4Y1KZqIQNngVkxtDecq2j4SfVfAAAAFQD5YJHwbyzzoV1TUNkNSvZo81ZQ8QAAAIEA19htkFN3vxdrEF571Jt0ACxFBx4xwsrrcbsyrPvJdqxhM85X6EREepIAGs5ronv2y//09WDs/APpoGU/jHwEdhBqXDUJehRzqEbLuY44hLk/DqWiXcrS4mkILqEWqsd5KDG6P1YwG5ezRslf9mrfi9zkGpPmNNATbkB7IMiCvDYAAACBAJcDObyRlj11hBvykYvdA3MU5ywRI8I8w+ldLSoYD3gZ7jVM+dF7H1x5uJFdbi2rXbceJfwABBVQwCh/yamgh+QfjgMoFeJ3/famFd8kqNsw8Y34s4eAVUVv/Xus6KX3OTIiqJjeD1T3pxYKCRLfuATuDSGQAmNhfMikj68w/JGz root@tc_202_117''' OP_KEY = '''from=\\"10.147.239.235,10.137.239.235,10.146.239.235,10.136.239.235,10.147.239.1,10.137.239.1,10.146.239.1,10.136.239.1\\",no-agent-forwarding,no-port-forwarding ssh-dss AAAAB3NzaC1kc3MAAACBAPx3767ksyO+E+L6fmIKJ+2Uq6yyyk3F83DQ2J+BLZgkzJG6K9FaoFLJQa+iLu3eL9ik+8/oNYcv96dL4M7tZRrQy0swBzIRlEhVRSMN7Ptiu+2TfNfgujA4PVPIvjPqVcbal1frEIy7VHQHSuVMwMI/6edd6J9FAo9CPHnsIlSdAAAAFQCFzOzFUZyUP9cOD+ubopSb+j3z0wAAAIBYXQOIRHmxk0hlwh13seetRtrkNYp1QGkaSLu8KvSr3cmGAUSndqxPVgvL5xT/C3S+sABB4H5KGpxlqTmqNn2MWM+oX4HBmsKXzslxIp0tlwqUE4DWaNvCEiKBqEBnWM+QTlSZ5C0kTJl+Os4rfEYC46R0bhHhYxc6NnpNyYv6JwAAAIBktn02O6k+Tg6CwN1RcG+RMZcqwZaUJ/kgDha3Ho9CZCcC5mNiC36M1qGW0J47RoEe5vSAsTitBgyr3pPEZp5+pnJjaXVo3uRWVtsAodDoWG0dOqYEIeX03VqrFQrt3SBsezPrpKoxZgItSbR6XnkK42iSrgk/f6Eyd3ckbh5a7Q== root@tc_239_235''' if tag == "add": command = "mkdir -p /root/.ssh && echo \"%s\" > /root/.ssh/authorized_keys && chmod 644 /root/.ssh/authorized_keys" % BIZOP_KEY result,error = deal_ssh.remote_ssh_password_exec(init_server_info,command) if result == "wrong": logger.write_log("%s 添加key BIZOP_KEY 失败." % init_server_info['client_server']['client_ip']) else: logger.write_log("%s 添加key BIZOP_KEY 成功." % init_server_info['client_server']['client_ip']) command = "echo %s >> /root/.ssh/authorized_keys" % OP_KEY result,error = deal_ssh.remote_ssh_password_exec(init_server_info,command) if result == "wrong": logger.write_log("%s 添加key OP_KEY 失败." % init_server_info['client_server']['client_ip']) else: logger.write_log("%s 添加key OP_KEY 成功." % init_server_info['client_server']['client_ip'])
def get_system_hostname(self,host_ip): ''' 获取服务器主机hostname ''' string = "" _command = 'hostname -f' result,error = self.ssh_con.do_remote_by_passwd_exec(_command) if result == "wrong": logger.write_log("host: %s 获取hostname 失败." % host_ip) else: logger.write_log("host: %s 获取hostname 成功." % host_ip) return result
def getSwitchState(clientInfo, deviceId): switch_uri = clientInfo['st_api_location'] + clientInfo['st_url'] + "/switch" switch_json = { "deviceId":deviceId } switch_header = { "Authorization": clientInfo['st_token_type'] + " " + clientInfo['st_access_token'] } response = requests.get(switch_uri, headers=switch_header, json=switch_json).json() print response if debug: print "Switch Response: " + str(response) logger.write_log("Switch Response: " + str(response)) return response['switch']
def check_tool_file(self): ''' 检查硬盘工具是否存在 ''' test_filename_command = "test -f /opt/MegaRAID/MegaCli/MegaCli64 && echo \"success\"" result,error = self.ssh_con.do_remote_by_passwd_exec(test_filename_command) if result == "wrong": logger.write_log("%s %s" % (self.host_ip,error)) return False elif result == "": logger.write_log("host:%s MegaCli64 is not exsit." % self.host_ip) return False else: return True
def smartThingsAuth(altId, userId, clientId, clientSecret): global stData currentClientUserId = userId currentClient = stData.getUser(userId) clientInfo = currentClient.getClientInfo() clientInfo.clientId = clientId clientInfo.clientSecret = clientSecret if debug: print 'ST Auth: ' + currentClientUserId logger.write_log('ST Auth: ' + currentClientUserId) auth_uri = settings.auth_uri_1.replace('CLIENTID',clientId).replace('CALLBACK',quote(settings.callback_url + altId)) if debug: print 'Auth URL: ' + auth_uri logger.write_log('Auth URL: ' + auth_uri) return auth_uri
def get_system_hostname(host_info): ''' 获取服务器主机hostname ''' string = "" _command = 'hostname -f' result,error = deal_ssh.remote_ssh_key_exec(host_info,_command) if result == "wrong": logger.write_log("host: %s 获取hostname 失败." % host_info['client_server']['client_ip']) else: logger.write_log("host: %s 获取hostname 成功." % host_info['client_server']['client_ip']) return result
def smartThingsAuth(altId, userId, clientId, clientSecret,clientEmail): global mongoST clientInfo = mongoST.find_one({'st_amazonEchoID':userId}) clientInfo['st_clientId'] = clientId clientInfo['st_clientSecret'] = clientSecret clientInfo['st_clientEmail'] = clientEmail if debug: print 'ST Auth: ' + userId logger.write_log('ST Auth: ' + userId) auth_uri = settings.auth_uri_1.replace('CLIENTID',clientId).replace('CALLBACK',quote(settings.callback_url + altId)) if debug: print 'Auth URL: ' + auth_uri logger.write_log('Auth URL: ' + auth_uri) mongoST.update({'st_amazonEchoID':userId},clientInfo,True) return auth_uri
def set_mode(userId,modeId): ''' This is used to chnage current mode ''' global mongoST clientInfo = mongoST.find_one({'st_amazonEchoID':userId}) if 'st_modes' not in clientInfo: st_update_users_modes(userId) clientInfo = mongoST.find_one({'st_amazonEchoID':userId}) modes = clientInfo['st_modes'] selectedMode = [a for a in modes if a.lower().replace('!','') == modeId.lower()] if len(selectedMode) < 1: st_update_users_modes(userId) selectedMode = [a for a in modes if a.lower().replace('!','') == modeId.lower()] if len(selectedMode) > 1: return "Too many modes matched the mode name I heard: " + modeId if len(selectedMode) < 1: return "No modes matched the mode name I heard: " + modeId else: selectedMode = selectedMode[0] if debug: print selectedMode mode_json = { "mode":selectedMode } mode_header = { "Authorization": clientInfo['st_token_type'] + " " + clientInfo['st_access_token'] } mode_uri = clientInfo['st_api_location'] + clientInfo['st_url'] + "/mode" response = requests.post(mode_uri, headers=mode_header, json=mode_json) response = requests.post(mode_uri, headers=mode_header, json=mode_json) if debug: print "Mode Response: " + str(response.json()) logger.write_log("Mode Response: " + str(response.json())) return modeId if response.json()['error'] == 0 else "Unknown Error. See Logs"
def set_phrase(userId,phraseId): ''' This is used to chnage current phrase ''' global mongoST clientInfo = mongoST.find_one({'st_amazonEchoID':userId}) if 'st_phrases' not in clientInfo: st_update_users_phrases(userId) clientInfo = mongoST.find_one({'st_amazonEchoID':userId}) phrases = clientInfo['st_phrases'] selectedPhrase = [a for a in phrases if a.lower().replace('!','') == phraseId.lower()] if len(selectedPhrase) < 1: st_update_users_phrases(userId) selectedPhrase = [a for a in phrases if a.lower().replace('!','') == phraseId.lower()] if len(selectedPhrase) > 1: return "Too many phrases matched the phrase name I heard: " + phraseId if len(selectedPhrase) < 1: return "No phrase matched the phrase name I heard: " + phraseId else: selectedPhrase = selectedPhrase[0] if debug: print "Phrase: " + selectedPhrase ## Push chnages to REST API phrase_json = { "phrase":selectedPhrase } phrase_header = { "Authorization": clientInfo['st_token_type'] + " " + clientInfo['st_access_token'] } phrase_uri = clientInfo['st_api_location'] + clientInfo['st_url'] + "/phrase" response = requests.post(phrase_uri, headers=phrase_header, json=phrase_json) if debug: print "Phrase Response: " + str(response.json()) logger.write_log("Phrase Response: " + str(response.json())) return phraseId if response.json()['error'] == 0 else "Unknown Error. See Logs"
def get_vm_memory_info(self): ''' 获取虚机内存相关数据 ''' cmd = "free -m |grep \"Mem\" | awk '{print $2}'" templist = [] #分区信息 result,error = self.ssh_con.do_remote_by_passwd_exec(cmd) memory_size = result.strip() memory_info = {"memory_size":math.ceil(float(memory_size)/1024.000),"memory_type":"NULL","memory_speed":"NULL"} templist.append(memory_info) memory_info = {} if self.save_memory_info(templist): logger.write_log("host: %s save_memory_info success!" % self.host_ip) else: logger.write_log("host: %s save_memory_info failed!" % self.host_ip)
def smartThingsToken(altId, userId, authCode): global mongoST clientInfo = mongoST.find_one({'st_amazonEchoID':userId}) if debug: print 'ST Token: ' + userId logger.write_log('ST Token: ' + userId) token_uri = settings.auth_uri_2.replace('CODE',authCode).replace('CLIENTID',clientInfo['st_clientId']).replace('CLIENTSECRET',clientInfo['st_clientSecret']).replace('CALLBACK',quote(settings.callback_url + altId)) if debug: print 'Token URL: ' + token_uri logger.write_log('Token URL: ' + token_uri) response = requests.get(token_uri).json() print response #clientInfo.setFromOauth(response) oauthResponse = response clientInfo['st_access_token'] = oauthResponse['access_token'] clientInfo['st_token_type'] = oauthResponse['token_type'] clientInfo['st_scope'] = oauthResponse['scope'] clientInfo['st_expires_in'] = oauthResponse['expires_in'] clientInfo['st_api'] = settings.api.replace('CLIENTID',str(clientInfo['st_clientId'])) clientInfo['st_api_location'] = settings.api_location if debug: print "Response: " + str(response) logger.write_log("Response: " + str(response)) #Get Endpoints endpoints_params = { "access_token": clientInfo['st_access_token'] } if debug: print "Endpoints URL: " + str(clientInfo['st_api']) logger.write_log("Endpoints URL: " + str(clientInfo['st_api'])) response = requests.get(clientInfo['st_api'], params=endpoints_params).json() #[0]['url'] print "Response: " print response response = response[0]['url'] if debug: print "Endpoints: " + str(response) logger.write_log("Endpoints: " + str(response)) clientInfo['st_url'] = response clientInfo['authenticated'] = True mongoST.update({'st_amazonEchoID':userId},clientInfo,True) return True
def check_machine_status(self): """ 查询数据库,未分配的服务器 """ sql = "select server_status from server_info where host_data_ip = '%s' or host_busi_ip = '%s'" result_id = super(Deal_machine_status, self).select_advanced(sql, self.host_ip, self.host_ip) if len(result_id) == 0: return True if result_id[0] == None: return True if len(result_id) > 0: if int(result_id[0]) == 1: logger.write_log("Machine: %s has used! Please check it!" % self.host_ip) return False else: return True else: return True
def save_password(self,host_ip,raw_pass,encrypt_pass): ''' 保存密码 ''' user = "******" _command = 'echo \"%s\" | passwd %s --stdin' % (raw_pass,user) self.ssh_con.do_remote_by_passwd_exec(_command) #保存密码临时文件 f = open('/tmp/machine.list.pass','a+') f.write(host_ip) f.write(",") f.write(encrypt_pass) f.write("\n") f.close() #保存密码到数据库 sql = "update server_info set root_password = '******' WHERE (host_busi_ip = '%s' or host_data_ip = '%s')" super(PassWorder,self).update_advanced(sql,encrypt_pass,host_ip,host_ip) logger.write_log("%s change password sucess." % host_ip)
def change_server_password(self,new_password,server_info): ''' 改变服务器密码 ''' user = server_info['client_server']['client_user'] _command = 'echo \"%s\" | passwd %s --stdin' % (new_password,user) result,error = deal_ssh.remote_ssh_key_exec(server_info,_command) if result == "wrong": logger.write_log("%s change password failed." % server_info['client_server']['client_ip']) return False else: # f = open('/tmp/machine.list.pass','a+') # f.write(server_info['client_server']['client_ip']) # f.write(",") # f.write(new_password) # f.write("\n") # f.close() # print "%s change password sucess." % server_info['client_server']['client_ip'] logger.write_log("%s change password sucess." % server_info['client_server']['client_ip']) return True
def add_hostname(self): ''' 添加hostname 清理原有hostname ''' remote_user = self.client_info['client_user'] remote_passwd = self.client_info['client_password'] remote_port = self.client_info['client_port'] inner_domain = "sogou-in.domain" look_hosts_cmd = "cat /etc/hosts" look_network_cmd = "cat /etc/sysconfig/network" short_host_name = "" hostname_list = [] for host_ip in self.host_ip_list: if common_lib.get_idc_name(host_ip.strip()): idcname,host_busi_ip,host_data_ip = common_lib.get_idc_name(host_ip) else: logger.write_log("host: %s,get_idc_name failed." % host_ip) return False #hostname信息 result = deal_ssh.remote_ssh_password_simple_online(host_ip,remote_user,remote_passwd,look_network_cmd) if isinstance(result,bool): logger.write_log("host: %s,get host info failed." % host_ip) return False buf = StringIO.StringIO(result) for line in buf.readlines(): line = line.strip() if line.startswith("HOSTNAME"): short_host_name = line.split("=")[1] hostname = short_host_name + "." + inner_domain p1 = re.compile("(.*)%s()" % short_host_name) #hostname信息 result = deal_ssh.remote_ssh_password_simple_online(host_ip,remote_user,remote_passwd,look_hosts_cmd) if isinstance(result,bool): logger.write_log("host: %s,get host info failed." % host_ip) return False buf = StringIO.StringIO(result) for line in buf.readlines(): line = line.strip() if p1.match(line): delete_old_cmd = "sed -i /%s/d /etc/hosts" % short_host_name deal_ssh.remote_ssh_password_simple_online(host_ip,remote_user,remote_passwd,delete_old_cmd) #添加hostname add_hostname = "%s %s %s" % (host_data_ip,hostname,short_host_name) add_hostname_cmd = "echo -e \"%s\" >> /etc/hosts" % add_hostname deal_ssh.remote_ssh_password_simple_online(host_ip,remote_user,remote_passwd,add_hostname_cmd) hostname_list.append(hostname) return hostname_list
def install_soft_programe(self,host_ip,remote_info): ''' 安装软件 ''' client_port = int(remote_info['client_port']) default_user = remote_info['client_user'] default_client_password = remote_info['client_password'] scripts_dir = os.path.join(os.path.split(os.path.dirname(__file__))[0],'scripts') src_file = "%s/%s" % (scripts_dir,"init_env.sh") dst_file = "/usr/local/src/init_env.sh" self.trans_file(host_ip,client_port,default_user,default_client_password,src_file,dst_file) cmd_remote = "bash %s" % (dst_file) result,error = self.ssh_con.do_remote_by_passwd_exec(cmd_remote) if result == "wrong": logger.write_log("host: %s,Install Soft failed." % host_ip) else: logger.write_log("host: %s finish soft install" % host_ip)
def data_handler(rawdata): global MyDataStore logger.write_log(str(json.dumps(rawdata,sort_keys=True,indent=4))) currentSession = MyDataStore.getSession(rawdata['session']) currentUser = MyDataStore.getUser(rawdata['session']) currentRequest = rawdata['request'] timestamp = currentRequest['timestamp'] if dc.datecheck(timestamp,5): response = request_handler(currentSession, currentUser, currentRequest) else: response = AlexaInvalidDate() if response is None: response = AlexaDidNotUnderstand() if debug: print json.dumps({"version":appVersion,"response":response},sort_keys=True,indent=4) logger.write_log(str(json.dumps({"version":appVersion,"response":response},sort_keys=True,indent=4))) return json.dumps({"version":appVersion,"response":response},indent=2,sort_keys=True)
def clean_ca_cert(self,remote_hostname,ca_info): ''' 清理CA证书 ''' clean_cmd_all = "" ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ca_host = ca_info['ca_host_ip'] ca_user = ca_info['ca_host_user'] ca_pass = ca_info['ca_host_password'] clean_cmd = "puppet cert -c %s;" % remote_hostname clean_cmd_all += clean_cmd try: ssh.connect(hostname = ca_host, username = ca_user, password=ca_pass) stdin,stdout,stderr = ssh.exec_command(clean_cmd_all) result = stdout.read() error = stderr.read() except Exception,e: logger.write_log("host: %s,%s" % (host,e)) return False
def switch(userId,deviceId,state): ''' This is used to chnage the state of a switch. State = "ON" or "OFF" ot "TOGGLE" ''' global mongoST clientInfo = mongoST.find_one({'st_amazonEchoID':userId}) if state.lower() == "toggle": state = "OFF" if getSwitchState(clientInfo, deviceId) == "on" else "ON" switch_uri = clientInfo['st_api_location'] + clientInfo['st_url'] + "/switch" switch_json = { "deviceId":deviceId, "command":state.lower() } switch_header = { "Authorization": clientInfo['st_token_type'] + " " + clientInfo['st_access_token'] } response = requests.post(switch_uri, headers=switch_header, json=switch_json) if debug: print "Switch Response: " + str(response.json()) logger.write_log("Switch Response: " + str(response.json())) return state if response.json()['error'] == 0 else "Unknown Error. See Logs"
def add_hostname(self,host_ip): ''' 添加hostname 清理原有hostname ''' inner_domain = "sogou-in.domain" look_hosts_cmd = "cat /etc/hosts" look_network_cmd = "cat /etc/sysconfig/network" short_host_name = "" hostname_list = [] if common_lib.get_idc_name(host_ip.strip()): idcname,host_busi_ip,host_data_ip = common_lib.get_idc_name(host_ip) else: logger.write_log("host: %s,get_idc_name failed." % host_ip) return False #hostname信息 result,error = self.ssh_con.do_remote_by_passwd_exec(look_network_cmd) if result == "wrong": logger.write_log("host: %s,get host info failed.Detail: %s" % host_ip,error) return False buf = StringIO.StringIO(result) for line in buf.readlines(): line = line.strip() if line.startswith("HOSTNAME"): short_host_name = line.split("=")[1] hostname = short_host_name + "." + inner_domain p1 = re.compile("(.*)%s()" % short_host_name) #hostname信息 result,error = self.ssh_con.do_remote_by_passwd_exec(look_hosts_cmd) if result == "wrong": logger.write_log("host: %s,get host info failed.Detail: %s" % host_ip,error) return False buf = StringIO.StringIO(result) for line in buf.readlines(): line = line.strip() if p1.match(line): delete_old_cmd = "sed -i /%s/d /etc/hosts" % short_host_name self.ssh_con.do_remote_by_passwd_exec(delete_old_cmd) #添加hostname add_hostname = "%s %s %s" % (host_data_ip,hostname,short_host_name) add_hostname_cmd = "echo -e \"%s\" >> /etc/hosts" % add_hostname self.ssh_con.do_remote_by_passwd_exec(add_hostname_cmd) return hostname