wpaspy: Add support for UDP connection
hostname and port can now be specified when using wpaspy.Ctrl, so we can connect to remote clients now. This can also be tested using test.py application with ./test.py <hostname> <port> Signed-off-by: Janusz Dziedzic <janusz.dziedzic@tieto.com>
This commit is contained in:
parent
3e67171846
commit
a2c88a8025
2 changed files with 67 additions and 18 deletions
|
@ -7,13 +7,23 @@
|
||||||
# See README for more details.
|
# See README for more details.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
import time
|
import time
|
||||||
import wpaspy
|
import wpaspy
|
||||||
|
|
||||||
wpas_ctrl = '/var/run/wpa_supplicant'
|
wpas_ctrl = '/var/run/wpa_supplicant'
|
||||||
|
|
||||||
def wpas_connect():
|
def wpas_connect(host=None, port=9877):
|
||||||
ifaces = []
|
ifaces = []
|
||||||
|
|
||||||
|
if host != None:
|
||||||
|
try:
|
||||||
|
wpas = wpaspy.Ctrl(host, port)
|
||||||
|
return wpas
|
||||||
|
except:
|
||||||
|
print "Could not connect to host: ", host
|
||||||
|
return None
|
||||||
|
|
||||||
if os.path.isdir(wpas_ctrl):
|
if os.path.isdir(wpas_ctrl):
|
||||||
try:
|
try:
|
||||||
ifaces = [os.path.join(wpas_ctrl, i) for i in os.listdir(wpas_ctrl)]
|
ifaces = [os.path.join(wpas_ctrl, i) for i in os.listdir(wpas_ctrl)]
|
||||||
|
@ -34,15 +44,15 @@ def wpas_connect():
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main(host=None, port=9877):
|
||||||
print "Testing wpa_supplicant control interface connection"
|
print "Testing wpa_supplicant control interface connection"
|
||||||
wpas = wpas_connect()
|
wpas = wpas_connect(host, port)
|
||||||
if wpas is None:
|
if wpas is None:
|
||||||
return
|
return
|
||||||
print "Connected to wpa_supplicant"
|
print "Connected to wpa_supplicant"
|
||||||
print wpas.request('PING')
|
print wpas.request('PING')
|
||||||
|
|
||||||
mon = wpas_connect()
|
mon = wpas_connect(host, port)
|
||||||
if mon is None:
|
if mon is None:
|
||||||
print "Could not open event monitor connection"
|
print "Could not open event monitor connection"
|
||||||
return
|
return
|
||||||
|
@ -66,4 +76,7 @@ def main():
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) > 2:
|
||||||
|
main(host=sys.argv[1], port=int(sys.argv[2]))
|
||||||
|
else:
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -7,16 +7,30 @@
|
||||||
# See README for more details.
|
# See README for more details.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import stat
|
||||||
import socket
|
import socket
|
||||||
import select
|
import select
|
||||||
|
|
||||||
counter = 0
|
counter = 0
|
||||||
|
|
||||||
class Ctrl:
|
class Ctrl:
|
||||||
def __init__(self, path):
|
def __init__(self, path, port=9877):
|
||||||
global counter
|
global counter
|
||||||
self.started = False
|
self.started = False
|
||||||
self.attached = False
|
self.attached = False
|
||||||
|
self.path = path
|
||||||
|
self.port = port
|
||||||
|
|
||||||
|
try:
|
||||||
|
mode = os.stat(path).st_mode
|
||||||
|
if stat.S_ISSOCK(mode):
|
||||||
|
self.udp = False
|
||||||
|
else:
|
||||||
|
self.udp = True
|
||||||
|
except:
|
||||||
|
self.udp = True
|
||||||
|
|
||||||
|
if not self.udp:
|
||||||
self.s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
|
self.s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
|
||||||
self.dest = path
|
self.dest = path
|
||||||
self.local = "/tmp/wpa_ctrl_" + str(os.getpid()) + '-' + str(counter)
|
self.local = "/tmp/wpa_ctrl_" + str(os.getpid()) + '-' + str(counter)
|
||||||
|
@ -28,6 +42,24 @@ class Ctrl:
|
||||||
self.s.close()
|
self.s.close()
|
||||||
os.unlink(self.local)
|
os.unlink(self.local)
|
||||||
raise
|
raise
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
ai_list = socket.getaddrinfo(path, port, socket.AF_INET,
|
||||||
|
socket.SOCK_DGRAM)
|
||||||
|
for af, socktype, proto, cn, sockaddr in ai_list:
|
||||||
|
self.sockaddr = sockaddr
|
||||||
|
break
|
||||||
|
self.s = socket.socket(af, socktype)
|
||||||
|
self.s.settimeout(5)
|
||||||
|
self.s.sendto("GET_COOKIE", sockaddr)
|
||||||
|
reply, server = self.s.recvfrom(4096)
|
||||||
|
self.cookie = reply
|
||||||
|
self.port = port
|
||||||
|
except:
|
||||||
|
print "connect exception ", path, str(port)
|
||||||
|
if self.s != None:
|
||||||
|
self.s.close()
|
||||||
|
raise
|
||||||
self.started = True
|
self.started = True
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
|
@ -43,10 +75,14 @@ class Ctrl:
|
||||||
pass
|
pass
|
||||||
if self.started:
|
if self.started:
|
||||||
self.s.close()
|
self.s.close()
|
||||||
|
if not self.udp:
|
||||||
os.unlink(self.local)
|
os.unlink(self.local)
|
||||||
self.started = False
|
self.started = False
|
||||||
|
|
||||||
def request(self, cmd, timeout=10):
|
def request(self, cmd, timeout=10):
|
||||||
|
if self.udp:
|
||||||
|
self.s.sendto(self.cookie + cmd, self.sockaddr)
|
||||||
|
else:
|
||||||
self.s.send(cmd)
|
self.s.send(cmd)
|
||||||
[r, w, e] = select.select([self.s], [], [], timeout)
|
[r, w, e] = select.select([self.s], [], [], timeout)
|
||||||
if r:
|
if r:
|
||||||
|
|
Loading…
Reference in a new issue