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 9129229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:41:53+00:00 2026-06-17T07:41:53+00:00

In my Python script I need to retrieve both the IP address of the

  • 0

In my Python script I need to retrieve both the IP address of the machine the script is running on and its network address and its network bytes.

As for the IP address, I found the solution in the archive:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("www.google.com",80))
myAddress = (s.getsockname()[0])
s.close()

But how should I go about finding network address and network bytes? I need to put this information into a filter for tcpdump in the format $NetworkAddress/$NetworkBytes, if that helps at all.

Example:

128.1.2.0/20

I can actually find it under inet when I run ip addr. Any easy way to get this information in Python?

  • 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-06-17T07:41:54+00:00Added an answer on June 17, 2026 at 7:41 am

    For Linux try

    iface = "eth0"
    socket.inet_ntoa(fcntl.ioctl(socket.socket(socket.AF_INET, socket.SOCK_DGRAM), 
                                 35099, struct.pack('256s', iface))[20:24])
    

    or http://github.com/rlisagor/pynetlinux

    (as suggested here: Retrieving network mask in Python)

    For Linux, Windows and MacOS consider http://alastairs-place.net/projects/netifaces/

    Update:

    If you need cidr (like ‘128.1.2.0/20’), you can use any of the related libs: http://pypi.python.org/pypi?%3Aaction=search&term=cidr&submit=search

    For example netaddr:

    >> from netaddr import IPNetwork
    >> print str(IPNetwork('1.2.3.4/255.255.255.0').cidr)
    1.2.3.0/24
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a python script that launches a shell script and import
I need to take an optional argument when running my Python script: python3 myprogram.py
I need to write a python script that makes multiple HTTP requests to the
I'm writing a Python backup script and I need to find the oldest file
In my python script, I need to call within a for loop an executable,
I need to access a few HTML pages through a Python script, problem is
Sometimes (in customer's PCs) I need a python script to execute in the Windows
I am writing a Script in Python and I need to search and Replace
I need help debugging some odd file behavior in Python. Take the following script
I am maintaining a Python script that uses xlrd to retrieve values from Excel

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.