Support Center

Window Size Sync Script

The following script, 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:
# Copyright 2013 by Akkana Peck -- share and enjoy under the GPL v2 or later.
# Original URL:
# 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"

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)

while True:
r, w, e =[fd], [], [])
if r:
output =
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"