Clear telnet patch voor rdl achter sommige N-porten die reverse telnet gebruiken
This commit is contained in:
43
lmwsip.py
43
lmwsip.py
@@ -28,7 +28,7 @@ Support for:
|
|||||||
|
|
||||||
def __init__(self, user=None, password=None,
|
def __init__(self, user=None, password=None,
|
||||||
host="sip-lmw.rws.nl", port=443, meetnet="LMW", ssl = True,
|
host="sip-lmw.rws.nl", port=443, meetnet="LMW", ssl = True,
|
||||||
check_ssl = True, timeout = 10, log = None):
|
check_ssl = True, timeout = 10, log = None, cleartelnet = False):
|
||||||
"""LmwSip(user, password, [host], [port], [meetnet], [ssl], [check_ssl], [timeout], [log])
|
"""LmwSip(user, password, [host], [port], [meetnet], [ssl], [check_ssl], [timeout], [log])
|
||||||
|
|
||||||
user(optinal): Lmw user name
|
user(optinal): Lmw user name
|
||||||
@@ -40,6 +40,7 @@ ssl(optional): Default true
|
|||||||
check_ssl(optional): true
|
check_ssl(optional): true
|
||||||
timeout(optional): 10
|
timeout(optional): 10
|
||||||
log(optional): None
|
log(optional): None
|
||||||
|
cleartelnet: False
|
||||||
|
|
||||||
Opens the connection and logs in
|
Opens the connection and logs in
|
||||||
"""
|
"""
|
||||||
@@ -52,6 +53,7 @@ Opens the connection and logs in
|
|||||||
self.check_ssl = check_ssl
|
self.check_ssl = check_ssl
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
self._socket = None
|
self._socket = None
|
||||||
|
self.cleartelnet = cleartelnet
|
||||||
if (log != None):
|
if (log != None):
|
||||||
self.log = log
|
self.log = log
|
||||||
self.log.debug("LmwSip.init")
|
self.log.debug("LmwSip.init")
|
||||||
@@ -156,28 +158,32 @@ send a sip command to the server
|
|||||||
|
|
||||||
recieve a answer from the sip server
|
recieve a answer from the sip server
|
||||||
"""
|
"""
|
||||||
buf=""
|
stringbuf=""
|
||||||
while self._socket != None and re.search("\r$", buf) == None:
|
while self._socket != None and re.search("\r$", buf) == None:
|
||||||
try:
|
try:
|
||||||
self.log.debug("LmwSip.recv: Waiting for data");
|
self.log.debug("LmwSip.recv: Waiting for data");
|
||||||
b = self._socket.recv(4096).decode('utf-8')
|
bytebuf = self._socket.recv(4096)
|
||||||
if (b == ""):
|
if self.cleartelnet:
|
||||||
self.log.error("SipLmw.recv: socket closed")
|
while bytebuf[0] == '\xff':
|
||||||
self.closesocket()
|
self.log.info("SipLmw.recv: telnet data: %s", bytebuf)
|
||||||
raise LmwSipConnectError("LmwSip.recv: Socket close")
|
bytebuf = self._socket.recv(4096)
|
||||||
else:
|
|
||||||
buf += b
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.log.error("SipLmw.recv: socket timeout: %s", e)
|
self.log.error("SipLmw.recv: socket timeout: %s", e)
|
||||||
self.closesocket()
|
self.closesocket()
|
||||||
raise LmwSipConnectError("LmwSip.recv: Socket timeout")
|
raise LmwSipConnectError("LmwSip.recv: Socket timeout")
|
||||||
|
try:
|
||||||
|
stringbuf += bytebuf.decode('utf-8')
|
||||||
|
except Exception as e:
|
||||||
|
self.log.error("SipLmw.recv: decode error: %s", e)
|
||||||
|
self.closesocket()
|
||||||
|
raise LmwSipDecodeError("LmwSip.recv: decode error", bytebuf)
|
||||||
if self._socket == None:
|
if self._socket == None:
|
||||||
self.log.warn("LmwSip.recv: No connection")
|
self.log.warn("LmwSip.recv: No connection")
|
||||||
elif buf[0] != '!':
|
elif stringbuf[0] != '!':
|
||||||
self.log.warn("LmwSip.recv: Sip error: %s" % buf.strip('\r'))
|
self.log.warn("LmwSip.recv: Sip error: %s" % stringbuf.strip('\r'))
|
||||||
else:
|
else:
|
||||||
self.log.debug("LmwSip.recv: result: %s" % buf.strip('\r'))
|
self.log.debug("LmwSip.recv: result: %s" % stringbuf.strip('\r'))
|
||||||
return(buf)
|
return(stringbuf)
|
||||||
|
|
||||||
def login(self):
|
def login(self):
|
||||||
"""login()
|
"""login()
|
||||||
@@ -420,6 +426,17 @@ class LmwSipConnectError(Exception):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return(self.message)
|
return(self.message)
|
||||||
|
|
||||||
|
class LmwSipDecodeError(Exception):
|
||||||
|
"""Connection exceptions for LmwSip"""
|
||||||
|
|
||||||
|
def __init__(self, message, buf):
|
||||||
|
self.message = message
|
||||||
|
self.buf = buf
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return(self.message + ":" + buf)
|
||||||
|
|
||||||
|
|
||||||
class LmwLoginFailure(Exception):
|
class LmwLoginFailure(Exception):
|
||||||
"""Exception from LmwSip on login failure"""
|
"""Exception from LmwSip on login failure"""
|
||||||
|
|
||||||
|
5
siprun
5
siprun
@@ -10,6 +10,8 @@ def main():
|
|||||||
parser = argparse.ArgumentParser(description="Run a sip file.")
|
parser = argparse.ArgumentParser(description="Run a sip file.")
|
||||||
parser.add_argument("-u", "--unencrypted", action="store_true",
|
parser.add_argument("-u", "--unencrypted", action="store_true",
|
||||||
help="Run a sip connection without ssl")
|
help="Run a sip connection without ssl")
|
||||||
|
parser.add_argument("-c", "--cleartelnet", action="store_true",
|
||||||
|
help="Clear telnet protocol in tcp session")
|
||||||
parser.add_argument("-H", "--host", action='store',
|
parser.add_argument("-H", "--host", action='store',
|
||||||
default="sip-lmw.rws.nl",
|
default="sip-lmw.rws.nl",
|
||||||
help="Host to connect to")
|
help="Host to connect to")
|
||||||
@@ -27,7 +29,8 @@ def main():
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
lmwsip = LmwSip(host=args.host, port=args.port,
|
lmwsip = LmwSip(host=args.host, port=args.port,
|
||||||
ssl=not args.unencrypted)
|
ssl=not args.unencrypted,
|
||||||
|
cleartelnet=args.cleartelnet)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Connect to lmw failed: %s" % e)
|
print("Connect to lmw failed: %s" % e)
|
||||||
exit(1)
|
exit(1)
|
||||||
|
Reference in New Issue
Block a user