Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to get a list of all available Network-Device Names on my Linux server. I figured that

netstat -a

would do the job, however netstat produces quite much output:

eth0      Link encap:Ethernet  Hardware Adresse 08:00:27:fc:5c:98  
          inet Adresse:192.168.2.222  Bcast:192.168.2.255  Maske:255.255.255.0
          inet6-Adresse: fe80::a00:27ff:fefc:5c98/64 Gültigkeitsbereich:Verbindung
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metrik:1
          RX packets:329 errors:0 dropped:0 overruns:0 frame:0
          TX packets:177 errors:0 dropped:0 overruns:0 carrier:0
          Kollisionen:0 Sendewarteschlangenlänge:1000 
          RX bytes:41496 (40.5 KiB)  TX bytes:32503 (31.7 KiB)

eth1      Link encap:Ethernet  Hardware Adresse 08:00:27:e9:35:7d  
          [...]

eth2      Link encap:Ethernet  Hardware Adresse 08:00:27:ff:db:fe  
          [...]

lo        Link encap:Lokale Schleife  
          [...]

What I want to achieve is a list like

eth0
eth1
eth2
lo

or even better just

eth0
eth1
eth2

I assume that this can be done by a combination of "cat", "sed" and "grep", but I have simply no clue of how to strip the uneccessary information.

Thanks in advance

share|improve this question
1  
That looks more like the output from ifconfig instead of netstat –  Doug Harris Oct 25 '10 at 20:54

7 Answers 7

up vote 3 down vote accepted

Give this a try:

ifconfig -a | sed 's/[ \t].*//;/^$/d'

This will omit lo:

ifconfig -a | sed 's/[ \t].*//;/^\(lo\|\)$/d'
share|improve this answer
    
Thanks dennis, this worked like a charm^^. Perfekt. Regular Expressions are really something i should look into^^ –  ftiaronsem Oct 26 '10 at 19:39

Try this:

ifconfig | cut -c 1-8 | sort | uniq -u
  • cut -c 1-8 extracts the first 8 characters of each line
  • sort sorts the lines
  • uniq -u prints only unique lines which will remove the blank lines for the description lines which have only spaces in their first eight characters

This works on two linux machines I tried, but on my MacBookPro (OS X 10.6.4), ifconfig uses tabs instead of spaces, so it's a bit more complicated:

ifconfig | expand | cut -c1-8 | sort | uniq -u | awk -F: '{print $1;}'
  • expand converts tabs to spaces
  • awk -F: '{print $1;}' prints the first field before a colon.
share|improve this answer
    
Thank you very much, especially for the very detailed answer explaining the used parameters. If I will have a similar issue in the future, I know at which post to look ;-). Unfortunatelly it was not cutting "lo", so the accepted answer goes to dennis. But this really is a very usefull answer, thank you :-) –  ftiaronsem Oct 26 '10 at 19:39
    
add | grep -v lo :-) –  Doug Harris Oct 27 '10 at 2:48

Here's one way to extract the interface names from the ifconfig output:

ifconfig -a | sed -n 's/^\([^ ]\+\).*/\1/p'

If you want to exclude certain names, one way is further filter with grep:

ifconfig -a | sed -n 's/^\([^ ]\+\).*/\1/p' | grep -Fvx -e lo

If you want to exclude more names, add more -e foo to the grep command.

share|improve this answer
    
Your solution works equally well as dennis's. Unfortunatelly I cant accept two posts, so dennis was simply faster. But thanks nevertheless, especially for your grep explanation. –  ftiaronsem Oct 26 '10 at 19:41

Using /sys filesystem:

basename -a $(ls /sys/devices/**/net/* -d)

Using ip and Perl:

ip -o l|perl -lane'$F[1]=~s/://g;print $F[1]'
share|improve this answer

to just print the first column:

netstat -a | awk '{print $1}'

you can incorporate other rules in awk to add or remove entries as needed.

EDIT: same goes with ifconfig (like Doug pointed out)

ifconfig | awk '{print $1}'

This is an example excluding the 'lo' interface

ifconfig | awk '{if ($1 != lo) print $1}'
share|improve this answer
    
That won't work. awk will ignore the leading white space and print the first word in each subsequent line as well. –  Doug Harris Oct 25 '10 at 21:41
    
Unfortunatelly, thats indeed not working, suffering the exact issue Doug pointed out. But thanks for you help, nevertheless I really appreciate you helping a newby like me. –  ftiaronsem Oct 26 '10 at 19:36

Another alternative would be:

ip -o link show | awk -F': ' '{print $2}'

Or maybe:

ls /sys/class/net
share|improve this answer

ifconfig | grep flags | awk -F: '{print $1;}'

share|improve this answer
1  
Could you please expand end explain what this does? One line answers are generally not a good format for Superuser. –  Doktoro Reichard May 15 at 0:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.