RasPi: How can i detect the actual local IP of the Ethernet Port lazarus / 32-Linux ---------------------------------------------------------------------- af0815 · 2019-01-16 20:23 I want to detect the actual IP of the Ethernetport. Only the local IP on a RasPi with (actual) Raspbian. Is this possible to to with Pascal or only with a cmdline Tool and parsing the output ? I have searched, but the found soloutions are only for an extrenal IP or not working on a RasPi with Rasbian. ---------------------------------------------------------------------- sash · 2019-01-18 22:39 What exactly doesn't work? Also, what do you mean by "local IP", loopback interface address? ---------------------------------------------------------------------- af0815 · 2019-01-19 10:39 A RasPi have one Ethernetport with an IP adress and one wlan adapter with one IP adress. Both are default DHCP. I want to query the actual local IP. Specially for Ethernet, but if i get both/all with an indication what device is used, it is ok and i can filter out the loopback. This should work with static IP too. Because i didnt know how the RasPi is configured and installed. And the detection should work with out root privileges (no sudo). ---------------------------------------------------------------------- HeavyUser · 2019-01-19 12:17 Quote from: af0815 on January 16, 2019, 09:23:06 pmI want to detect the actual IP of the Ethernetport. Only the local IP on a RasPi with (actual) Raspbian. Is this possible to to with Pascal or only with a cmdline Tool and parsing the output ? I have searched, but the found soloutions are only for an extrenal IP or not working on a RasPi with Rasbian. using indy 10 its as simple as Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  uses  IdStack; procedure TForm1.btnRetreiveIPsClick(Sender :TObject);begin  TIdStack.IncUsage;  try    GStack.AddLocalAddressesToList(ListBox1.Items);  finally    TIdStack.DecUsage;  end;end;                     PS. Listbox1.items can be replaced with any TStringlist variable with out any other changes. eg Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Function RetreiveIPs(Sender :TObject):TStringList;begin  Result := TStringList.Create;  try    TIdStack.IncUsage;    try      GStack.AddLocalAddressesToList(Result);    finally      TIdStack.DecUsage;    end;  Except       On E:Exception do begin      Result.free; //no memory leaks please.        Raise E;     end;  end;end; ---------------------------------------------------------------------- af0815 · 2019-01-19 12:32 Thx for the sample, BUT indy is realy too oversized for querying only the IP =:-) I have tried this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function GetIPAddress: string;var  theProcess: TProcess;  AddressString: AnsiString;begin  try    theProcess := TProcess.Create(nil);    theProcess.Executable := 'hostname';    theProcess.Parameters.Add('-I');    theProcess.Options := [poUsePipes];    theProcess.Execute;    if theProcess.Output.NumBytesAvailable > 0 then    begin      SetLength(AddressString{%H-}, theProcess.Output.NumBytesAvailable);    theProcess.Output.ReadBuffer(AddressString[1], theProcess.Output.NumBytesAvailable);    end;    GetIPAddress := AddressString;  finally    theProcess.Free;  end;end; But i got no response. hostname on commandline works. If i work with the debugger now on a RasPi, i see it is working sometimes. I dig deeper and found, i have to use Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i heProcess.Options := [poUsePipes,poWaitOnExit];Now i got a response. I have to test more. A second improvement maybe Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i theProcess.Parameters.Add('--all-ip-addresses'); If i read the manuals correct. ---------------------------------------------------------------------- HeavyUser · 2019-01-19 12:42 Quote from: af0815 on January 19, 2019, 01:32:53 pmThx for the sample, BUT indy is realy too oversized for querying only the IP =:-) You do understand that the installation size and your applications size are two different things right? In this case it should only add minimal size in the KB range in your exe. Having said that I will not search through the code of the library for you. Quote from: af0815 on January 19, 2019, 01:32:53 pmI have tried this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function GetIPAddress: string;var  theProcess: TProcess;  AddressString: AnsiString;begin  try    theProcess := TProcess.Create(nil);    theProcess.Executable := 'hostname';    theProcess.Parameters.Add('-I');    theProcess.Options := [poUsePipes];    theProcess.Execute;    if theProcess.Output.NumBytesAvailable > 0 then    begin      SetLength(AddressString{%H-}, theProcess.Output.NumBytesAvailable);    theProcess.Output.ReadBuffer(AddressString[1], theProcess.Output.NumBytesAvailable);    end;    GetIPAddress := AddressString;  finally    theProcess.Free;  end;end; But i got no response. hostname on commandline works. If i work with the debugger now on a RasPi, i see it is working sometimes. I dig deeper and found, i have to use Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i heProcess.Options := [poUsePipes,poWaitOnExit];Now i got a response. I have to test more. A second improvement maybe Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i theProcess.Parameters.Add('--all-ip-addresses'); If i read the manuals correct. Sorry I never execute external application from code unless the user has request it (eg open a document or an URL) its a chip trick that has nothing to do with programming its only useful for system admins and scripters. ---------------------------------------------------------------------- Thaddy · 2019-01-19 13:12 Synapse has a function called GetLocalIPs and that works on a Raspberry Pi (any version). It is in the BlckSock unit. http://synapse.ararat.cz/doc/help/ ---------------------------------------------------------------------- engkin · 2019-01-19 14:39 One simple solution involves a connection using TInetSocket: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1; {$mode objfpc}{$H+} uses  {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}  sockets, ssockets; var  c: TInetSocket;begin  //catch ESocketError (seHostNotFound or seConnectFailed) if you care  try    c := TInetSocket.Create('www.google.com',80);    WriteLn(NetAddrToStr(c.LocalAddress.sin_addr));  finally    c.Free;  end;  ReadLn;end. A better solution I found linked on SO is here: Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i #include #include /* for strncpy */#include #include #include #include #include intmain(){ int fd; struct ifreq ifr; fd = socket(AF_INET, SOCK_DGRAM, 0); /* I want to get an IPv4 IP address */ ifr.ifr_addr.sa_family = AF_INET; /* I want IP address attached to "eth0" */ strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1); ioctl(fd, SIOCGIFADDR, &ifr); close(fd); /* display result */ printf("%s\n", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); return 0;} Unfortunately I failed to convert to Pascal. ---------------------------------------------------------------------- af0815 · 2019-01-19 15:39 Quote from: Thaddy on January 19, 2019, 02:12:27 pmSynapse has a function called GetLocalIPs and that works on a Raspberry Pi (any version). It is in the BlckSock unit. http://synapse.ararat.cz/doc/help/ You know, synaser have a nasty bug and cannot compile an a RasPi without changing the sources. QuoteCompile package laz_synapse 40.1: Exit code 1, Errors: 22, Warnings: 26, Hints: 91 .... synaser.pas(232,15) Error: Identifier not found "B500000" synaser.pas(232,22) Error: Illegal expression synaser.pas(233,14) Error: Identifier not found "B576000" synaser.pas(233,21) Error: Illegal expression synaser.pas(234,14) Error: Identifier not found "B921600" synaser.pas(234,21) Error: Illegal expression synaser.pas(235,15) Error: Identifier not found "B1000000" synaser.pas(235,23) Error: Illegal expression synaser.pas(236,15) Error: Identifier not found "B1152000" synaser.pas(236,23) Error: Illegal expression synaser.pas(237,15) Error: Identifier not found "B1500000" synaser.pas(237,23) Error: Illegal expression synaser.pas(238,15) Error: Identifier not found "B2000000" synaser.pas(238,23) Error: Illegal expression synaser.pas(239,15) Error: Identifier not found "B2500000" synaser.pas(239,23) Error: Illegal expression synaser.pas(240,15) Error: Identifier not found "B3000000" synaser.pas(240,23) Error: Illegal expression synaser.pas(241,15) Error: Identifier not found "B3500000" synaser.pas(241,23) Error: Illegal expression synaser.pas(242,15) Error: Identifier not found "B4000000" synaser.pas(242,23) Error: Illegal expression I think this is a missing definition in fpc (missed in termios.inc) or not handled by synapse. So it is not working on arm ---------------------------------------------------------------------- af0815 · 2019-01-19 15:52 Quote from: Thaddy on January 19, 2019, 02:12:27 pmSynapse has a function called GetLocalIPs The function is in synamisc not blcksock and is not working correct - only return localhost Quote--- GetIPAddress (with hostname)--- 192.168.1.41 --- GetIpAddrList (with ifconfig)--- 192.168.1.41  127.0.0.1  --- Synase GetLocalIPs --- 127.0.1.1 I have tested it with hostname, ifconfig and synapse DIRECT on a Raspi Quoteuname -a Linux XxxPi 4.14.71-v7+ #1145 SMP Fri Sep 21 15:38:35 BST 2018 armv7l GNU/Linux ---------------------------------------------------------------------- af0815 · 2019-01-19 16:07 Quote from: engkin on January 19, 2019, 03:39:01 pmOne simple solution involves a connection using TInetSocket: It is working :-) THX for this 'not scriptkidding' woking soloution. Quote--- GetIpAddrList (with TInetSocket)--- 192.168.1.41 Edit: Attached the testproject ---------------------------------------------------------------------- engkin · 2019-01-19 16:24 A cleaner solution should not involve a connection. There is another thread that uses getifaddrs ---------------------------------------------------------------------- avra · 2019-01-20 02:26 Quote from: af0815 on January 19, 2019, 04:39:40 pmYou know, synaser have a nasty bug and cannot compile an a RasPi without changing the sources. QuoteCompile package laz_synapse 40.1: Exit code 1, Errors: 22, Warnings: 26, Hints: 91 .... synaser.pas(232,15) Error: Identifier not found "B500000" synaser.pas(232,22) Error: Illegal expression synaser.pas(233,14) Error: Identifier not found "B576000" synaser.pas(233,21) Error: Illegal expression synaser.pas(234,14) Error: Identifier not found "B921600" synaser.pas(234,21) Error: Illegal expression synaser.pas(235,15) Error: Identifier not found "B1000000" synaser.pas(235,23) Error: Illegal expression synaser.pas(236,15) Error: Identifier not found "B1152000" synaser.pas(236,23) Error: Illegal expression synaser.pas(237,15) Error: Identifier not found "B1500000" synaser.pas(237,23) Error: Illegal expression synaser.pas(238,15) Error: Identifier not found "B2000000" synaser.pas(238,23) Error: Illegal expression synaser.pas(239,15) Error: Identifier not found "B2500000" synaser.pas(239,23) Error: Illegal expression synaser.pas(240,15) Error: Identifier not found "B3000000" synaser.pas(240,23) Error: Illegal expression synaser.pas(241,15) Error: Identifier not found "B3500000" synaser.pas(241,23) Error: Illegal expression synaser.pas(242,15) Error: Identifier not found "B4000000" synaser.pas(242,23) Error: Illegal expression I think this is a missing definition in fpc (missed in termios.inc) or not handled by synapse. So it is not working on arm Are you using official Synapse with Synaser? It is old and you should use OPM's version or trunk. In OPM's version I can see this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const{$IFDEF UNIX}  {$IFDEF BSD}  MaxRates = 18;  //MAC  {$ELSE}   MaxRates = 30; //UNIX  {$ENDIF}{$ELSE}  MaxRates = 19;  //WIN{$ENDIF}  Rates: array[0..MaxRates, 0..1] of cardinal =  (    (0, B0),    (50, B50),    (75, B75),    (110, B110),    (134, B134),  (150, B150),    (200, B200),    (300, B300),    (600, B600),    (1200, B1200),    (1800, B1800),    (2400, B2400),    (4800, B4800),  (9600, B9600),    (19200, B19200),    (38400, B38400),    (57600, B57600),    (115200, B115200),    (230400, B230400){$IFNDEF BSD}    ,(460800, B460800)  {$IFDEF UNIX}    ,(500000, B500000),    (576000, B576000),    (921600, B921600),    (1000000, B1000000),    (1152000, B1152000),    (1500000, B1500000),    (2000000, B2000000),    (2500000, B2500000),    (3000000, B3000000),    (3500000, B3500000),  (4000000, B4000000)  {$ENDIF}{$ENDIF}    );{$ENDIF}  So B500000 and others should be defined. ---------------------------------------------------------------------- af0815 · 2019-01-20 07:25 Its OPM Version. B500000 and the other not defined in termios.inc as i sayed before. Maybe the arm cannot handle this. So it should fixed. Is fpc team responsible or Synapse ? But Synapse didnt send back the correct answer as i stated before. ---------------------------------------------------------------------- avra · 2019-01-20 13:23 Quote from: af0815 on January 20, 2019, 08:25:54 amIts OPM Version. B500000 and the other not defined in termios.inc as i sayed before. Maybe the arm cannot handle this. So it should fixed. Is fpc team responsible or Synapse ?You are right. I have just compared termios.inc from 3.0.5 and 3.2.1 (3.2.1 from December).  Although difference is huge, some things are missing. \fpcsrc\rtl\linux\termios.inc from 3.0.5 has only {$ifdef cpuarm} with baudrates up to B460800, and 3.2.1 has the same defines for cpuarm (up to B460800), but it also has {$ifdef cpuaarch64} with defines up to B4000000. So I would say that currently B500000..B4000000 is currently possible on cpuaarch64 and not on cpuarm. Therefore I guess that you are trying to use cpuarm. You might try to extend cpuarm version and test if it eventualy works. ---------------------------------------------------------------------- Thaddy · 2019-01-20 13:36 I thought this was already fixed.....(because I fixed it, I believe) ---------------------------------------------------------------------- af0815 · 2019-01-20 14:35 Quote from: Thaddy on January 20, 2019, 02:36:41 pmI thought this was already fixed.....(because I fixed it, I believe) In actual fpc trunk (40937), the baudrates for cpuarm not changed. I am on fpc fixes32. But i think higher bausrates did not work out of the box. If i read the RasPi Forums, you have to use resistors (33 Ohm) and to deal with the cables for higher baudrates. So it have to fixed in synapse. Edit: http://fw.hardijzer.nl/?p=138 looks like the baud rate is/was limited to 187500 baud. But you have to fiddle with the kernelinformation and more. See the link from 2012. ---------------------------------------------------------------------- af0815 · 2019-01-20 15:05 The is a patch for the arm issu on the Sourceforge Page https://sourceforge.net/p/synalist/bugs/43/ since April 2018 but not applied. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i --- synaser_r207.pas    2018-04-14 07:16:03.622511742 -0700+++ synaser.pas 2018-04-14 07:16:38.146908883 -0700@@ -200,7 +200,11 @@   {$IFDEF BSD}   MaxRates = 18;  //MAC   {$ELSE}-   MaxRates = 30; //UNIX+    {$IFDEF CPUARM}+    MaxRates = 19; //CPUARM+    {$ELSE}+    MaxRates = 30; //UNIX+    {$ENDIF}   {$ENDIF} {$ELSE}   MaxRates = 19;  //WIN@@ -229,6 +233,7 @@ {$IFNDEF BSD}     ,(460800, B460800)   {$IFDEF UNIX}+    {$IFNDEF CPUARM}     ,(500000, B500000),     (576000, B576000),     (921600, B921600),@@ -240,6 +245,7 @@     (3000000, B3000000),     (3500000, B3500000),     (4000000, B4000000)+    {$ENDIF}   {$ENDIF} {$ENDIF}     ); ---------------------------------------------------------------------- mdalacu · 2023-09-26 07:45 Quote from: HeavyUser on January 19, 2019, 01:17:23 pmQuote from: af0815 on January 16, 2019, 09:23:06 pmI want to detect the actual IP of the Ethernetport. Only the local IP on a RasPi with (actual) Raspbian. Is this possible to to with Pascal or only with a cmdline Tool and parsing the output ? I have searched, but the found soloutions are only for an extrenal IP or not working on a RasPi with Rasbian. using indy 10 its as simple as Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  uses  IdStack; procedure TForm1.btnRetreiveIPsClick(Sender :TObject);begin  TIdStack.IncUsage;  try    GStack.AddLocalAddressesToList(ListBox1.Items);  finally    TIdStack.DecUsage;  end;end;                     PS. Listbox1.items can be replaced with any TStringlist variable with out any other changes. eg Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Function RetreiveIPs(Sender :TObject):TStringList;begin  Result := TStringList.Create;  try    TIdStack.IncUsage;    try      GStack.AddLocalAddressesToList(Result);    finally      TIdStack.DecUsage;    end;  Except       On E:Exception do begin      Result.free; //no memory leaks please.        Raise E;     end;  end;end;                     The most simple and elegant solution! Thank you!