Come conoscere il proprio indirizzo IP pubblico
Home › Forum › Discussioni su problematiche generali › Come conoscere il proprio indirizzo IP pubblico
- Questo topic ha 1 risposta, 1 partecipante ed è stato aggiornato l'ultima volta 2 anni, 1 mese fa da
Sergio Bertana.
-
AutorePost
-
Marzo 17, 2023 alle 4:47 pm #70973
Anonimo
InattivoStò utilizzando dei servizi di Dynamic DNS gestiti dal router ma non sempre l’aggiornamento dell’indirizzo IP avviene in tempi rapidi.
Volevo sapere se c’è la possibilità da programma PLC SlimLine di conoscere l’indirizzo IP pubblico del router a cui è connesso (nel caso specifico un WLINK WL-R100x).
Marzo 17, 2023 alle 4:49 pm #70975Sergio Bertana
Amministratore del forumSe hai la necessità di conoscere l’indirizzo IP pubblico con il quale il tuo sistema SlimLine esce su Internet hai due possibilità.
Tutti i router supportano la registrazione presso servizi di Dynamic DNS, quindi puoi registrarti e poi eseguendo il FB DNSRequest effettuare la richiesta di risoluzione URL.
Ma se non vuoi appoggiarti a servizi di DNS dinamico puoi connetterti a siti che offrono gratuitamente il servizio di segnalazione indirizzo IP, ad esempio ipify.org. Inviando una richiesta API al sito ti verrà inviato in risposta una stringa con il tuo indirizzo IP pubblico, ecco il listato del programma HTTPIPIfy che esegue la richiesta. Attivando da debug la variabile Request nel buffer Page ti verrà ritornato il tuo indirizzo IP pubblico.
PROGRAM HTTPIPIfy VAR i : UDINT; (* Auxiliary variable *) Request : BOOL; (* Request command *) FlipFlop : BOOL; (* Program Flip/Flop *) Errors : UDINT; (* Execution errors *) Page : STRING[ 64 ]; (* Page buffer *) TCPClient : SysTCPClient; (* TCP client management *) HTTPRq : HTTPClient_v4; (* HTTP client *) END_VAR // ***************************************************************************** // PROGRAM "HTTPIPIfy" // ***************************************************************************** // The program send an API request to "api.ipify.org" and receive back the own // public IP address. // ----------------------------------------------------------------------------- // ------------------------------------------------------------------------- // INITIALIZATIONS // ------------------------------------------------------------------------- // Program initializations. IF (SysFirstLoop) THEN // Set TCPClient parameters. TCPClient.PeerAdd:=ADR('api.ipify.org'); //Peer address TCPClient.PeerPort:=80; //Peer port TCPClient.LocalAdd:=ADR('0.0.0.0'); //Local address TCPClient.LocalPort:=0; //Local port TCPClient.FlushTm:=50; //Flush time (mS) TCPClient.LifeTm:=20; //Life time (S) TCPClient.RxSize:=512; //Rx buffer size TCPClient.TxSize:=512; //Tx buffer size // Set HTTPClient parameters. HTTPRq.RMethod:=0; //Request method, GET HTTPRq.SpyOn:=TRUE; //Activate the spy HTTPRq.HostName:=TCPClient.PeerAdd; // Hostname HTTPRq.Page:=ADR(''); //Web page HTTPRq.Request:=eNULL; //Request string HTTPRq.Header:=eNULL; //HTTP header HTTPRq.DBSize:=512; //Data buffer size HTTPRq.Timeout:=10.0; //Execution timeout END_IF; // ------------------------------------------------------------------------- // MANAGE CONNECTION // ------------------------------------------------------------------------- // Manage the connection. TCPClient(Connect:=HTTPRq.Connect); //TCPClient management HTTPRq(File:=TCPClient.File); //HTTP client IF (HTTPRq.Fault) THEN Errors:=Errors+1; FlipFlop:=FALSE; END_IF; // ------------------------------------------------------------------------- // PROGRAM SEQUENCIES // ------------------------------------------------------------------------- // Program sequencies. IF NOT(FlipFlop) THEN // --------------------------------------------------------------------- // Wait for request command. HTTPRq.Enable:=FALSE; //HTTP enable HTTPRq.Send:=FALSE; //Send request IF NOT(Request) THEN RETURN; END_IF; Request:=FALSE; //Request command // Initialize buffers and enable client. i:=Sysmemset(ADR(Page), 0, SIZEOF(Page)); //Empty page buffer HTTPRq.Enable:=TRUE; //HTTP enable HTTPRq.Send:=TRUE; //Send request FlipFlop:=TRUE; //Program Flip/Flop ELSE // --------------------------------------------------------------------- // Page data are received from server by chunked. When a chunk has been // received DBChars returns its length. The API service sends back your // public IP address on a text string like: "xxx.xxx.xxx.xxx". IF ((HTTPRq.DBChars <> 0) AND (HTTPRq.HPSelector)) THEN IF ((Sysstrlen(ADR(Page))+HTTPRq.DBChars) < SIZEOF(Page)) THEN i:=Sysmemmove(ADR(Page)+Sysstrlen(ADR(Page)), HTTPRq.DBAddress, HTTPRq.DBChars); END_IF; END_IF; // On Done it's possible to test if page has been loaded. IF (HTTPRq.Done) THEN IF NOT(HTTPRq.PLoad) THEN Errors:=Errors+1; FlipFlop:=FALSE; RETURN; END_IF; // Here your public IP address is in the page buffer. FlipFlop:=FALSE; //Program Flip/Flop END_IF; END_IF; // [End of file]
-
AutorePost
- Devi essere connesso per rispondere a questo topic.