[RUBY] To get a local IP address programmatically

Introduction

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

shell

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

Summary

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.

Related article

-[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

To get a local IP address programmatically
Get local IP address
Convert IP address to decimal
Get a global IP and export it to Google Spreadsheets
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
Get the client's IP address in Django
How to get IP when Tornado + nginx
Get a local DynamoDB environment with Docker
How to get a stacktrace in python
Translate IP address range to another subnet 1: 1
Get your own IP address in Python
Note to plot IP address with Kibana + Elasticsearch
Added firewalld to Amazon Linux 2 (IP address restrictions)
How to set up a local development server
I made a tool to get new articles
Create a command to get the work log
A layman wants to get started with Python
A tool to insert the country name and country code in the IP address part
The shortest route to get a cultural fish environment
How to get a logged-in user with Django's forms.py
How to create a local repository for Linux OS
certificate verify failed: unable to get local issuer certificate (_ssl.c: 1108)')]
[Linux] How to put your IP in a variable
I want to bind a local variable with lambda
[Linux] [C / C ++] How to get the return address value of a function and the function name of the caller
Make an IP address allocation / allocation list for a certain area
[Python] I want to get a common set between numpy
[Command] Command to get a list of files containing double-byte characters
Developed a library to get Kindle collection list in Python
I tried "How to get a method decorated in Python"
I tried to get started with Hy ・ Define a class
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
I wrote a script to get a popular site in Japan
Assign a link-local address to Linux and use DNS-SD / SSDP
How to get a list of links from a page from wikipedia
How to get a quadratic array of squares in a spiral!
How to display the CPU usage, pod name, and IP address of a pod created with Kubernetes