Get the IPv4 address assigned to the network interface in code (Linux)

However, the output of the ʻifconfig command and the ʻip command should not be parsed.

On Linux, you can get it with ʻioctl (SIOCGIFADDR) `.

It is almost the same as the script of the source (Reference 1 below). With Python, it's easy because you can write so far with the distribution attached.

import sys, socket, struct
from fcntl import ioctl

SIOCGIFADDR = 0x8915

if len(sys.argv) < 2:
  print >> sys.stderr, "Usage:", sys.argv[0], "<interface name>"
  sys.exit()

interface = sys.argv[1]

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
  ifreq  = struct.pack('16s16x', interface)
  ifaddr = ioctl(s.fileno(), SIOCGIFADDR, ifreq)
finally:
  s.close()

_, sa_family, port, in_addr = struct.unpack('16sHH4s8x', ifaddr)
print socket.inet_ntoa(in_addr)

Reference

Recommended Posts

Get the IPv4 address assigned to the network interface in code (Linux)
Get the address from the zip code
Get the client's IP address in Django
Disable IPv6 on network interface on CentOS Linux 8
Get the EDINET code list in Python
[Linux] Command to get a list of commands executed in the past
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
Use pygogo to get the log in json.
A tool to insert the country name and country code in the IP address part
How to get the variable name itself in python
How to get the number of digits in Python
[Linux] [C / C ++] How to get the return address value of a function and the function name of the caller
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
One liner to get the nth commit hash in Git
How to use linux commands in Visual Studio Code terminal
Test code to check for broken links in the page
I tried to summarize the code often used in Pandas
How to get Instruction Pointer (= program counter) in Linux kernel
How to get the last (last) value in a list in Python
How to get all the keys and values in the dictionary
To write a test in Go, first design the interface
[Shell] How to get the remote default branch in Git
How to implement Java code in the background of RedHat (LinuxONE)
Automatically acquire the operation log in the terminal when logging in to Linux
[Linux] I want to know the date when the user logged in
How to get all the possible values in a regular expression
How to get the printer driver for Oki Mac into Linux
I wrote the code to write the code of Brainf * ck in python
Hit the New Relic API in Python to get the server status
How to get the vertex coordinates of a feature in ArcPy
The easiest way to get started in Slack socket mode (Go)
Create a function to get the contents of the database in Go
Programming to fight in the world ~ 5-1
Programming to fight in the world ~ 5-5,5-6
Get the desktop path in Python
Get the script path in Python
In the python command python points to python3.8
How to get the Python version
[linux] kill command to kill the process
Road to Linux Intermediate: Network Edition
To maintain code quality in PyCharm
Get the desktop path in Python
Cython to try in the shortest
Linux commands related to character code
How to run TensorFlow 1.0 code in 2.0
Programming to fight in the world ~ 5-2
Get the query string (query string) in Django
How to get the date and time difference in seconds with python
Click the Selenium links in order to get the elements of individual pages
I tried to get the authentication code of Qiita API with Python.
Script to register the IP address used by wercker in Security Group
Use a shortcut to enable or disable the touchpad in Linux Mint