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

link|improve this question

1  
That looks more like the output from ifconfig instead of netstat – Doug Harris Oct 25 '10 at 20:54
feedback

4 Answers

up vote 1 down vote accepted

Give this a try:

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

This will omit lo:

ifconfig -a | sed 's/[ \t].*//;/^\(lo\|\)$/d'
link|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
feedback

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.

link|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
feedback

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.
link|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
feedback

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}'
link|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
feedback

Your Answer

 
or
required, but never shown

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