Last active
August 29, 2015 13:58
-
-
Save juangesino/10077466 to your computer and use it in GitHub Desktop.
Postea la IP interna de la RPi a una pagina web! (usando Python + PHP + MySQL)La RPi ejecuta send_internal_ip.py cada vez que arranca, este archivo manda por variable GET la ip de la RPi a un archivo PHP alojado en un dominio. El archivo recibe esta variable GET y actualiza una base de datos MySQL. Al mismo tiempo si la variable no esta definida…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$dbc = mysql_connect('HOSTNAME','USERNAME','PASSWORD'); | |
if (!$dbc){ | |
die(mysql_error()); | |
} | |
$db_selected = mysql_select_db('DATABASE',$dbc); | |
if(!$db_selected){ | |
die(mysql_error()); | |
} | |
$result = mysql_query("SELECT `FIELD` FROM `TABLE_NAME`"); | |
$row = mysql_fetch_array($result); | |
$old_ip = $row['FIELD']; | |
if(isset($_GET['VAR_NAME']) && !empty($_GET['VAR_NAME'])){ | |
$new_ip = $_GET['VAR_NAME']; | |
if($new_ip != $old_ip){ | |
if(mysql_query("UPDATE `TABLE_NAME` SET `FIELD` = '$new_ip'")){ | |
die(); | |
} | |
} | |
}else{ | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>RPi's IP</title> | |
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>So you want the IP of the RPi?</h1> | |
<h3>Here it is: <?php echo $old_ip ?></h3> | |
</div> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> | |
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> | |
</body> | |
</html> | |
<?php | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 | |
import time | |
import socket | |
import os | |
time.sleep(240) | |
if os.name != "nt": | |
import fcntl | |
import struct | |
def get_interface_ip(ifname): | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', | |
ifname[:15]))[20:24]) | |
def get_lan_ip(): | |
ip = socket.gethostbyname(socket.gethostname()) | |
if ip.startswith("127.") and os.name != "nt": | |
interfaces = [ | |
"eth0", | |
"eth1", | |
"eth2", | |
"wlan0", | |
"wlan1", | |
"wifi0", | |
"ath0", | |
"ath1", | |
"ppp0", | |
] | |
for ifname in interfaces: | |
try: | |
ip = get_interface_ip(ifname) | |
break | |
except IOError: | |
pass | |
return ip | |
ip = get_lan_ip() | |
address = "http://MISITIO.com/internal_ip.php?VAR_NAME=" + ip | |
page = urllib2.urlopen(address).read() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment