Monday, April 29, 2024
 Popular · Latest · Hot · Upcoming
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

 Answers
4

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)

[#22087] Saturday, February 11, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fulild

Total Points: 239
Total Questions: 103
Total Answers: 112

Location: Papua New Guinea
Member since Thu, Jul 9, 2020
4 Years ago
;