How do I pass a variable to os.popen?

E

extraspecialbitter

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!
 
I

Ian Kelly

   cmd = 'ethtool %interface'

That is not Python syntax for string interpolation. Try:
cmd = 'ethtool %s' % interface

On a side note, os.popen is deprecated. You should look into using
the higher-level subprocess.check_output instead.

Cheers,
Ian
 
J

J

   if line[10:20] == "Link encap":
      interface=line[:9]
   cmd = 'ethtool %interface'
   print cmd
   gp = os.popen(cmd)

because you're saying that cmd is 'ethtool %interface' as you pointed
out later on...

how about:

cmd = 'ethtool %s' % interface

note the spaces there... that tells it to convert the contents of
interface to a string and insert them into the string you're assigning
to cmd...

assuming interface is things like eth0, you shoud now see "ethtool
eth0" when the print statement runs.
 
C

Chris Rebert

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

os.popen() is somewhat deprecated. Use the subprocess module instead.
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'

cmd will literally contain a percent-sign and the word "interface". If
your shell happens to use % as a prefix to indicate a variable, note
that Python variables are completely separate from and not accessible
from the shell. So either ethtool will get the literal string
"%interface" as its argument, or since there is no such shell
variable, after expansion it will end up getting no arguments at all.
Perhaps you meant:
cmd = "ethtool %s" % interface
Which could be more succinctly written:
cmd = "ethtool " + interface
   print cmd
   gp = os.popen(cmd)
   fat=gp.read()

The subprocess equivalent is:
fat = subprocess.check_output(["ethtool", interface])
   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!

Cheers,
Chris
 

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,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top