Skip to content

Instantly share code, notes, and snippets.

@nicolasegp
Created August 4, 2020 00:39
Show Gist options
  • Save nicolasegp/d5822626a61f35e43474d24b87598b95 to your computer and use it in GitHub Desktop.
Save nicolasegp/d5822626a61f35e43474d24b87598b95 to your computer and use it in GitHub Desktop.
Avisa cuando llega el Internet vía Telegram
#!/bin/python3
import time
import curses
import socket
import requests
import subprocess
host = '1.1.1.1'
chat = '*****'
token = '*****:**********'
inicio = time.time()
def fecha(t):
return time.strftime('%I:%M:%S %p (%d.%m.%Y)', time.localtime(t))
def tiempo(t):
return time.strftime('%Hh %Mm %Ss', time.gmtime(t))
def main():
try:
screen.addstr(1, 1, 'Revisando IP: ' + host)
screen.addstr(2, 1, 'Tiempo transcurrido: 00h 00m 00s')
screen.refresh()
while True:
try:
socket.setdefaulttimeout(3)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, 53))
except (socket.error, IOError):
screen.addstr(2, 1, 'Tiempo transcurrido: ' + tiempo(time.time()-inicio) + ' ')
pass
else:
fin = time.time()
duracion = tiempo(fin-inicio)
requests.post(
'https://api.telegram.org/bot{}/sendMessage'.format(token),
data = {
'chat_id': chat,
'parse_mode': 'Markdown',
'text': '*Llegó el Internet*\n' \
'`• {}`\n' \
'`• {}`\n' \
'`• {}`'.format(fecha(inicio), fecha(fin), duracion)
}
)
subprocess.call([
'notify-send',
'-t',
'0',
'-i',
'package-install',
'Llegó el Internet',
'• {}\n• {}\n• {}\n'.format(fecha(inicio), fecha(fin), duracion)
])
print()
print(' • Inicio: ' + fecha(inicio))
print(' • Fin: ' + fecha(fin))
print(' • Duracion: ' + duracion)
print()
break
finally:
screen.refresh()
finally:
curses.endwin()
if __name__ == "__main__":
screen = curses.initscr()
curses.noecho()
curses.cbreak()
try:
main()
except (KeyboardInterrupt, SystemExit) as ex:
print(' Error( {} )'.format( ex.__class__.__name__ ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment