1

I have this command line, but it's doubling the grep and awk

ifconfig eth1 2> /dev/null | grep "inet " | awk '{gsub("addr:","",$2);  print $2 }' ||
ifconfig eth0 2> /dev/null | grep "inet " | awk '{gsub("addr:","",$2);  print $2 }'`

I have being trying to shorten this by doing something like,

echo $(ifconfig eth1 2> /dev/null || ifconfig eth0 2> /dev/null) &>  grep "inet " |
awk '{gsub("addr:","",$2);  print $2 }'

but it returns nothing at all when I am looking to get the IP of server. What is the best way to say get info if on error try this and then pipe it to grep

10
  • Please add OS/distribution and version. Commented Jul 6, 2015 at 19:03
  • I'm testing on a CentOS box but I'd like it to be as universal as I can Commented Jul 6, 2015 at 19:04
  • 2
    See: Deprecated Linux networking commands and their replacements. A default installation of RHEL7 has no ifconfig. Commented Jul 6, 2015 at 19:26
  • @cyrus, so should I have a ip a || as well in there? Commented Jul 6, 2015 at 19:29
  • 2
    or forget ifconfig and use ip. Commented Jul 6, 2015 at 19:31

4 Answers 4

2

Another more modular approach: not bash-specific

get_ifconfig () { 
    for iface do 
        ifconfig "$iface" 2>/dev/null && return
    done
}
get_ifconfig eth1 eth2 eth3 | sed -n 's/.*inet addr:\([0-9.]\+\).*/\1/p'
4
  • this is on the command line not in a file Commented Jul 6, 2015 at 19:22
  • It's OK to define functions on the command line. You can paste this code directly to a shell prompt. Commented Jul 6, 2015 at 19:44
  • in this case i need it in one line Commented Jul 6, 2015 at 19:46
  • @jeremy.bass Then replace the newlines by semicolons, except the one after do. Commented Jul 6, 2015 at 22:46
0

Try this:

$ ( ifconfig eth2 2>/dev/null || ifconfig eth0 2>/dev/null ) | grep ...

It has the same logic, but it does the || inside the parens so it only passes the output of the successful command to grep.

6
  • $ ( ifconfig eth2 2>/dev/null || ifconfig eth0 2>/dev/null ) | grep "inet " | awk '{gsub("addr:","",$2); print $2 }' returns bash: syntax error near unexpected token ifconfig'` Commented Jul 6, 2015 at 19:00
  • what version of bash? Commented Jul 6, 2015 at 19:04
  • 4.1.2(1)-release (x86_64-redhat-linux-gnu) but I'd like it to be as universal as I can Commented Jul 6, 2015 at 19:05
  • The $ is not part of the command. Just ( ifconfig eth2 2>/dev/null || ifconfig eth0 2>/dev/null ) | grep ... Commented Jul 6, 2015 at 19:15
  • Correct - the $ is the prompt. The command you type in starts with the (. Commented Jul 6, 2015 at 19:16
0

How about this approach

$ echo eth1 eth0 | xargs -n 1 ifconfig 2>/dev/null | awk '/inet/{gsub("addr:","",$2);print $2;exit}'
192.168.124.132
$
3
  • I end up with addr:10.255.255.3 addr: addr:10.0.2.15 addr: I should only end up with 10.255.255.3 Commented Jul 6, 2015 at 19:16
  • 1
    echo eth1 eth0 | xargs -n 1 ifconfig 2>/dev/null | grep "inet " | awk '{gsub("addr:","",$2); print $2 }' is a little closer but it should be that if eth1 doesn't have a value go look to eth0 right now it's still getting both Commented Jul 6, 2015 at 19:19
  • Now edited to fix (added gsub and the exit). Commented Jul 6, 2015 at 19:21
0

If you want to grab the first interface that has an IPv4 address, you can run ifconfig just once to display all the interfaces and extract the first entry with an IPv4 address. You can also optimize the extraction by combining what you're doing with grep and awk.

ifconfig -a | awk '/^[^ ]/ {iface=$1}
                  iface ~ /^eth/ && sub(/inet addr:/,"") {print $1; exit}'

The newline is optional, I included it for clarity. This is a more elaborate version of

ifconfig -a | awk 'sub(/inet addr:/,"") {print $1; exit}'

which might print something like 127.0.0.1 (from the loopback interface) that you presumably don't want.

If you prefer to use ip, parsing is about equally awkward.

ip addr | awk '/^[0-9]/ {iface=$2}
               iface ~ /^eth/ && $1 == "inet" {sub(/\/.*/, "", $2); print $2}'

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.