50
rated 0 times
[
50]
[
0]
/ answers: 1 / hits: 110422
/ 1 Year ago, fri, february 10, 2023, 9:06:57
How can I display the IP address shown on eth0 using a script ?
More From » scripts
How can I display the IP address shown on eth0 using a script ?
For the sake of providing another option, you could use the ip addr
command this way to get the IP address:
ip addr show eth0 | grep "inet" | awk '{print $2}' | cut -d/ -f1
ip addr show eth0
shows information about eth0
grep "inet"
only shows the line that has the IPv4 address (if you wanted the IPv6 address, change it to "inet6"
)awk '{print $2}'
prints on the second field, which has the ipaddress/mask, example 172.20.20.15/25
cut -d/ -f1
only takes the IP address portion.In a script:
#!/bin/bash
theIPaddress=$(ip addr show eth0 | grep "inet" | awk '{print $2}' | cut -d/ -f1)