PoTrolls
20170109
la méthode banale pour connaitre l'adresse "internet" de votre réseau local est de communiquer avec un serveur qui vous retourne l'adresse dont il a reçu l'appel.
cela se fait simplement avec de nombreux serveurs web
exemples de réponses:
le site:
http://www.monip.org/
x
<!DOCTYPE html PUBLIC "-W3CDTD HTML 4.01 TransitionalEN">
<html>
<head>
<title>
MonIP?.org v1.0</title>
<META http-equiv="Content-type" content="text/html; charset=UTF-8">
</head>
<P ALIGN="center"><FONT size=8><BR>IP : 82.253.189.19<br></font><font size=3>
<i>lns-bzn-30-82-253-189-19.adsl.proxad.net</i>
<br></font><font size=1><br><br>Pas de proxy détecté - No Proxy detected
</font>
</html>
plus frustre:
http://antiguide.free.fr/MyPublicIp.php
IP:82.253.189.19
exemple de code Arduino pour récupérer l'adresse:
#include <ESP8266WiFi.h>
// wifi
#define SSID "lelac"
#define WIFIKEY ".........."
#define HOST "smtp.free.fr"
class TcpRxTx{
public:
bool begin(String h,int p, bool d){
host=h;
port=p;
dbg=d;
Serial.print("\n[Connecting to ... ");
Serial.print( host);
Serial.print(":");
Serial.println(port);
if (client.connect(host.c_str(), port))
{
Serial.println("connected]");
return true;
}
else
{
Serial.println("connection failed!]");
client.stop();
return false;
}
}
void put(String t){
client.println(t);
}
void stop(){
client.stop();
}
String rep(){
if (dbg) Serial.println("[Response:]");
while(true){
if (client.available())
{
String l=client.readStringUntil('\n');
if(dbg) Serial.printf("ligne lon: %d",l.length());
if (dbg) Serial.println(l);
return l;
} // else Serial.println("no client av");
} //while
}
;
private :
WiFiClient client;
String host;
bool dbg=true;
int port;
}; // Tcprxtx class
String myPublicIp(String host, String url, int port,String needle, int timeout, bool debug){
long start=millis();
TcpRxTx po ;
if (! po.begin(host,port,debug)){
Serial.println("ERROR");
return "";
;
}
po.put("GET "+url+" HTTP/1.0\r\nHOST: "+host+"\r\n");
//po.put("GET "+url+" HTTP/1.0\r\n\r\n");
String repget=po.rep();
// Serial.printf("lon rep get %d\n",repget.length());
// Serial.println(repget.c_str());
while ((millis()-start)<1000L*timeout){
String repget2=po.rep();
int xx=repget2.indexOf(needle);
if (debug) Serial.printf(" lon repget2 %d ip? at %d : ",repget2.length(),xx);
if (debug) Serial.println(repget2.c_str());
if (xx>=0 ) {
Serial.println("IP TROUVE");
int deb=xx+needle.length();
int fin=deb;
String car=".0123456789";
while (
(fin<=repget2.length() ) &&
(car.indexOf(repget2.substring(fin,fin+1)) >=0)
) fin=fin+1;
Serial.println(fin);
return repget2.substring(deb,fin);
}
}
// Serial.println("-----------------------");
po.stop();
// Serial.println("-----------------------");
return "";
}
void setup()
{
Serial.begin(74880);
Serial.printf("\nConnecting to wifi %s \n", SSID);
WiFi.begin(SSID, WIFIKEY);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
String myIp;
myIp=myPublicIp("antiguide.free.fr","/MyPublicIp.php",80,"IP:",10,false);
Serial.println (myIp);
myIp=myPublicIp("www.monip.org","/",80,"IP : ",10,false);
Serial.println (myIp);
}
void loop(){
// place here fast activity
}