notify user from service with python
Question: In the following text the word "write" refers to the unix write command which
writes a message to another user's tty.

I have a service running (in inetd, but that shouldn't matter) which needs to
notify an arbitrary user. Until now I tried to call the write command with
subprocess. But that doesn't work always and on some machines leads to an
error message like:

    
    
    write: you are uid 65534, but your login is as uid 1000
    

(please note that the service is running as nobody.) Write permission to the
terminal is turned on with mesg. So I looked at inetutils-talkd (the GNU
implementation), which does the same. I saw that it uses the ttymsg function
and believe that this function does what I want, but I think that this
function isn't available to python.

So the main questions are: Is there something like the ttymsg function in
python OR how can i get write to do what i want?


Answer: 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.

