February 4, 2010

0

Print Active Network Interface List in Linux using Python

I'm checking the replies of my previous code on Create network interfaces list using Python. E A Faisal suggestion to use /proc/net/dev is somehow seems interesting to me. Now, let's do some python code for fun before going out to lunch.

Here is my code to get the same result as my previous python code to list the network interfaces:

#!/usr/bin/python

# read the file /proc/net/dev
f = open('/proc/net/dev','r')

# put the content to list
ifacelist = f.read().split('\n')

# close the file
f.close()

# remove 2 lines header
ifacelist.pop(0)
ifacelist.pop(0)

# loop to check each line
for line in ifacelist:

ifacedata = line.replace(' ','').split(':')

# check the data have 2 elements
if len(ifacedata) == 2:

# check the interface is up (Transmit/Receive data)
if int(ifacedata[1]) > 0:

# print the interface
print ifacedata[0]

Seems like it is working on my ubuntu linux (in the office ). I believe it should possibly work on other linux without any issue with that SIOCGIFCONF and ioctl. And the code is even simpler.

That's all for now... I'm going out for lunch. Happy python coding!


Bookmark This Article:
Feed Me Digg Technorati del.icio.us Best to Stumbleupon Reddit Blinklist Furl Spurl Yahoo Simpy




0

Create network interfaces list using Python

While checking my email this morning, I found a python question on python.my mailing list which sound like this:

Date: Wed, 3 Feb 2010 23:48:10 +0800
Message-ID: <52d26d931002030748pd2c6321p1290b1eeee703...@mail.gmail.com>
Subject: showing interfaces
From: Umarzuki Mochlis <umarz...@gmail.com>
To: pythonmy@googlegroups.com

Hi all,

I wonder how I can output network interfaces with python the same
way I can with these commands on linux

sudo ifconfig | cut -d " " -f 1 > ifconfig.txt
sed '/ *#/d; /^ *$/d' < ifconfig.txt

--
Regards,

Umarzuki Mochlis

For those who couldn't imagine the output of those two lines command in the question, here is the explanation:
  • The first command write to 'ifconfig.txt' the name of up interfaces which comes from ifconfig output and remove other unwanted informations.
  • the second line read the 'ifconfig.txt' file, remove empty lines and print the list on the screen.
You can read the discussion thread on python.my mailing list: showing interfaces

Answer:
Here is how you can create network interfaces list with python:

import array
import struct
import socket
import fcntl

SIOCGIFCONF = 0x8912 #define SIOCGIFCONF
BYTES = 4096 # Simply define the byte size

# get_iface_list function definition
# this function will return array of all 'up' interfaces
def get_iface_list():
# create the socket object to get the interface list
sck = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# prepare the struct variable
names = array.array('B', '\0' * BYTES)

# the trick is to get the list from ioctl
bytelen = struct.unpack('iL', fcntl.ioctl(sck.fileno(), SIOCGIFCONF, struct.pack('iL', BYTES, names.buffer_info()[0])))[0]

# convert it to string
namestr = names.tostring()

# return the interfaces as array
return [namestr[i:i+32].split('\0', 1)[0] for i in range(0, bytelen, 32)]

# now, use the function to get the 'up' interfaces array
ifaces = get_iface_list()

# well, what to do? print it out maybe...
for iface in ifaces:
print iface

This code is tested to be working in my ubuntu linux. Since I made SIOCGIFCONF ioctl number (0x8912) hardcoded, it may seems broken on other UNIX like system. However, you may modified the code to be compatible with your system when you understand it. I hope this snippet can help others too. Enjoy coding!



Bookmark This Article:
Feed Me Digg Technorati del.icio.us Best to Stumbleupon Reddit Blinklist Furl Spurl Yahoo Simpy

December 15, 2009

1

Howto Backup and Restore SD card image with tar and dd

Just my quick post today... I'm quite busy these days...



I normally backup my embedded system SD card image using dd and the output size will be 969M. To reduce the size, I'm using tar and gzip to compress the image file. Here is the command:

# dd if=/dev/sdd of=sd1gb.dd
$ tar zcvf sd1gb.dd.tar.gz


This way I will have the image in tar.gz file which is only 218MB. And delete the dd file.

However, to use the tar.gz, we don't have to extract the file because we can only use a single command like this:

# tar Ozxf sd1gb.dd.tar.gz | dd of=/dev/sdd


That's all for today... see u later!


Bookmark This Article:
Feed Me Digg Technorati del.icio.us Best to Stumbleupon Reddit Blinklist Furl Spurl Yahoo Simpy