Any chance of getting this to run on OSX?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Any chance of getting this to run on OSX?

Leo-2
There is this simple python script:
http://coderstalk.blogspot.com/2010/02/create-network-interfaces-list-using.html

Any idea how to fix it so that it runs on OSX? Thanks. Leo

------------------------
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', '' * 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('', 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

_______________________________________________
Pythonmac-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG
Reply | Threaded
Open this post in threaded view
|

Re: Any chance of getting this to run on OSX?

Nicholas Riley-4
On Sun, Feb 26, 2012 at 10:50:27PM +0800, Leo wrote:
> There is this simple python script:
> http://coderstalk.blogspot.com/2010/02/create-network-interfaces-list-using.html
>
> Any idea how to fix it so that it runs on OSX? Thanks. Leo

That script is an excellent example of how not to do things; it makes
assumptions about constant values and structure layout that may not
hold true on one operating system, much less between operating
systems.

You are almost certainly better off using another mechanism.  What
interface information are you trying to obtain?

--
Nicholas Riley <[hidden email]>
_______________________________________________
Pythonmac-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG
Reply | Threaded
Open this post in threaded view
|

Re: Any chance of getting this to run on OSX?

Leo-2
On 2012-02-27 08:44 +0800, Nicholas Riley wrote:
> That script is an excellent example of how not to do things; it makes
> assumptions about constant values and structure layout that may not
> hold true on one operating system, much less between operating
> systems.
>
> You are almost certainly better off using another mechanism.  What
> interface information are you trying to obtain?

The interface names and the addresses bound to them. Much like
network-interface-list (http://paste.pocoo.org/show/556841/) in Emacs,
which returns:

  (("en1" . [10 0 0 114 0]) ("lo0" . [127 0 0 1 0]))

Leo

_______________________________________________
Pythonmac-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG
Reply | Threaded
Open this post in threaded view
|

Re: Any chance of getting this to run on OSX?

Jan Flyborg
Since you probably are going to develop platform dependent code
anyway, why not just parse the output from the following command?

system_profiler -xml SPNetworkDataType

Cheers
    //Jan

On Mon, Feb 27, 2012 at 07:24, Leo <[hidden email]> wrote:

> On 2012-02-27 08:44 +0800, Nicholas Riley wrote:
>> That script is an excellent example of how not to do things; it makes
>> assumptions about constant values and structure layout that may not
>> hold true on one operating system, much less between operating
>> systems.
>>
>> You are almost certainly better off using another mechanism.  What
>> interface information are you trying to obtain?
>
> The interface names and the addresses bound to them. Much like
> network-interface-list (http://paste.pocoo.org/show/556841/) in Emacs,
> which returns:
>
>  (("en1" . [10 0 0 114 0]) ("lo0" . [127 0 0 1 0]))
>
> Leo
>
> _______________________________________________
> Pythonmac-SIG maillist  -  [hidden email]
> http://mail.python.org/mailman/listinfo/pythonmac-sig
> unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG
_______________________________________________
Pythonmac-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG