Skip to main content

how to get interface card names using python socket

Namaste Friends,





Today, I am going to share with you simple python codes, which can enumerate available interface name.



Introduction


To make a connection in a computer, first we have to choose a Network Interface from available source and at that time, This automatic function can help you to find the available Interface name so, that you can proceed further.


Before Sharing This Codes, I want to clear that These codes are not written by me.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import socket
import fcntl
import struct
import array


# # found on <http://code.activestate.com/recipes/439093/#c1>
# get all interface names
def all_interfaces():
    max_possible = 128  # arbitrary. raise if needed.

    bytes = max_possible * 32

    # Create a dummy socket
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)


    names = array.array('B', '\0' * bytes)

    outbytes = struct.unpack('iL', fcntl.ioctl(
        s.fileno(),
        0x8912,  # SIOCGIFCONF
        struct.pack('iL', bytes, names.buffer_info()[0])
    ))[0]

    namestr = names.tostring()

    lst = []

    for i in range(0, outbytes, 40):
        name = namestr[i:i+16].split('\0', 1)[0]
        ip   = namestr[i+20:i+24]
        lst.append((name, socket.inet_ntoa(ip)))

    s.close()
    return lst



if __name__ == '__main__':
    print all_interfaces()


Credit Link: http://code.activestate.com/recipes/439093/


Comments

Related Post

Top Visited

Create Simple Packet Sniffer Using Python

how to install burp suite in Linux/Ubuntu 16.04

Big List Of Google Dorks For Sqli Injection

List of Keyboard Shortcuts Keys for GNOME Desktop (Kali linux / Linux / Ubuntu/*nix )

How to create Phishing Page Using Kali Linux | Webpage Page Cloning Using Kali Linux Social Engineering Toolkit

Latest Google Dorks List

What is the use of Pseudo header in TCP/UDP packets?

Best 1000 User-agents List For Web Scraping

How To Install GDB Peda?

how to configure burpsuite with firefox?