本文整理汇总了Python中fcntl.ioctl方法的典型用法代码示例。如果您正苦于以下问题:Python fcntl.ioctl方法的具体用法?Python fcntl.ioctl怎么用?Python fcntl.ioctl使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fcntl
的用法示例。
在下文中一共展示了fcntl.ioctl方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mac_set_ip_address
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import ioctl [as 别名]
def mac_set_ip_address(self, dev, ip, serverip, netmask):
ifr = struct.pack('<16sBBHIIIBBHIIIBBHIII',
self.iface_name,
16, socket.AF_INET, 0, struct.unpack('<L', socket.inet_pton(socket.AF_INET, ip))[0], 0, 0,
16, socket.AF_INET, 0, struct.unpack('<L', socket.inet_pton(socket.AF_INET, serverip))[0], 0, 0,
16, 0, 0, struct.unpack('<L', socket.inet_pton(socket.AF_INET, "255.255.255.255"))[0], 0, 0)
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
fcntl.ioctl(sock, self.IOCTL_MACOSX_SIOCAIFADDR, ifr)
except Exception as e:
common.internal_print("Something went wrong with setting up the interface.", -1)
print(e)
sys.exit(-1)
# adding new route for forwarding packets properly.
integer_ip = struct.unpack(">I", socket.inet_pton(socket.AF_INET, serverip))[0]
rangeip = socket.inet_ntop(socket.AF_INET, struct.pack(">I", integer_ip & ((2**int(netmask))-1)<<32-int(netmask)))
ps = subprocess.Popen(["route", "add", "-net", rangeip+"/"+netmask, serverip], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = ps.communicate()
if stderr:
if not "File exists" in stderr:
common.internal_print("Error: adding client route: {0}".format(stderr), -1)
sys.exit(-1)
return
示例2: freebsd_tun_alloc
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import ioctl [as 别名]
def freebsd_tun_alloc(self, dev, flags):
try:
sockfd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ifr = struct.pack('<16si', 'tun', 0)
self.iface_name = fcntl.ioctl(sockfd, self.IOCTL_FREEBSD_SIOCIFCREATE2, ifr)
self.iface_name = self.iface_name.rstrip("\x00")
buff = array.array('c', dev+"\x00")
caddr_t, _ = buff.buffer_info()
ifr = struct.pack('16sP', self.iface_name, caddr_t);
fcntl.ioctl(sockfd, self.IOCTL_FREEBSD_SIOCSIFNAME, ifr)
tun = os.open("/dev/"+self.iface_name, os.O_RDWR | os.O_NONBLOCK)
self.iface_name = dev
except IOError as e:
print e
common.internal_print("Error: Cannot create tunnel. Is {0} in use?".format(dev), -1)
sys.exit(-1)
return tun
示例3: get_termsize
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import ioctl [as 别名]
def get_termsize():
"""Return terminal size as a tuple (height, width)."""
try:
# this works on unix machines
import struct, fcntl, termios
height, width = struct.unpack("hhhh",
fcntl.ioctl(0,termios.TIOCGWINSZ,
"\000"*8))[0:2]
if not (height and width):
height, width = 24, 79
except ImportError:
# for windows machins, use default values
# Does anyone know how to get the console size under windows?
# One approach is:
# http://code.activestate.com/recipes/440694/
height, width = 24, 79
return height, width
示例4: resize_handler
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import ioctl [as 别名]
def resize_handler(self):
"""Send the new window size to conmon."""
def wrapped(signum, frame): # pylint: disable=unused-argument
packed = fcntl.ioctl(self.pseudo_tty.stdout, termios.TIOCGWINSZ,
struct.pack('HHHH', 0, 0, 0, 0))
rows, cols, _, _ = struct.unpack('HHHH', packed)
logging.debug('Resize window(%dx%d) using %s', rows, cols,
self.pseudo_tty.control_socket)
# TODO: Need some kind of timeout in case pipe is blocked
with open(self.pseudo_tty.control_socket, 'w') as skt:
# send conmon window resize message
skt.write('1 {} {}\n'.format(rows, cols))
return wrapped
示例5: __connect
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import ioctl [as 别名]
def __connect(self):
device_addr, hidraw_device, event_device = self.__find_device()
if device_addr is None:
return False
self.__report_fd = os.open(hidraw_device, os.O_RDWR | os.O_NONBLOCK)
self.__fd = FileIO(self.__report_fd, "rb+", closefd=False)
self.__input_device = InputDevice(event_device)
self.__input_device.grab()
buf = bytearray(38)
buf[0] = 0x02
try:
return bool(fcntl.ioctl(self.__fd, 3223734279, bytes(buf)))
except:
pass
if self.recv():
self.update_controller()
示例6: _get_terminal_size_linux
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import ioctl [as 别名]
def _get_terminal_size_linux():
def ioctl_GWINSZ(fd):
try:
import fcntl
import termios
cr = struct.unpack('hh',
fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
return cr
except:
pass
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
try:
cr = (os.environ['LINES'], os.environ['COLUMNS'])
except:
return None
return int(cr[1]), int(cr[0])
示例7: getLocalip
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import ioctl [as 别名]
def getLocalip(interface: str = "wlan0") -> str:
"""This function will return the Local IP Address of the interface"""
if "nux" in sys.platform:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
return socket.inet_ntoa(
fcntl.ioctl(
s.fileno(), 0x8915, struct.pack('256s',interface[:15])
)[20:24]
)
except IOError:
print("{}[!] Error, unable to detect local ip address.".format(Colors.FAIL))
print("[!] Check your connection to network {}".format(Colors.ENDC))
exit()
elif "darwin" in sys.platform:
return [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][0]
示例8: read_byte_data
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import ioctl [as 别名]
def read_byte_data(self, addr, cmd):
"""Read a single byte from the specified cmd register of the device."""
assert (
self._device is not None
), "Bus must be opened before operations are made against it!"
# Build ctypes values to marshall between ioctl and Python.
reg = c_uint8(cmd)
result = c_uint8()
# Build ioctl request.
request = make_i2c_rdwr_data(
[
(addr, 0, 1, pointer(reg)), # Write cmd register.
(addr, I2C_M_RD, 1, pointer(result)), # Read 1 byte as result.
]
)
# Make ioctl call and return result data.
ioctl(self._device.fileno(), I2C_RDWR, request)
return result.value
示例9: get_ip_address
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import ioctl [as 别名]
def get_ip_address(ifname):
"""
Get the ip address from the specified interface.
>>> get_ip_address('eth0')
'192.168.0.7'
@type ifname: string
@param ifname: The interface name. Typical names are C{'eth0'},
C{'wlan0'}, etc.
@rtype: string
@return: The IP address of the specified interface.
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15]))[20:24])
示例10: getAlarmElapsedRealTime
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import ioctl [as 别名]
def getAlarmElapsedRealTime():
### This method is used on Android to get the UP_TIME.
elapsedTime = -1
try:
alarmFile = open("/dev/alarm", 'r')
if alarmFile:
t = timespec()
# ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME) = 0x40086134
result = fcntl.ioctl(alarmFile.fileno(), 0x40086134, t)
if result == 0:
elapsedTime = t.tv_sec
alarmFile.close()
except Exception, e:
log.error(e)
示例11: getAlarmElapsedRealTime
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import ioctl [as 别名]
def getAlarmElapsedRealTime():
### DEPRECATED: This method was used on Android to get the UP_TIME (replaced by monotonicTime())
elapsedTime = -1
try:
alarmFile = open("/dev/alarm", 'r')
if alarmFile:
t = timespec()
# ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME) = 0x40086134
result = fcntl.ioctl(alarmFile.fileno(), 0x40086134, t)
if result == 0:
elapsedTime = t.tv_sec
alarmFile.close()
except Exception, e:
log.error(e)
示例12: send_IOCTL
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import ioctl [as 别名]
def send_IOCTL(ioc, device_name = "wlan0"):
# This code is untested, because our target (BCM43430a1) talks NETLINK
# so on Pi0w sendNL_IOCTL should be used
SIOCDEVPRIVATE = 0x89F0
# create ioctl ifreq
ifr = nexconf.create_ifreq(device_name, ioc)
# debug out
'''
print repr(nexconf.c_struct2str(ifr))
print len(nexconf.c_struct2str(ifr))
print repr(string_at(ifr.ifr_data, sizeof(ioc)))
'''
# send ioctl to kernel via UDP socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
fcntl.ioctl(s.fileno(), SIOCDEVPRIVATE, ifr)
s.close()
示例13: activate_network_interface
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import ioctl [as 别名]
def activate_network_interface(iface):
"""Bring up the given network interface.
@raise OSError: if interface does not exist or permissions are missing
"""
iface = iface.encode()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP)
try:
# Get current interface flags from kernel
ifreq = struct.pack(
_STRUCT_IFREQ_LAYOUT_IFADDR_SAFAMILY, iface, socket.AF_INET, b"0" * 14
)
ifreq = fcntl.ioctl(sock, _SIOCGIFFLAGS, ifreq)
if_flags = struct.unpack(_STRUCT_IFREQ_LAYOUT_IFFLAGS, ifreq)[1]
# Set new flags
ifreq = struct.pack(
_STRUCT_IFREQ_LAYOUT_IFFLAGS, iface, if_flags | _IFF_UP, b"0" * 14
)
fcntl.ioctl(sock, _SIOCSIFFLAGS, ifreq)
finally:
sock.close()
示例14: get_terminal_size
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import ioctl [as 别名]
def get_terminal_size():
fallback = (80, 24)
try:
from shutil import get_terminal_size
except ImportError:
try:
import termios
import fcntl
import struct
call = fcntl.ioctl(0, termios.TIOCGWINSZ, "\x00"*8)
height, width = struct.unpack("hhhh", call)[:2]
except (SystemExit, KeyboardInterrupt):
raise
except:
width = int(os.environ.get('COLUMNS', fallback[0]))
height = int(os.environ.get('COLUMNS', fallback[1]))
# Work around above returning width, height = 0, 0 in Emacs
width = width if width != 0 else fallback[0]
height = height if height != 0 else fallback[1]
return width, height
else:
return get_terminal_size(fallback)
示例15: handleResize
# 需要导入模块: import fcntl [as 别名]
# 或者: from fcntl import ioctl [as 别名]
def handleResize(self, signum, frame):
h,w=array('h', ioctl(sys.stderr,termios.TIOCGWINSZ,'\0'*8))[:2]
self.term_width = w