parsing text from "ethtool" command

  • Thread starter extraspecialbitter
  • Start date
E

extraspecialbitter

I'm still trying to write that seemingly simple Python script to print
out network interfaces (as found in the "ifconfig -a" command) and
their speed ("ethtool <interface>"). The idea is to loop for each
interface and
print out its speed. I'm looping correctly, but have some issues
parsing the output for all interfaces except for the "pan0"
interface. I'm running on eth1, and the "ifconfig -a" command also
shows an eth0, and of course lo. My script is trying to match on the
string "Speed", but I never seem to successfully enter the "if"
clause.

First, here is the output of "ethtool eth1":

=================

Settings for eth1:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Speed: 100Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 1
Transceiver: internal
Auto-negotiation: on
MDI-X: off
Supports Wake-on: pumbag
Wake-on: g
Current message level: 0x00000001 (1)
Link detected: yes

=================

The script *should* match on the string "Speed" and then assign "100Mb/
s" to a variable, but is never getting past the second if statement
below:

=================

#!/usr/bin/python

# Quick and dirty script to print out available interfaces and their
speed

# Initializations

output = " Interface: %s Speed: %s"
noinfo = "(Speed Unknown)"
speed = noinfo

import os, socket, types, subprocess

fp = os.popen("ifconfig -a")
dat=fp.read()
dat=dat.split('\n')
for line in dat:
if line[10:20] == "Link encap":
interface=line[:9]
cmd = "ethtool " + interface
gp = os.popen(cmd)
fat=gp.read()
fat=fat.split('\n')
for line in fat:
if line[0:6] == "Speed":
try:
speed=line[8:]
except:
speed=noinfo
print output % (interface, speed)

=================

Again, I appreciate everyone's patience, as I'm obviously I'm a python
newbie. Thanks in advance!
 
M

Miki Tebeka

In my box, there are some spaces (tabs?) before "Speed". IMO re.search("Speed", line) will be a more robust.
 
E

extraspecialbitter

In my box, there are some spaces (tabs?) before "Speed". IMO re.search("Speed", line) will be a more robust.

Or simply:

if "Speed" in line:

There is no need for a regular expression here.  This would also work
and be a bit more discriminating:

if line.strip().startswith("Speed")

BTW, to the OP, note that your condition (line[0:6] == "Speed") cannot
match, since line[0:6] is a 6-character slice, while "Speed" is a
5-character string.

Cheers,
Ian

Ian,

Replacing my regular expression with line.strip().startswith did the
trick. Thanks for the tip!

Paul
 
J

Jean-Michel Pichavant

extraspecialbitter said:
I'm still trying to write that seemingly simple Python script to print
out network interfaces (as found in the "ifconfig -a" command) and
their speed ("ethtool <interface>"). The idea is to loop for each
interface and
print out its speed. I'm looping correctly, but have some issues
parsing the output for all interfaces except for the "pan0"
interface. I'm running on eth1, and the "ifconfig -a" command also
shows an eth0, and of course lo. My script is trying to match on the
string "Speed", but I never seem to successfully enter the "if"
clause.

First, here is the output of "ethtool eth1":

=================

Settings for eth1:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Speed: 100Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 1
Transceiver: internal
Auto-negotiation: on
MDI-X: off
Supports Wake-on: pumbag
Wake-on: g
Current message level: 0x00000001 (1)
Link detected: yes

=================

The script *should* match on the string "Speed" and then assign "100Mb/
s" to a variable, but is never getting past the second if statement
below:

=================

#!/usr/bin/python

# Quick and dirty script to print out available interfaces and their
speed

# Initializations

output = " Interface: %s Speed: %s"
noinfo = "(Speed Unknown)"
speed = noinfo

import os, socket, types, subprocess

fp = os.popen("ifconfig -a")
dat=fp.read()
dat=dat.split('\n')
for line in dat:
if line[10:20] == "Link encap":
interface=line[:9]
cmd = "ethtool " + interface
gp = os.popen(cmd)
fat=gp.read()
fat=fat.split('\n')
for line in fat:
if line[0:6] == "Speed":
try:
speed=line[8:]
except:
speed=noinfo
print output % (interface, speed)

=================

Again, I appreciate everyone's patience, as I'm obviously I'm a python
newbie. Thanks in advance!
Hi, without starting a flamewar about regular expression, they sometimes
can become usefull and really simplify code:

s1 = """eth0 Link encap:Ethernet HWaddr 00:1d:09:2b:d2:be
inet addr:192.168.200.176 Bcast:192.168.200.255
Mask:255.255.255.0
inet6 addr: fe80::21d:9ff:fe2b:d2be/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:297475688 errors:0 dropped:7 overruns:0 frame:2
TX packets:248662722 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2795194692 (2.6 GiB) TX bytes:2702265420 (2.5 GiB)
Interrupt:17

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:5595504 errors:0 dropped:0 overruns:0 frame:0
TX packets:5595504 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1601266268 (1.4 GiB) TX bytes:1601266268 (1.4 GiB)

"""

import re

itfs = [section for section in s1.split('\n\n') if section and section
!= '\n'] # list of interfaces sections, filter the empty sections

for itf in itfs:
match = re.search('^(\w+)', itf) # search the word at the begining
of the section
interface = match and match.group(1)
match = re.search('MTU:(\d+)', itf) # search for the field MTU: and
capture its digital value
mtu = (match and match.group(1)) or 'MTU not found'
print interface, mtu


If you're not familiar with python regexp, I would advise to use
kodos.py (google it), it really does help.
The strong point about the code above, is that it removes all the
tedious if then else logic and the arbitrary slice indexes.

JM

PS : I cannot test the 'Speed' because it's absent from my ifconfig
display, but you should be able to figure it out :eek:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top