Re: parsing text from ethtool command
On Nov 1, 7:35 pm, Ian Kelly ian.g.ke...@gmail.com wrote: On Tue, Nov 1, 2011 at 5:19 PM, Miki Tebeka miki.teb...@gmail.com wrote: 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 -- http://mail.python.org/mailman/listinfo/python-list
parsing text from ethtool command
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: 0x0001 (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! -- http://mail.python.org/mailman/listinfo/python-list
How do I pass a variable to os.popen?
I'm trying to write a 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. os.popen seems to be the right solution for the ifconfig command, but it doesn't seem to like me passing the interface variable as an argument. Code snippet is below: #!/usr/bin/python # Quick and dirty script to print out available interfaces and their speed # Initializations output = Interface: %s Speed: %s import os, socket, types 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' print cmd gp = os.popen(cmd) fat=gp.read() fat=fat.split('\n') = I'm printing out cmd in an attempt to debug, and interface seems to be passed as a string and not a variable. Obviously I'm a newbie, and I'm hoping this is a simple syntax issue. Thanks in advance! -- http://mail.python.org/mailman/listinfo/python-list