Posted: . At: 9:39 AM. This was 9 months ago. Post ID: 18345
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.



Sponsored



Get information such as your MAC address and IP address easily.


Getting the current MAC address of your current network card is very simple. The ethtool utility can print out this information easily.

Print comprehensive information about a network adapter.

╭──(john㉿DESKTOP-PF01IEE)───╮
╰───────────────────────────╾╯(~)-(172.28.100.30)┋ ethtool eth0
Settings for eth0:
        Supported ports: [ ]
        Supported link modes:   Not reported
        Supported pause frame use: No
        Supports auto-negotiation: No
        Supported FEC modes: Not reported
        Advertised link modes:  Not reported
        Advertised pause frame use: No
        Advertised auto-negotiation: No
        Advertised FEC modes: Not reported
        Speed: 10000Mb/s
        Duplex: Full
        Port: Other
        PHYAD: 0
        Transceiver: internal
        Auto-negotiation: off
Cannot get wake-on-lan settings: Operation not permitted
        Current message level: 0x000000f7 (247)
                               drv probe link ifdown ifup rx_err tx_err
        Link detected: yes

get your MAC address easily with this useful command, this is not too hard.

(jcartwright@localhost) 192.168.1.5 ~  $ ip neigh | awk 'NR==2 {print $5}'
c8:14:51:5f:a9:47

Here is a nice Python script to get comprehensive information about your network interface card.

network.py
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
42
43
44
45
import re
import socket
import platform
import netifaces
import subprocess
from get_nic import getnic
from tabulate import tabulate
 
def get_ip_address():
    try:
        # Create a socket to get the IP address
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80))
        ip_address = s.getsockname()[0]
        s.close()
        return ip_address
    except socket.error as e:
        return "Error: " + str(e)
 
def get_mac_address():
    try:
        # Use subprocess to run the 'ipconfig' command (Windows) or 'ifconfig' command (Linux/macOS)
        if platform.system() == "Windows":
            output = subprocess.check_output("ipconfig /all", shell=True, universal_newlines=True)
            mac_address = re.findall(r"Physical Address[\. ]+:\s*([^\r\n]*)", output)[0]
        else:
            output = subprocess.check_output("ifconfig", shell=True, universal_newlines=True)
            mac_address = re.findall(r"ether\s+([^\s]+)", output)[0]
        return mac_address
    except subprocess.CalledProcessError as e:
        return "Error: " + str(e)
 
def get_model_name():
    return platform.machine()
 
if __name__ == "__main__":
    ip_address = get_ip_address()
    mac_address = get_mac_address()
    model_name = netifaces.interfaces()
    model_inf = socket.if_nameindex()
    interfaces = getnic.interfaces()
    network_info = getnic.ipaddr(interfaces)
 
    print("Model:")
    print(tabulate(network_info.items(), headers=["Interface", "Info"], tablefmt="grid"))

This requires python pip to install the needed Python modules.

Install this package first.

[root@localhost Documents]# dnf in python3-netifaces

This will install a required Python module.

Then this module.

(jcartwright@localhost) 192.168.1.5 Documents  $ pip install get-nic

This will satisfy the requirements to run this Python script. Then it will run. I need a good way to get the model name as well, but this is still very good to get simple network card information.

(jcartwright@localhost) 192.168.1.5 Documents  $ python3.9 network.py 
Model:
+-------------+--------------------------------------------------------------------------------------------------------------------+
| Interface   | Info                                                                                                               |
+=============+====================================================================================================================+
| lo          | {'state': 'UNKNOWN', 'inet4': '127.0.0.1/8', 'inet6': '::1/128'}                                                   |
+-------------+--------------------------------------------------------------------------------------------------------------------+
| eno1        | {'state': 'UP', 'HWaddr': 'fc:34:97:a5:bc:7e', 'inet4': '192.168.1.5/24', 'inet6': 'fe80::fe34:97ff:fea5:bc7e/64'} |
+-------------+--------------------------------------------------------------------------------------------------------------------+

To print a full line with awk when you ask for a certain line, use this example.

(jcartwright@localhost) 192.168.1.5 ~  $ lspci | awk 'NR==14{ print $_}'
00:1f.6 Ethernet controller: Intel Corporation Ethernet Connection (14) I219-V (rev 11)

The NR variable keeps a count of the number of lines in the input. The NR==14 example will print the 14th line of the text and then use print $_ to print the full matching line.

Another way to print a full line in awk is to use the $0 operator.

(jcartwright@localhost) 192.168.1.5 ~  $ lspci | awk 'FNR==14{ print $0}'
00:1f.6 Ethernet controller: Intel Corporation Ethernet Connection (14) I219-V (rev 11)


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.