473,397 Members | 2,077 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,397 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 3290
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.