Search code examples
pythonsshkeypressparamiko

Python: sending key press events over SSH


I am trying to find out how to simulate key press events on remote server (wich has no X.org aboard) using Python 2.7 Paramiko module. I need to send F2 and Enter consequentially.

According to this discussion I have implemented this code:

import paramiko
client = paramiko.SSHClient()
client.connect(...)
channel = client.get_transport().open_session()
channel.get_pty("vt100")
channel.settimeout(100)

Assuming that F2 is equal to the '\e[12~' Python string (this follows from mentioned answer and xterm control sequences) I am trying to send it to the remote server:

channel.send('\e[12~')

After that the script hangs. What am I doing wrong? Thank you.


Solution

  • Firstly, I recommend the PDF version of the xterm control sequences document; it was originally designed for paper and the automatic conversion to HTML is not perfect.

    The history of terminal emulators is long and complicated, but one point is that the VT100 only had four function keys (PF1 through PF4), and when its successor the VT220 added another 16 function keys, the original coding scheme ran out of room, so they switched to a different one... but they used the old encodings of F1 through F4 for compatibility's sake. Therefore, although F5 is CSI 15 ~, it's not true that F2 is CSI 12 ~. Instead, especially if you're telling Paramiko that you're impersonating a VT100, you should use the VT100 encoding of F2, which is SSE Q or '\eOQ as a Python string.

    I don't know if that's exactly what's causing your problem, but it's probably part of it.