When I wrote the article [Sound with Docker](// abrakatabura.hatenablog.com/entry/2014/08/21/062305), how to get the local IP address of the host executing the Docker command I was curious and found out how to write in some of the languages I'm using these days. I also checked OCaml, but gave up because I didn't have enough google and English skills. .. ..
Ruby
2.1 or later
ruby -r socket -e 'puts Socket.getifaddrs.select{|x| x.name == "eth0" and x.addr.ipv4?}.first.addr.ip_address'
Link
-In Ruby 2.1.0, you can easily get the interface address with Socket.getifaddrs --Qiita
Python
The method of using gethostname came out immediately, but on Ubuntu etc., it seems that it is quite possible to get 127.0.0.1, so I searched for another method.
python -c "import socket;print([(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1])"
pip install netifaces
python -c "import netifaces;print(netifaces.ifaddresses('eth0')[netifaces.AF_INET][0]['addr'])"
Link
Perl
cpanm install IO::Interface
perl -MIO::Interface::Simple -e 'CORE::say IO::Interface::Simple->new(shift || "eth0")->address'
Link
-Getting a local address in Perl-Naoya's Hatena Diary
Node.js
It worked nicely with standard modules and worked on Windows.
node -e "require('os').networkInterfaces()['eth0'].filter(function(elm){if(elm.family=='IPv4')console.log(elm.address)})"
node -e "require('os').networkInterfaces()['Local area connection'].filter(function(elm){if(elm.family=='IPv4') console.log(elm.address)})"
Link
hostname -I
The OSX hostname command didn't work.
AppleScript
osascript -e "IPv4 address of (get system info)"
Link
-Get a local IP address-Unnamed techno hand
Surprisingly, I found it difficult to get a local IP address. For many languages, install modules etc. separately Writing with it was smart and platform-dependent.
Unfortunately, there is no such thing as OK in any environment with the writing styles listed here, so fine adjustment is necessary if necessary.
-[Create an https server in ll language](// abrakatabura.hatenablog.com/entry/2013/09/18/070657) -[To notify iPhone in LAN of your IP address with Electron](// abrakatabura.hatenablog.com/entry/2015/10/24/113659) -[I wrote a js that can get the MAC address of the Amazon Dash button only on Windows](// abrakatabura.hatenablog.com/entry/2017/03/04/230941) -[I made a tool to get a list of * .local host names in LAN at high speed using goroutine](// abrakatabura.hatenablog.com/entry/2018/07/04/213245)
Recommended Posts