Python 发送 control + Q 然后 control + A(特殊键)

Python send control + Q then control + A (special keys)

我需要发送一些特殊的击键,但不确定如何发送。

我需要发送 Ctrl + Q 然后是 Ctrl + A 到终端(我正在使用 Paramiko)。

我试过了

shell = client.invoke_shell()

shell.send(chr(10))
time.sleep(5)
shell.send(chr(13))

shell.send('\x11')
shell.send('\x01')

print 'i tried'

我可以看到两个returns成功进入,但是什么也没有,它没有退出picocom (还要注意,我把它弄错了,它期待 ctrl+a,然后是 ctrl+q)

如果有帮助,这就是设备 http://www.cisco.com/c/en/us/td/docs/routers/access/interfaces/eesm/software/configuration/guide/4451_config.html#pgfId-1069760

如您在第 2 步中所见

Step 2 Exit the session from the switch, press Ctrl-a and Ctrl-q from your keyboard:

Switch# <type ^a^q>
Thanks for using picocom
Router#

更新:

我试过\x01\x16\x11\n但是这个returns

Switch#
Switch#
*** baud: 9600
*** flow: none
*** parity: none
*** databits: 8
*** dtr: down

Switch#

这看起来可能是另一个特殊命令?

Ctrl+键实际上是一种“用户友好”的输入ASCII控制字符的方式。这是通过从输入密钥的 ASCII 代码中减去 64(在适用的情况下采用大写字母)来完成的。例如组合Ctrl + H相当于输入退格(H有代码72,72-64=8,退格字符). This Wikipedia page 列出与其组合键关联的 ASCII 控制字符,因此 Ctrl+A, Ctrl+Q相当于通过paramiko通道发送字符串"\x01\x11"

channel = client.invoke_shell()
channel.send('\x01\x11')

更新

检查当我按下Ctrl+A时实际传输了什么Ctrl+Q 我设计了一个小测试程序:

# decode.py
import sys

while True:
    inp = sys.stdin.read(1)
    if len(inp) == 0:
        break
    print ord(inp[0])

如果我现在通过 ssh localhost python decode.py 调用它并输入 Ctrl+A Ctrl+V Ctrl+Q(我必须执行 Ctrl+V因为Ctrl+Q被我本地[=98解释为XON =]而不传递给对方),然后EnterCtrl+D关闭连接,我得到 11710 作为序数,或 '\x01\x11\n',如预期的那样。

我基本上通过执行printf '\x01\x11\n' | ssh localhost python decode.py得到相同的结果。但是,如果我通过 printf '\x01\x11\n' | ssh -tt localhost python decode.py 在远程端分配一个 pty,则 \x11 会被远程 pty 截获并且不会传递给 运行 脚本(我得到 1,10 作为输出)。在这种情况下,它有助于在 Ctrl 之前发送 Ctrl+V (\x16) ]+Q 指示 pty 传递下一个字符 Verbatim。 正如预期的那样 printf '\x01\x16\x11\n' | ssh -tt localhost python decode.py 输出 11710.

正如假设:也许伪终端会有所帮助

import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(...)
channel = сlient.get_transport().open_session()
channel.get_pty()
channel.settimeout(5)
channel.exec_command('\x11\x01') 

这对我来说非常有用,返回的正是我所期望的。上面的代码中显然缺少一些部分,因此这需要一些技巧。

import sys
import time
import getpass
import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('127.0.0.1',
            username='apsuser',
            password=getpass.getpass('Password: '))
shell = ssh.invoke_shell()
shell.settimeout(0.25)

shell.send('picocom /dev/ttyS0\n')
time.sleep(2)
sys.stdout.buffer.write(shell.recv(10000))
sys.stdout.buffer.flush()

shell.send('\x01')
shell.send('\x11')

time.sleep(2)
sys.stdout.buffer.write(shell.recv(10000))
sys.stdout.buffer.flush()
print()
time.sleep(2)

结果是:

Password: 

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Apr 14 19:55:57 2016 from 127.0.0.1
picocom /dev/ttyS0
apsuser@Steve-Laptop:~$ picocom /dev/ttyS0
picocom v1.7

port is        : /dev/ttyS0
flowcontrol    : none
baudrate is    : 9600
parity is      : none
databits are   : 8
escape is      : C-a
local echo is  : no
noinit is      : no
noreset is     : no
nolock is      : no
send_cmd is    : sz -vv
receive_cmd is : rz -vv
imap is        : 
omap is        : 
emap is        : crcrlf,delbs,

Terminal ready

Thanks for using picocom
apsuser@Steve-Laptop:~$ 

那么我做了哪些您的代码没有做的事情?