From 883a85abf3c02828f9573382f1594f4587d15dda Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 18 Dec 2013 07:17:45 -0500 Subject: [PATCH 1/2] Adding NAWS to initial connection, available at self.WIDTH and self.HEIGHT --- telnetsrv/telnetsrvlib.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/telnetsrv/telnetsrvlib.py b/telnetsrv/telnetsrvlib.py index 2ed6a3f..75d2664 100755 --- a/telnetsrv/telnetsrvlib.py +++ b/telnetsrv/telnetsrvlib.py @@ -24,13 +24,13 @@ import SocketServer import socket +import struct import sys import traceback import curses.ascii import curses.has_key import curses import logging -import re #if not hasattr(socket, 'SHUT_RDWR'): # socket.SHUT_RDWR = 2 @@ -489,6 +489,12 @@ def streamserver_handle(cls, socket, address): log.debug("Accepted connection, starting telnet session.") cls(request, address, server) + def setnaws(self, naws): + ''' Set width and height of the terminal on initial connection''' + self.WIDTH = struct.unpack('>h', naws[0:2])[0] + self.HEIGHT = struct.unpack('>h', naws[2:4])[0] + log.debug("Set width to %s and height to %s" % (self.WIDTH, self.HEIGHT)) + def setterm(self, term): "Set the curses structures for this terminal" log.debug("Setting termtype to %s" % (term, )) @@ -560,6 +566,8 @@ def options_handler(self, sock, cmd, opt): self.setterm(subreq[2:]) except: log.debug("Terminal type not known") + elif subreq[0] == NAWS: + self.setnaws(subreq[1:]) elif cmd == SB: pass else: From 873172a29b666f92bbb8d9b4eea4bf1da01f3bb4 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 18 Dec 2013 07:22:10 -0500 Subject: [PATCH 2/2] Adding docs for width/height, fixing typo --- README.rst | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 83c25f1..f4069d0 100644 --- a/README.rst +++ b/README.rst @@ -217,6 +217,12 @@ These will be provided for inspection. ``TERM`` String ID describing the currently connected terminal + +``WIDTH`` + Integer describing the width of the terminal at connection time. + +``HEIGHT`` + Integer describing the height of the terminal at connection time. ``username`` Set after authentication succeeds, name of the logged in user. @@ -234,6 +240,7 @@ These will be provided for inspection. Provides some information about the current terminal. ''' self.writeresponse( "Username: %s, terminal type: %s" % (self.username, self.TERM) ) + self.writeresponse( "Width: %s, height: %s" % (self.WIDTH, self.HEIGHT) ) self.writeresponse( "Command history:" ) for c in self.history: self.writeresponse(" %r" % c) @@ -375,7 +382,7 @@ others). import gevent.server server = gevent.server.StreamServer(("", 8023), MyHandler.streamserver_handle) - server.server_forever() + server.serve_forever() Short Example