6

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

2
  • For a start it's ifconfig, ipconfig is a DOS command (I'm guessing that was a typo though). So what is the output of ifconfig -a on its own then? Is there a NIC on your system with an IP of 10.0.0.10 listed? Commented May 13, 2016 at 1:35
  • Note on systemd init OS's ipconfig may not be installed, instead the ip command is used (ip addr specifically) Commented May 13, 2016 at 3:34

5 Answers 5

6
ifconfig | grep -B1 10.0.0.10 | grep -o "^\w*"
Sign up to request clarification or add additional context in comments.

2 Comments

Good solution without awk
Doesn't work with multiple IPs in a single network card.
5
ip -br -4 a sh | grep 10.0.0.10 | awk '{print $1}'

Comments

4

You should use this comand :

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

Hope this help !

5 Comments

I'm not sure how this will help if the OP couldn't find what they were after using plain old grep 10.0.0.10 then adding to the string to find still won't find it.
can you explain more? what is OP , and you'd to print out ethernet_name if it's match with given ip address, right ?
OP = original poster, and btw Thanh, this is EXACTLY what I was looking for, thanks :)
No, anything with grep x | awk '{ y }' is usually better written awk '/x/ { y }' - this is a useless use of grep
Although it appends a newline character to the end though? Why is that?
1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.