1

I have an application which was currently only being used on Windows machines. Now want to use it also in raspberry PI with linux distributions (raspbian).

My problem is this line:

string ComputerName = System.Net.Dns.GetHostEntry(Request.ServerVariables["REMOTE_ADDR"]).HostName;

This was giving my Active Directy PC full name. Now on the PI I would like to have something like IPAddress + Hostname. But I don´t know how to get those values from a C# application.

Any help would be appreciated, tnx.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57
Henrique Pombo
  • 537
  • 1
  • 6
  • 20

1 Answers1

2

For HostName: HttpContext.Current.Request.ServerVariables["REMOTE_HOST"];

For IPAddress : HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; (See also How to Get IP Address?)

You can see list of ServerVariables at Microsoft Docs

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
  • `HTTP_X_FORWARDED_FOR`is only set if the request goes through a proxy. – Poul Bak Aug 06 '21 at 08:54
  • Yes you are right, that is why I added https://stackoverflow.com/questions/1907195/how-to-get-ip-address to my answer – Selim Yildiz Aug 06 '21 at 08:56
  • The *Hostname* bit didn't work for me (or perhaps I'm judging wrongly what the hostname actually is). Instead of giving my PI Hostname (which is APLGRT10001) it gives me the IP. – Henrique Pombo Aug 06 '21 at 10:04
  • How about if you try this `System.Net.Dns.GetHostByAddress(Request.ServerVariables.Item("REMOTE_HOST")).HostName` ? – Selim Yildiz Aug 06 '21 at 10:37
  • I haven't tried it yet. It says `GetHostByAddress` is obsolete, to change it to `GetHostEntry`. `GetHostEntry` wasn't working on the PI's, so that's why I was searching an alternative. (it would give me a Socket.Exception: No Such host is known) – Henrique Pombo Aug 06 '21 at 10:53
  • Could you please try all options that mentioned here https://stackoverflow.com/a/1768219/5519709, I assume one of them will work :) – Selim Yildiz Aug 06 '21 at 11:12
  • Is rDNS lookup working on your RaspberryPi? Try it for example with `nslookup someip` in the shell. – Steeeve Aug 06 '21 at 11:19
  • @SelimYildiz The example you gave me return the server's name, not the clients (the PI's). – Henrique Pombo Aug 06 '21 at 12:00
  • @SelimYildiz I don't have the nslookup package installed on my PI's. Do I need it to get the hostname? Is there no other way? (I would have to update a lot of PI's, sigh) – Henrique Pombo Aug 06 '21 at 12:01
  • @SelimYildiz you don't need nslookup for rDNS lookups to work, it is one of the possibilities to check if it works. `dig -x ipaddress` would be another possibility to check it. – Steeeve Aug 06 '21 at 12:06