The file /var/run/utmp contains information about the active sessions on terminals. It can be parsed with:
import struct
sig = 'hi32s4s32s256shhiii36x'
size = struct.calcsize(sig)
file = open('/var/run/utmp', 'rb')
chunk = file.read(size)
entrys = []
while len(chunk) == size:
entry = struct.unpack(chunk)
entrys.append()
chunk = file.read(size)
With that approach I can gather the needed data to choose a terminal and write to that terminal.