efelinto

Enviar informações do IP automático - Raspberry Pi

May 18th, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. #Importando módulo Pushbullet
  5. from pushbullet import Pushbullet
  6.  
  7. api_key = "" # Coloque a sua API KEY aqui
  8.  
  9. pb = Pushbullet(api_key)
  10.  
  11. import os
  12. import socket
  13.  
  14. # Analisando e pegando o IP da LAN
  15. if os.name != "nt":
  16.     import fcntl
  17.     import struct
  18.  
  19.     def get_interface_ip(ifname):
  20.         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  21.         return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s',
  22.                                 ifname[:15]))[20:24])
  23.  
  24. def get_lan_ip():
  25.     ip = socket.gethostbyname(socket.gethostname())
  26.     if ip.startswith("127.") and os.name != "nt":
  27.         interfaces = [
  28.             "eth0",
  29.             "eth1",
  30.             "eth2",
  31.             "wlan0",
  32.             "wlan1",
  33.             "wifi0",
  34.             "ath0",
  35.             "ath1",
  36.             "ppp0",
  37.             ]
  38.         for ifname in interfaces:
  39.             try:
  40.                 ip = get_interface_ip(ifname)
  41.                 break
  42.             except IOError:
  43.                 pass
  44.     return ip
  45.  
  46. ip = get_lan_ip()
  47.  
  48. # Mensagem a ser enviada para o Raspberry Pi
  49. saudacao = 'Raspberry Pi tem uma mensagem para você!'
  50. mensagem = 'O IP que estou utilizando nesta LAN é o ' + ip
  51.  
  52. pb.push_note(saudacao , mensagem)
Advertisement
Add Comment
Please, Sign In to add comment