Support Center

Window Size Sync Script

The following script, resize.py can be run on the remote machine to query the terminal for the current window size and set it appropriately:

#!/usr/bin/env python
#
# Set the TTY window size to match that of the terminal
# when logged in over a serial port.
#
# Source: https://www.decisivetactics.com/support/files/resize.py
#
# CHANGES
# Copyright 2013 by Akkana Peck -- share and enjoy under the GPL v2 or later.
# Original URL: http://shallowsky.com/blog/hardware/serial-24-line-terminals.html
# Modified 24 June 2014 by Chris Kent -- use Xterm window size report.
#
#

import os, sys
import fcntl
import struct
import re
import termios
import select

tty = open('/dev/tty', 'r+')

# Ask for an Xterm window size report by sending "ESC [ 18 t"
# In response, we expect to receive "ESC [ 8; nrows; ncols t"
tty.write('\x1b[18t')
tty.flush()

fd = sys.stdin.fileno()

oldterm = termios.tcgetattr(fd)
newattr = oldterm[:]
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)

oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)

try:
while True:
r, w, e = select.select([fd], [], [])
if r:
output = sys.stdin.read()
break
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)

replycode, rows, cols = map(int, re.findall(r'\d+', output))

# Set TTY window size using TIOCSWINSZ ioctl
fcntl.ioctl(fd, termios.TIOCSWINSZ,
struct.pack("HHHH", rows, cols, 0, 0))

print "Terminal set to", cols, "cols x", rows, "rows"