Return types of os:type/0 and default route discovery

zxq9 zxq9@REDACTED
Fri Feb 5 08:03:45 CET 2021


I'm writing a convenience function to discover the default gateway, 
network interface and local source IP address of the host machine. 
Lacking a native interface to this within the runtime and needing it to 
be as broadly cross platform as possible I'm currently thinking to check 
the value of os:type/0 and issue the current system's default equivalent 
of something like `os:cmd("ip route get 8.8.8.8")` and parse the output 
to get the parts of the return value I need.

To do this properly I need to know what return types I should be 
checking for in the OS name part of the return value, as most systems 
have some differences in what default command line network information 
utilities are available.

Picking an initial command to examine for IPv4 might look something like:

   default_iface() -> default_iface(os:type()).

   default_iface({unix, linux} ->
       Out = os:cmd("ip route get 8.8.8.8"),
       % get_stuff_from(Out);
   default_iface({unix, osx} ->
       Out = os:cmd("route -n get 8.8.8.8"),
       % get_stuff_from(Out);
   default_iface({unix, bsd} ->
       Out = os:cmd("netstat -r"),
       % get_stuff_from(Out);
   default_iface({windows, nt} ->
       Out = os:cmd("route -4"),
       % get_stuff_from(Out);
   default_iface({Family, Name} ->
       %...?


What else should I be looking for or be aware of? Is there a better way 
to approach the problem? (Keep in mind this code needs to be purely in 
Erlang, though callouts through os:cmd/1 could be anything as long as it 
is guaranteed to be available by default on the target system)

Thanks,
-Craig


More information about the erlang-questions mailing list