Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to send some special keystrokes and am unsure of how to do it.

I need to send Ctrl + Q followed by Ctrl + A to a terminal (I'm using Paramiko).

i have tried

shell = client.invoke_shell()

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

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

print 'i tried'

I can see the two returns go in successfully, but then nothing, it doesnt quit the picocom (also to note i have it the wrong way round, its expecting ctrl+a, then ctrl+q)

if it helps this is the device http://www.cisco.com/c/en/us/td/docs/routers/access/interfaces/eesm/software/configuration/guide/4451_config.html#pgfId-1069760

as you can see at step 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#

UPDATE:

i have tried \x01\x16\x11\n but this returns

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

Switch#

this looks like it could be another special command?

like image 227
AlexW Avatar asked Mar 24 '16 17:03

AlexW


People also ask

How do you press CTRL in Python?

This method is widely used for coping and pasting actions via (ctrl+c, ctrl+v). In order to carry out this action, we need to first press the ctrl key downwards and simultaneously press C key. These two steps can be automated by key_down() method and can only be used along with Shift, Alt and Control keys.

How do I use shortcut keys in Python?

Approach Used:Create a list of which Hotkey is needed to be pressed to perform the desired operation. Here we wanted the Hotkeys to be Shift+A and Shift+a. We create a function execute() that runs our desired program while pressing the Hotkey. Here we wished to print “Detected Hotkey”

How do you send Ctrl Z in Python?

CTRL-Z is used as end-of-message marker when sending SMS in text mode with the AT+CMGS=... command.


1 Answers

This works perfectly for me, returning exactly what I would expect. There are obviously some pieces missing from your code above, so this required a little winging.

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)

And the results are:

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:~$ 

So what did I do that your code does not do?

like image 65
Steve Cohen Avatar answered Oct 29 '22 14:10

Steve Cohen