Sending ASCII Control codes with asyncssh

325 Views Asked by At

I am writing a small test script that exercises asyncssh facilities. One of the test scenarios is running ping localhost process and then sending CTRL+\ to report intermediary summary. However I have been unsuccessful sending ASCII control codes. Here is some test code:

async def test_control_code():
  async with asyncssh.connect(host='localhost', username='user', password='userpw', known_hosts=None) as conn: 
    async with conn.create_process('ping localhost') as proc: 
      proc._stdin.write(b'\x1c') # Error, says can't send in bytes

if __name__ == '__main__': 
  asyncio.run(test_control_code())

How can I send CTRL + \ to the running process using asyncssh?

1

There are 1 best solutions below

0
On

OK, here's the problem. The ping command does not look for the Ctrl-\ sequence. Indeed, ping doesn't read from stdin at all, so nothing you send will affect it in any way.

When you type Ctrl-\ in a Linux terminal session, the terminal emulator generates a SIGQUIT signal to the foreground process. That's what ping is looking for. Since you don't have a terminal session, there's nobody to make that translation.

So, instead of forcing characters, just send the signal directly:

    proc.send_signal(signal.SIGQUIT)