1

I'm configuring simple-httpd so I can access my elfeed remotely and would like to automatically detect the IP address of the host (since I use the config across machines and want to set it dynamically).

I found this old thread but C-h v network- doesn't list network-interface-list.

Is there a function I could leverage to get this or should I look at parsing output of ip a in some manner?

EDIT :

Config I'm using for simple-httpd is...

(use-package simple-httpd
  :ensure t
  :config
  (setq httpd-host '"217.168.27.32")
  (setq httpd-port '"8818"))

Since I run Emacs in daemon mode on a VPS my aim is to have it start simple-httpd (via elfeed-web-start) and then be able to point my browser (from anywhere) to the hostname and port I have associated with my VPS.

However, my Emacs config is shared across machines and if my VPS goes down I'd like to be able to point to a device on my home network that also runs Emacs in daemon mode and access that (it has a static IP on the home network, I have a static IP from my ISP and have a DNS entry for that and I can port forward requests to the correct device).

Hence the sharing of configuration across devices means I'd like to dynamically set the httpd-host for simple-httpd.

I can as suggested get the system-name (although help indicates its obsolete and to use (system-name)instead), so its presumably then a case of setting the static IP address' I have conditional on this value by the sounds of it.

slackline
  • 293
  • 3
  • 14

1 Answers1

1

Computers have more than one ip address, and it’s not always stable. Normally mine is x.x.x.2, but just yesterday I happened to notice that it was x.x.x.133 for some reason. I didn’t investigate because I was about to install a new kernel and reboot, but I’m sure I’ll have to figure that out at some point. I don’t think that you should use ip addresses to distinguish computers in your config file.

You should use hostnames. Hostnames are intended to be human recognizable, rememberable, and are generally chosen by the owner of the machine. You can retrieve the hostname of the current machine by calling system-name.

That said, since you are configuring a network service specifically you might be configuring the listening port of the service. In that case you should specify "0.0.0.0" or "::" to listen on all available IPv4 or IPv6 interfaces, regardless of what machine you are currently on. You can read about this in the documentation for make-network-process, but know that this is the normal OS behavior when opening a listening socket. This is what you do in any programming language when you want to listen on all available interfaces.

Edit: Like I said, use "0.0.0.0" as the httpd-host.

(use-package simple-httpd
  :ensure t
  :config
  (setq httpd-host "0.0.0.0")
  (setq httpd-port "8818"))

This will tell it to listen on all available network interfaces. It will do exactly what you want on all of your computers.

(Also, strings don’t need to be quoted, so you don’t need to put a ' in front of them.)

db48x
  • 15,741
  • 1
  • 19
  • 23
  • Thanks, I do understand a little about `dhcp` and on my home network assign static IP addresses to the two systems I wish to run this on. I also pay my ISP for a static IP and a static IP on my VPS and use DNS with both. I've added some clarity to the above. – slackline Jun 16 '22 at 05:24
  • Ahha, I understand now, thank you. I've only ever configured `nginx` before and never had to specify a host before. Thank you very much @db48x – slackline Jun 16 '22 at 06:48
  • You’re welcome. FWIW, the nginx `listen` directive works much the same way, but with a bunch of additions (such as listening on both IPv4 and IPv6 interfaces, which requires opening one socket for each). This is one of those things that is common to all programs that use listening sockets. – db48x Jun 16 '22 at 09:08