TonyW
TonyW

Reputation: 786

Find network interface by IP address - Linux/Bash

I'm wondering how I can query by IP address using sed, and it will show which interface name that is using it.

For example..

ipconfig -a | grep 10.0.0.10

I would expect it to come back with ETH0

Upvotes: 6

Views: 8737

Answers (5)

x-yuri
x-yuri

Reputation: 18863

In iproute2-4.14.0 they added a -json argument:

[ANNOUNCE] iproute2 4.14.1
ip: add new command line argument -json (mutually exclusive with -color)
ip: ipaddress.c: add support for json output

And these days (6.11.0) you can do:

$ ip -json address \
    | jq -r 'map(select(
        (.addr_info | first).local == "192.168.122.5"
    ))[0].ifname'
br-433ed67eb05d

// or

$ ip -json address \
    | jq -r 'map(select(
        .addr_info
        | map(select(.family == "inet" and .local == "192.168.122.5"))
        | length > 0
    ))[0].ifname'
br-433ed67eb05d

// or

$ ip -json address \
    | jq -r 'map(select(
        .addr_info | any(
            .family == "inet" and .local == "192.168.122.5"
        )
    ))[0].ifname'
br-433ed67eb05d

The ip -json address output is of the form:

[
  {
    "ifname": "br-433ed67eb05d",
    "addr_info": [
      {
        "local": "192.168.122.5",
        ...
      }
    ],
    ...
  },
  ...
]

map(f) applies f to the items ({"ifname": ...}) of the input array. select(f) returns its input ({"ifname": ...}) if f is true for that input. (.addr_info | first).local is the local field of the first item in .addr_info. Then [0].ifname makes it return the ifname field of the first result, and -r outputs the string as is, not as a JSON value (in quotes).

Or in a less reliable way (depending on your task you might want to be more careful):

$ ip address | grep -FB2 'inet 192.168.122.5' | head -1 \
    | awk '{print $2}' | sed -E 's/:$//'
br-433ed67eb05d

Another possibly safer option:

$ ip a | grep -E '^([0-9]+: | *inet )' | grep -E -B1 '^ *inet 192.168.122.5' \
    | head -1 | sed -E 's/^[0-9]+: ([^:]+):.*/\1/'
br-433ed67eb05d

The -br[ief] solution was already posted, so I'll just mention that it (-br[ief]) was added in 4.2.0:

[ANNOUNCE] iproute2 4.2.0
add support for brief output for link and addresses

And sh (ip -br -4 a sh) is optional.

Upvotes: 1

Hamid Reza Moradi
Hamid Reza Moradi

Reputation: 469

ip -br -4 a sh | grep 10.0.0.10 | awk '{print $1}'

Upvotes: 5

promaty
promaty

Reputation: 197

ifconfig | grep -B1 10.0.0.10 | grep -o "^\w*"

Upvotes: 6

RMDS
RMDS

Reputation: 419

If you want sed specific solution you may try this. Its little hard to digest how it works , but finally this combination works.

 ifconfig | sed -n '/addr:10.0.0.10/{g;H;p};H;x' | awk '{print $1}'

If you want to take it as an argument via script use "$1" or so instead if 10.0.0.10.

Sed manual for reference : http://www.gnu.org/software/sed/manual/sed.html#tail

Upvotes: 0

Thanh Nguyen Van
Thanh Nguyen Van

Reputation: 11772

You should use this comand :

ifconfig | grep -B1 "inet addr:10.0.0.10" | awk '$1!="inet" && $1!="--" {print $1}'

Hope this help !

Upvotes: 4

Related Questions