showing interfaces

79 views
Skip to first unread message

Umarzuki Mochlis

unread,
Feb 3, 2010, 10:48:10 AM2/3/10
to pyth...@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
http://debmal.my

Marcus

unread,
Feb 3, 2010, 11:19:15 AM2/3/10
to pyth...@googlegroups.com
hmm since u asking this, anyone here played with snmpd before?
what is the easiest snmpd.conf file that allows public query on CPU and harddisk usage.....

Umarzuki Mochlis wrote:
--
You received this message because you are subscribed to the Google Groups "python.my" group.
To post to this group, send email to pyth...@googlegroups.com.
To unsubscribe from this group, send email to pythonmy+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pythonmy?hl=en.

ApOgEE

unread,
Feb 3, 2010, 6:16:28 PM2/3/10
to pyth...@googlegroups.com
Hi Umarzuki,

I made the answer on my blog. check it out at http://coderstalk.blogspot.com/2010/02/create-network-interfaces-list-using.html


On Thu, Feb 4, 2010 at 12:19 AM, Marcus <mar...@python.my> wrote:
hmm since u asking this, anyone here played with snmpd before?
what is the easiest snmpd.conf file that allows public query on CPU and harddisk usage.....


Umarzuki Mochlis wrote:
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
http://debmal.my
--



--
Best Wishes,

M. Fauzilkamil Zainuddin
Software Engineer
Persiasys Sdn. Bhd.
----------------------------------------------------
ApOgEE a.k.a JeRuNgKuN
----------------------------------------------------
https://edge.launchpad.net/~apogee - ApOgEE on LaunchPad
http://artofapogee.blogspot.com - Art Of ApOgEE
http://coderstalk.blogspot.com - Coder's Talk
----------------------------------------------------

Umarzuki Mochlis

unread,
Feb 3, 2010, 8:55:07 PM2/3/10
to pyth...@googlegroups.com


2010/2/4 Marcus <mar...@python.my>

hmm since u asking this, anyone here played with snmpd before?
what is the easiest snmpd.conf file that allows public query on CPU and harddisk usage.....

i had a couple of time only when i had to study how to use nagios
maybe this configuration can help http://umarzuki.org/blogku/?p=157
the article is in malay
 

Umarzuki Mochlis wrote:
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
http://debmal.my
--
You received this message because you are subscribed to the Google Groups "python.my" group.
To post to this group, send email to pyth...@googlegroups.com.
To unsubscribe from this group, send email to pythonmy+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pythonmy?hl=en.

--
You received this message because you are subscribed to the Google Groups "python.my" group.
To post to this group, send email to pyth...@googlegroups.com.
To unsubscribe from this group, send email to pythonmy+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pythonmy?hl=en.

sweemeng ng

unread,
Feb 3, 2010, 8:59:45 PM2/3/10
to pyth...@googlegroups.com
one way to do it, is to use the subprocess library in python 2.6 - 3.x

or if you use a older version of python, you can use popen

This will call create a process in the OS, so you can execute all the command on the os itself. guarantee to work on all os..

Izhar Firdaus

unread,
Feb 3, 2010, 9:03:19 PM2/3/10
to pyth...@googlegroups.com
On Thu, Feb 4, 2010 at 9:59 AM, sweemeng ng <swee...@gmail.com> wrote:
> one way to do it, is to use the subprocess library in python 2.6 - 3.x
> or if you use a older version of python, you can use popen
> This will call create a process in the OS, so you can execute all the
> command on the os itself. guarantee to work on all os..

popen _is_ in the subprocess library (or do u mean os.popen or
popen2?) , and subprocess been around for ages, since 2.4

http://docs.python.org/dev/library/subprocess

no guarantee to work on all os, as not all os have the same command tools ..

--
Mohd Izhar Firdaus Bin Ismail
Amano Hikaru 天野晃 「あまの ひかる」
Fedora Malaysia Contributor & Ambassador
http://fedoraproject.org/wiki/MohdIzharFirdaus
http://blog.kagesenshi.org
92C2 B295 B40B B3DC 6866 5011 5BD2 584A 8A5D 7331

Izhar Firdaus

unread,
Feb 3, 2010, 9:04:23 PM2/3/10
to pyth...@googlegroups.com
On Thu, Feb 4, 2010 at 10:03 AM, Izhar Firdaus <kagese...@gmail.com> wrote:
> On Thu, Feb 4, 2010 at 9:59 AM, sweemeng ng <swee...@gmail.com> wrote:
>> one way to do it, is to use the subprocess library in python 2.6 - 3.x
>> or if you use a older version of python, you can use popen
>> This will call create a process in the OS, so you can execute all the
>> command on the os itself. guarantee to work on all os..
>
> popen _is_ in the subprocess library (or do u mean os.popen or
> popen2?) , and subprocess been around for ages, since 2.4
>
>  http://docs.python.org/dev/library/subprocess
>
> no guarantee to work on all os, as not all os have the same command tools ..

and different OS might have different output format of a same tool ..

ApOgEE

unread,
Feb 3, 2010, 9:46:15 PM2/3/10
to pyth...@googlegroups.com
I wonder... I may have forget to paste the code here...

I don't know if this have answered your question. Comment please

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


-------------
more info about this code on http://coderstalk.blogspot.com/2010/02/create-network-interfaces-list-using.html

Umarzuki Mochlis

unread,
Feb 3, 2010, 10:04:24 PM2/3/10
to pyth...@googlegroups.com
AFAIK, your code works
thanks. 

I'm just curious of how long the (python) code would be to achieve the same with bash

2010/2/4 ApOgEE <jeru...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "python.my" group.
To post to this group, send email to pyth...@googlegroups.com.
To unsubscribe from this group, send email to pythonmy+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pythonmy?hl=en.

E A Faisal

unread,
Feb 3, 2010, 10:42:43 PM2/3/10
to pyth...@googlegroups.com
Since the question is Linux specific, wouldn't it be easier to just parse /proc/net/dev ?

ApOgEE

unread,
Feb 4, 2010, 1:10:45 AM2/4/10
to pyth...@googlegroups.com
On Thu, Feb 4, 2010 at 11:42 AM, E A Faisal <eafa...@gmail.com> wrote:
Since the question is Linux specific, wouldn't it be easier to just parse /proc/net/dev ?

 
E A Faisal, yes it is... I'm just having fun coding. So, I tried your suggestion and here is the second version of the morning code:
----------
#!/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]

----------
Read more info about this code at http://coderstalk.blogspot.com/2010/02/print-active-network-interface-list-in.html

Izhar Firdaus

unread,
Feb 4, 2010, 1:23:08 AM2/4/10
to pyth...@googlegroups.com
On Thu, Feb 4, 2010 at 2:10 PM, ApOgEE <jeru...@gmail.com> wrote:
> On Thu, Feb 4, 2010 at 11:42 AM, E A Faisal <eafa...@gmail.com> wrote:
>>
>> Since the question is Linux specific, wouldn't it be easier to just parse
>> /proc/net/dev ?
>
>
> E A Faisal, yes it is... I'm just having fun coding. So, I tried your
> suggestion and here is the second version of the morning code:
erm ......

for l in open('/proc/net/dev'):
if ':' in l:
print l.split(':')[0].strip()

> --
> You received this message because you are subscribed to the Google Groups
> "python.my" group.
> To post to this group, send email to pyth...@googlegroups.com.
> To unsubscribe from this group, send email to
> pythonmy+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pythonmy?hl=en.
>

--

ApOgEE

unread,
Feb 4, 2010, 1:33:35 AM2/4/10
to pyth...@googlegroups.com
On Thu, Feb 4, 2010 at 2:23 PM, Izhar Firdaus <kagese...@gmail.com> wrote:
On Thu, Feb 4, 2010 at 2:10 PM, ApOgEE <jeru...@gmail.com> wrote:
> On Thu, Feb 4, 2010 at 11:42 AM, E A Faisal <eafa...@gmail.com> wrote:
>>
>> Since the question is Linux specific, wouldn't it be easier to just parse
>> /proc/net/dev ?
>
>
> E A Faisal, yes it is... I'm just having fun coding. So, I tried your
> suggestion and here is the second version of the morning code:
erm ......

for l in open('/proc/net/dev'):
   if ':' in l:
      print l.split(':')[0].strip()


Kage, simple codes are fun too... but a bit difficult to understand (seeing thru the eyes of newbies... :p). Here's a fix maybe... To remove the inactive interfaces:


for l in open('/proc/net/dev'):
   if ':' in l:
         if int(l.split(':')[1].replace(' ','')) > 0:

               print l.split(':')[0].strip()

Izhar Firdaus

unread,
Feb 4, 2010, 1:45:10 AM2/4/10
to pyth...@googlegroups.com
On Thu, Feb 4, 2010 at 2:33 PM, ApOgEE <jeru...@gmail.com> wrote:
> On Thu, Feb 4, 2010 at 2:23 PM, Izhar Firdaus <kagese...@gmail.com>
> wrote:
>>
>> On Thu, Feb 4, 2010 at 2:10 PM, ApOgEE <jeru...@gmail.com> wrote:
>> > On Thu, Feb 4, 2010 at 11:42 AM, E A Faisal <eafa...@gmail.com> wrote:
>> >>
>> >> Since the question is Linux specific, wouldn't it be easier to just
>> >> parse
>> >> /proc/net/dev ?
>> >
>> >
>> > E A Faisal, yes it is... I'm just having fun coding. So, I tried your
>> > suggestion and here is the second version of the morning code:
>> erm ......
>>
>> for l in open('/proc/net/dev'):
>>    if ':' in l:
>>       print l.split(':')[0].strip()
>>
>
> Kage, simple codes are fun too... but a bit difficult to understand (seeing
> thru the eyes of newbies... :p). Here's a fix maybe... To remove the
> inactive interfaces:

at least its not like perl .. :P

>
> for l in open('/proc/net/dev'):
>    if ':' in l:
>          if int(l.split(':')[1].replace(' ','')) > 0:
>                print l.split(':')[0].strip()

use strip() is better than replace(' ','') ..

and btw .. having data transfered by the interface doesnt necessarily
means the interface is up ..

compare the outputs ..

from subprocess import Popen,PIPE

p = Popen('ifconfig',stdout=PIPE)
p.wait()
for l in p.stdout:
ls = l.split(' ')
if ls[0].strip():
print ls[0].strip()

print '----'

for l in open('/proc/net/dev'):
if ':' in l:
print l.split(':')[0].strip()

print '----'

for l in open('/proc/net/dev'):
if ':' in l:
if int(l.split(':')[1].replace(' ','')) > 0:
print l.split(':')[0].strip()


my output:

eth2
lo
tun0
virbr0
wlan1
wmaster0
----
lo
eth2
wmaster0
wlan1
vboxnet0
pan0
virbr0
tun0
----
lo
wlan1
virbr0
tun0


>
>
> --
> Best Wishes,
>
> M. Fauzilkamil Zainuddin
> Software Engineer
> Persiasys Sdn. Bhd.
> ----------------------------------------------------
> ApOgEE a.k.a JeRuNgKuN
> ----------------------------------------------------
> https://edge.launchpad.net/~apogee - ApOgEE on LaunchPad
> http://artofapogee.blogspot.com - Art Of ApOgEE
> http://coderstalk.blogspot.com - Coder's Talk
> ----------------------------------------------------
>

Izhar Firdaus

unread,
Feb 4, 2010, 1:46:03 AM2/4/10
to pyth...@googlegroups.com
On Thu, Feb 4, 2010 at 2:45 PM, Izhar Firdaus <kagese...@gmail.com> wrote:
> On Thu, Feb 4, 2010 at 2:33 PM, ApOgEE <jeru...@gmail.com> wrote:
>> On Thu, Feb 4, 2010 at 2:23 PM, Izhar Firdaus <kagese...@gmail.com>
>> wrote:
>>>
>>> On Thu, Feb 4, 2010 at 2:10 PM, ApOgEE <jeru...@gmail.com> wrote:
>>> > On Thu, Feb 4, 2010 at 11:42 AM, E A Faisal <eafa...@gmail.com> wrote:
>>> >>
>>> >> Since the question is Linux specific, wouldn't it be easier to just
>>> >> parse
>>> >> /proc/net/dev ?
>>> >
>>> >
>>> > E A Faisal, yes it is... I'm just having fun coding. So, I tried your
>>> > suggestion and here is the second version of the morning code:
>>> erm ......
>>>
>>> for l in open('/proc/net/dev'):
>>>    if ':' in l:
>>>       print l.split(':')[0].strip()
>>>
>>
>> Kage, simple codes are fun too... but a bit difficult to understand (seeing
>> thru the eyes of newbies... :p). Here's a fix maybe... To remove the
>> inactive interfaces:
>
> at least its not like perl .. :P
>
>>
>> for l in open('/proc/net/dev'):
>>    if ':' in l:
>>          if int(l.split(':')[1].replace(' ','')) > 0:
>>                print l.split(':')[0].strip()
>

ignore this


> use strip() is better than replace(' ','') ..

hehe :P .. different usecase ..

Reply all
Reply to author
Forward
0 new messages