473,290 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,290 software developers and data experts.

Access to sysctl on FreeBSD?

How do I access the sysctl(3) call from Python on BSD?

Specifically I want to retrieve:

$ sysctl -d net.inet.ip.stats
net.inet.ip.stats: IP statistics (struct ipstat, netinet/ip_var.h)

So I'll need some way of getting to struct ipstat from Python as well

Thanks,
Skye

Jun 27 '08 #1
4 3286
How do I access the sysctl(3) call from Python on BSD?
>
Specifically I want to retrieve:

$ sysctl -d net.inet.ip.stats
net.inet.ip.stats: IP statistics (struct ipstat, netinet/ip_var.h)

So I'll need some way of getting to struct ipstat from Python as well
At the moment, only through ctypes - if you need to use sysctl(3),
and you'll have to manufacture the layout of struct ipstat yourself.

Regards,
Martin
Jun 27 '08 #2
Great, thanks for the help (I'm fairly new to Python, didn't know
about ctypes)

from ctypes import *
libc = CDLL("libc.so.7")
size = c_uint(0)
libc.sysctlbyname("net.inet.ip.stats", None, byref(size), None, 0)
buf = c_char_p(" " * size.value)
libc.sysctlbyname("net.inet.ip.stats", buf, byref(size), None, 0)

So now that I've got the data, can you point me towards docs
explaining how to layout struct ipstat?

Thanks,
Skye

Jun 27 '08 #3
Nevermind, I seem to have found it on my own =]

http://python.org/doc/2.5/lib/module-struct.html

This module performs conversions between Python values and C structs
represented as Python strings. It uses format strings (explained
below) as compact descriptions of the lay-out of the C structs and the
intended conversion to/from Python values. This can be used in
handling binary data stored in files or from network connections,
among other sources.

I freakin' love Python!!

Skye

Jun 27 '08 #4
Skye schrieb:
Great, thanks for the help (I'm fairly new to Python, didn't know
about ctypes)

from ctypes import *
libc = CDLL("libc.so.7")
size = c_uint(0)
libc.sysctlbyname("net.inet.ip.stats", None, byref(size), None, 0)
buf = c_char_p(" " * size.value)
libc.sysctlbyname("net.inet.ip.stats", buf, byref(size), None, 0)
IIUC, sysctlbyname writes the information you requested into the buffer supplied
as second argument. If this is true, then the above code is incorrect.
c_char_p(src) creates an object that shares the buffer with the Python string 'src';
but Python strings are immutable and you must not write ctypes code that modifies them.

Instead, you should code:

from ctypes import *
libc = CDLL("libc.so.7")
size = c_uint(0)
libc.sysctlbyname("net.inet.ip.stats", None, byref(size), None, 0)
buf = create_string_buffer(size.value)
libc.sysctlbyname("net.inet.ip.stats", buf, byref(size), None, 0)

Thomas
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Weston C | last post by:
I'm building a small web application for a friend using PHP. He'd like to use MS Access to keep the data in, and update the data on the site by FTP'ing Access files he edits on his machine up to...
2
by: siliconmike | last post by:
http://dev.mysql.com/doc/mysql/en/freebsd.html says: You can link MySQL on FreeBSD 4.x against the LinuxThreads library, which avoids a few of the problems that the native FreeBSD thread...
13
by: Uwe Mayer | last post by:
<posted & mailed> Hi, AFAICT there seems to be a bug on FreeBSD's Python 2.3.4 open function. The documentation states: > Modes 'r+', 'w+' and 'a+' open the file for updating (note that...
4
by: kerb | last post by:
Hello can you help I am trying to access direct address in ufs filesystem what I am trying to do is for example I have inode 12930123912 and I want to know where on which CHS (cylinder,head...
0
by: Bill Moran | last post by:
I'm having some problems. Hopefully there are some FreeBSD folks here that can help me out, if not, I'll try the FreeBSD lists next. I'm running Postgres 7.4 installed from a just cvsupped...
2
by: Ruslan A Dautkhanov | last post by:
Hello ! I'm about to install O9i on FreeBSD box. uname -a: FreeBSD stat2.scn.ru 5.2.1-RELEASE-p3 FreeBSD 5.2.1-RELEASE-p3 #2: Fri Apr 23 19:19:43 KRAST 2004...
0
by: Akira Kitada | last post by:
Hi list, I was trying to build Python 2.6 on FreeBSD 4.11 and found it failed to build some of the modules. """ Failed to find the necessary bits to build these modules: _bsddb ...
0
by: Akira Kitada | last post by:
Hi Marc-Andre, Thanks for the suggestion. I opened a ticket for this issue: http://bugs.python.org/issue4204 Now I understand the state of the multiprocessing module, but it's too bad to see...
0
by: M.-A. Lemburg | last post by:
On 2008-10-25 20:19, Akira Kitada wrote: Thanks. The errors you are getting appear to be related to either some missing header files or a missing symbol definition to enable these - looking...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.