我需要发送一些特殊的击键,并且不确定如何做到这一点。
我需要发送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'
我可以看到两个返回成功,但没有,它没有退出picocom(也注意我有错误的方式回合,它期望ctrl + a,然后ctrl + q)
正如你在第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但是这会返回
Switch#
Switch#
*** baud: 9600
*** flow: none
*** parity: none
*** databits: 8
*** dtr: down
Switch#
看起来这可能是另一个特殊命令?
发布于 2019-05-21 09:21:26
这对我来说非常合适,完全符合我的期望。上面的代码中显然缺少一些部分,所以这需要一点翼。
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:~$
那么我的代码没有做什么呢?
发布于 2019-05-21 10:13:38
正如假设:也许伪终端会有所帮助
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')
https://stackoverflow.com/questions/-100009041
复制相似问题