Last active
June 10, 2024 04:01
-
-
Save pudquick/581a71425439f2cf8f09 to your computer and use it in GitHub Desktop.
Calling sysctl from python via ctypes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ctypes import CDLL, c_uint, byref, create_string_buffer | |
from ctypes.util import find_library | |
libc = CDLL(find_library("c")) | |
def sysctl(name, isString=True): | |
size = c_uint(0) | |
# Find out how big our buffer will be | |
libc.sysctlbyname(name, None, byref(size), None, 0) | |
# Make the buffer | |
buf = create_string_buffer(size.value) | |
# Re-run, but provide the buffer | |
libc.sysctlbyname(name, buf, byref(size), None, 0) | |
if isString: | |
return buf.value | |
else: | |
return buf.raw |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Kentzo,
Thanks for the assist. I figured it was something I was overlooking. I can confirm that this works properly now after making that change.