def announce_ip(): index = 0 while index < 200: print('++ attempting to announce ip: {}'.format(index)) try: ip_address = get_ip() current_connection = get_current_connection() print('++ found ip_address: {}'.format(str(ip_address))) print('++ found current_connection: {}'.format(str(current_connection))) routes = subprocess.check_output('route -n', shell=True) print('++ routes') print(routes) print('++ endroutes') if ip_address: telegram_log('@channel: its sar2d2: {} | {}'.format(str(ip_address), str(current_connection))) break except Exception as e: print(':/ error: {}'.format(str(e))) pass index += 1 time.sleep(1) # after we have connected, log some info about the connection if LOG_DETAILED_INFO: try: print('++++ logging detailed info') ifconfig = subprocess.check_output('ifconfig', shell=True) print('ifconfig: {}'.format(ifconfig)) iwget = subprocess.check_output('iwgetid', shell=True) print('iwget: {} | {}'.format(iwget, get_ip())) except Exception as e: print('warning: failed to log detailed info: {}'.format(str(e)))
def reboot(update, context): telegram_log('++ someone is rebooting sar2d2: {}'.format(update.effective_chat.id)) context.bot.send_message(chat_id=update.effective_chat.id, text="++ no problem dear, one sec... will take about a minute") try: write_reboot_id(update.effective_chat.id) except exception as e: telegram_log('++ failed to log to reboot_file: {}'.format(e.message)) pass os.system('/usr/bin/sudo /sbin/reboot')
def log_ip(ssh_str): try: telegram_log('ngrok is now connected: {}'.format(ssh_str)) except Exception as e: print('error logging ssh_str to telegram: {}'.format(e)) try: with open(ssh_file_path, "w") as ip_file: ip_file.write(ssh_str) except Exception as e: print('error logging ssh_str to file: {}'.format(e))
def check_for_new_who(): previous_users = [] while True: current_users = get_online_users() new_users = filter(lambda user: user not in previous_users, current_users) if new_users: for user in new_users: telegram_log('++ @channel: {user} just logged into oasis'.format( user=user )) previous_users = current_users time.sleep(5)
def open_ngrok_tunnel(): telegram_log('++ opening ngrok tunnel') # Open a tunnel on the default port 80 ngrok.set_auth_token(ngrok_token) public_url = ngrok.connect(port=22, proto="tcp") regex = '^tcp:\/\/(\d+\.tcp\.ngrok\.io)\:(\d+?)$' match = re.match(regex, public_url) if match: ssh_string = 'ssh swim@{} -p{}'.format(match.group(1), match.group(2)) log_ip(ssh_string) else: log_ip(public_url)
def test_ngrok_tunnel(debug_log=False): if debug_log: telegram_log('++ testing ngrok tunnel') with open(ssh_file_path) as f: ssh_url = f.read() if debug_log: telegram_log('testing ssh connection: {}'.format(ssh_url)) print(ssh_url) regex = 'ssh swim@(\S+) -p(\S+)' match = re.match(regex, ssh_url) if match: host = match.group(1) port = match.group(2) else: print('regex failed for ssh_url') return False s = pxssh.pxssh() if not s.login(host, 'swim', 'hello', port=port, sync_multiplier=5): if debug_log: telegram_log('++ ssh via ngrok failed') return False else: if debug_log: telegram_log('++ ssh via ngrok successful') s.logout() return True
def open_cmd(update, context): telegram_log('++ someone is opening the front door') context.bot.send_message(chat_id=update.effective_chat.id, text="++ no problem dear, will try to open the front door now") try: press_button() telegram_log("++ just tried to open the door, if it didn't work, wait a few seconds and try again, or you can try sending the message reboot and then try again") except Exception as e: telegram_log('++ failed to open the door: '.format(str(e))) context.bot.send_message(chat_id=update.effective_chat.id, text="++ oops, something went wrong")
def initiate_listener(): telegram_token = SECRETS_DICT['TELEGRAM_TOKEN'] updater = Updater(token=telegram_token, use_context=True) dispatcher = updater.dispatcher import logging logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) # attach handlers start_handler = CommandHandler('start', start) dispatcher.add_handler(start_handler) open_handler = CommandHandler('open', reboot) dispatcher.add_handler(open_handler) reboot_handler = CommandHandler('reboot', open_cmd) dispatcher.add_handler(reboot_handler) message_handler = MessageHandler(Filters.text, handle_message) dispatcher.add_handler(message_handler) # start polling telegram_log('++ starting telegram bot poller') updater.start_polling()
def write_reboot_id(t_id): reboot_log_file = SECRETS_DICT['REBOOT_LOG_FILE'] with open(reboot_log_file, 'w') as r_file: text = json.dumps({'t_id': t_id}) r_file.write(text) telegram_log('++ logged {} to reboot_file'.format(text))
def _log(msg): try: telegram_log(msg) except: print(msg)
GPIO.setup(servoPIN, GPIO.OUT) p = GPIO.PWM(servoPIN, 50) # GPIO 17 for PWM with 50Hz p.start(2.5) # Initialization try: p.ChangeDutyCycle(8.1) time.sleep(1.5) p.ChangeDutyCycle(2.5) time.sleep(0.5) finally: p.stop() GPIO.cleanup() time.sleep(1.0) if __name__ == '__main__': time.sleep(2) try: r_file_path = SECRETS_DICT['REBOOT_LOG_FILE'] if os.path.isfile(r_file_path): with open(r_file_path, 'r') as r_file: print('++ trying to load reboot_file {}'.format(r_file_path)) data_dict = json.loads(r_file.read()) t_id = data_dict['t_id'] send_telegram(chat_id=t_id, msg='++ trying to open the door now') telegram_log('++ tried to open after reboot for {}'.format(t_id)) os.remove(r_file_path) except: pass press_button()