Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1807904
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:11:46+00:00 2026-05-17T06:11:46+00:00

I need to list the available network interfaces and their IP addresses and corresponding

  • 0

I need to list the available network interfaces and their IP addresses and corresponding netmasks using Python in a Linux environment. I can get the interfaces and the IP addresses of each interface using ioctl and SIOCGIFCONF as outlined here, but I’m at loss when it comes to determining the netmask when there are multiple IP address on an interface.

I can get the netmask of the primary IP address of an interface as suggested in Retrieving network mask in Python:

import socket
import fcntl
import struct

SIOCGIFNETMASK = 0x891b

def get_network_mask(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    netmask = fcntl.ioctl(s, SIOCGIFNETMASK, struct.pack('256s', ifname))[20:24]
    return socket.inet_ntoa(netmask)

>>> get_network_mask('eth0')
'255.255.255.0'

However, this won’t work if I have multiple IP addresses with different netmasks on the same interface, as follows:

$ ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
    inet 192.168.1.1/16 brd 192.168.255.255 scope global eth0
    inet 172.16.0.123/24 scope global eth0

In this case I’ll only be able to retrieve one of the netmasks using the above function. Is there a way to retrieve the netmasks for all addresses, except by parsing the output from ip addr show or ifconfig?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:11:47+00:00Added an answer on May 17, 2026 at 6:11 am

    Technically what “ip addr sh” does is use the netlink library to interrogate (and optionally monitor) the kernel network interfaces / routing tables.

    You might be able to do this in python, but I strongly recommend parsing the output of “/sbin/ip addr sh”

    This is because

    • Using the rtnetlink library is complicated
    • You don’t care about performance – network interfaces don’t change often enough that you care (and in any case you can monitor them using “ip monitor”)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to list the available network interfaces and their IP addresses and corresponding
In Python, how big can a list get? I need a list of about
I need to use Java to get a list of all the IP addresses
I need to query for a list of available Wireless Access points. I only
I'm building a website in ASP.Net, using MVC, and need to list a set
We need a mailing list package similar to Python Mailman . I'd use Mailman
Hi I am new to android. I need to list available files in device
I'm using QWINSTA.EXE to get the session/user list from a terminal server on our
Where can I found the list of all the states of world with their
I need to search for available wireless networks and take this list and store

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.