0

I'm accessing ifconfig, iwconfig and iw from subprocess.Popen as below:

p = sp.Popen(["ifconfig",nic],stdin=sp.PIPE,stdout=sp.PIPE,stderr=sp.PIPE)
out,err = p.communicate()

Is there a better way, i.e faster to do this such as interfacing directly to the underlying code perhaps or is the added time delay negligible - time becomes a concern when using iw to switch channels.

Cœur
  • 37,241
  • 25
  • 195
  • 267
WraithWireless
  • 634
  • 9
  • 21
  • if you're okay with external dependencies, native bindings exist (ie `python-ifconfig`). – roippi Jul 08 '14 at 20:04
  • I've looked at that however, it is my understanding that in the case of iw the "pythonic" version is very limited. How would one go about coding a python-iw with native bindings? – WraithWireless Jul 08 '14 at 20:12

2 Answers2

2

Try reading/writing the relevant files from the /proc filesystem directly.

The /proc pseudo filesystem is where the Linux kernel exposes a lot of information. You might want to look around in /proc/net, especially /proc/net/dev and /proc/net/wireless. See the documentation, and some more information.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • @dvp The `/proc` filesystem is where the Linux kernel makes a lot of information about processes and its internal state available for reading and sometimes writing. I've added some documentation links to my answer. – Roland Smith Jul 08 '14 at 21:00
  • OK Thanks, what I've seen so far is good. Will this be the same across all linux? – WraithWireless Jul 08 '14 at 21:36
  • 1
    @dvp Yes, basically. It is a filesystem made available by the kernel. The only way it wouldn't work is if the `/proc` filesystem isn't mounted. But a lot of tools depend on it, so it is generally always mounted. There are some differences in `/proc` layout between major kernel versions, though. – Roland Smith Jul 08 '14 at 21:47
1

So, finally got around to writing own code - interfaces directly with netlink and ioctl to handle iw and ifconfig respectively - hosted on https://github.com/wraith-wireless/pyric and https://pypi.python.org/pypi/PyRIC/

WraithWireless
  • 634
  • 9
  • 21