how to send F2 key to remote host using python - python

I have to send F2 key to telnet host. How do I send it using python...using getch() I found that the character < used for the F2 key but when sending >, its not working. I think there is a way to send special function keys but I am not able to find it. If somebody knows please help me. Thanks in advance

Extended keys (non-alphanumeric or symbol) are composed of a sequence of single characters, with the sequence depending on the terminal you have told the telnet server you are using. You will need to send all characters in the sequence in order to make it work. Here, using od -c <<< 'CtrlVF2' I was able to see a sequence of \x1b0Q with the xterm terminal.

First, as Ignacio pointed out, you have to determine the exact sequence of characters sent by F2. On my machine, this happens to be ^[OQ, where ^[ denotes an escape character with ASCII code 27. This is identical to what he got. You have to send the exact same byte sequence over telnet. Assuming that the correct sequence is the one I have shown above, it boils down to this:
import telnetlib
tn = telnetlib.Telnet(HOST, PORT)
tn.write(idpass)
tn.write("\x1b0Q")
tn.close()
In case you are wondering, \x1b stands for a character having ASCII code 27, since 27 in hexadecimal is 1b.
This works on my machine (tested by a simple echo server on the receiving end), so if it does not work for you, it means that the remote end expects something else in place of an F2 keypress.

Related

Sending F2 key over pyserial write

I am trying to use pyserial to write or send F2 keystroke over serial port.
I tried:
serial.write(chr(115).encode())
as 115 is ASCII code for F2. I got this from javascript getch function from one of the answers:
ascii codes for windows keyboard keys and the codes for function keys(F1 - F12) and other keys like shift,capslock,backspace,ctrl etc
For ESC key, I got it working with chr(27).
These are special sequences that need to be interpreted by receiver.
This describes some of them, and you can see F2 is ^[OQ or ^[[12~. (btw, ASCII 115 that you mention is s)
In my system, ^[OQ is up arrow key for some reasons.
I finally found from PUTTY manual that F2 is ESC [12~.
Therefore, it is "\x1B[12"
Pass that string into
serial.write("\x1B[12")
and it works.
Hope that it is useful for other people. I am actually trying to reboot into BIOS without physically hitting F2 key.

How do I press enter with pexpect [duplicate]

I am working with pythons pexpect module to automate tasks, I need help in figuring out key characters to use with sendcontrol. how could one send the controlkey ENTER ? and for future reference how can we find the key characters?
here is the code i am working on.
#!/usr/bin/env python
import pexpect
id = pexpect.spawn ('ftp 192.168.3.140')
id.expect_exact('Name')
id.sendline ('anonymous')
id.expect_exact ('Password')
*# Not sure how to send the enter control key
id.sendcontrol ('???')*
id.expect_exact ('ftp')
id.sendline ('dir')
id.expect_exact ('ftp')
lines = id.before.split ('\n')
for line in lines :
print line
pexpect has no sendcontrol() method. In your example you appear to be trying to send an empty line. To do that, use:
id.sendline('')
If you need to send real control characters then you can send() a string that contains the appropriate character value. For instance, to send a control-C you would:
id.send('\003')
or:
id.send(chr(3))
Responses to comment #2:
Sorry, I typo'ed the module name -- now fixed. More importantly, I was looking at old documentation on noah.org instead of the latest documentation at SourceForge. The newer documentation does show a sendcontrol() method. It takes an argument that is either a letter (for instance, sendcontrol('c') sends a control-C) or one of a variety of punctuation characters representing the control characters that don't correspond to letters. But really sendcontrol() is just a convenient wrapper around the send() method, which is what sendcontrol() calls after after it has calculated the actual value that you want to send. You can read the source for yourself at line 973 of this file.
I don't understand why id.sendline('') does not work, especially given that it apparently works for sending the user name to the spawned ftp program. If you want to try using sendcontrol() instead then that would be either:
id.sendcontrol('j')
to send a Linefeed character (which is control-j, or decimal 10) or:
id.sendcontrol('m')
to send a Carriage Return (which is control-m, or decimal 13).
If those don't work then please explain exactly what does happen, and how that differs from what you wanted or expected to happen.
If you're just looking to "press enter", you can send a newline:
id.send("\n")
As for other characters that you might want to use sendcontrol() with, I found this useful: https://condor.depaul.edu/sjost/lsp121/documents/ascii-npr.htm
For instance, I was interested in Ctrl+v. Looking it up in the table shows this line:
control character
python & java
decimal
description
^v
\x16
22
synchronous idle
So if I want to send that character, I can do any of these:
id.send('\x16')
id.send(chr(22))
id.sendcontrol('v')
sendcontrol() just looks up the correct character to send and then sends it like any other text
For keys not listed in that table, you can run this script: https://github.com/pexpect/pexpect/blob/master/tests/getch.py (ctrl space to exit)
For instance, ran that script and pressed F4 and it said:
27<STOP>
79<STOP>
83<STOP>
So then to press F4 via pexpect:
id.send(chr(27) + chr(79) + chr(83))

Prevent terminal character set switch on data printing

I am running a console application that takes data coming from various sensors around the house. Sometimes the transmission is interrupted and thus the packet does not make sense. When that happens, the contents of the packet is output to a terminal session.
However, what has happened is that while outputting the erroneous packet, it has contained characters that changed character set of the current terminal window, rendering any text (apart from numbers) as unreadable gibberish.
What would be the best way to filter the erroneous packets before their display while retaining most of the special characters? What exactly are sequences that can change behaviour of the terminal?
I would also like to add that apart from scrambled output, the application still works as it should.
Your terminal may be responding to ANSI escape codes.
To prevent the data from inadvertently affecting the terminal, you could print the repr of the data, rather than the data itself:
For example,
good = 'hello'
bad = '\033[41mRED'
print('{!r}'.format(good))
print('{!r}'.format(bad))
yields
'hello'
'\x1b[41mRED'
whereas
print(good)
print(bad)
yields
(Btw, typing reset will reset the terminal.)
You can convert all characters outside of the ASCII range which should eliminate any stray escape sequences that will change the state of your terminal.
print s.encode('string-escape')
When you get a packet check it for validity before outputting it. One possibility is to check that each character in the packet is printable, that is, in the range of 32-127 decimal, before output. Or add checksums to the packets and reject any with bad checksums.

How to send an arrow key use paramiko library in python?

I'm using python 2.7 and code ssh client with paramiko library, I use myhost.channel.send(chr(keycode)) to send every keycode to server. But It only works with 1 byte keycodes. I want to send other multi-byte keycodes like arrow keys. How can I achieve this? Please help me.
A GUI like Windows or MacOS identifies keys with 'keycodes', but an SSH pipe just transfers bytes, not keycodes.
Assuming the program running inside ssh on your server is interactive (that is, it's expecting a human to be using it), you'll need to find out what kind of byte-patterns it's expecting to receive. When you open your channel, make sure you're calling .get_pty() and giving it a terminal parameter (the default, vt100, is pretty safe). Then, you'll need to read the documentation for the VT100 terminal to find out what byte sequences it sends when various keys are pressed. I recommend reading the Xterm Control Sequences documentation (Xterm is not strictly a vt100 emulator, but its documentation is very complete), and not confusingly mixed up with the hardware details of the original VT100 terminal). Note that in that document, "CSI" effectively means the Python string '\e['.
For example, the Xterm Control Sequences document says that the arrow keys are "CSI A" for up, "CSI B" for down, "CSI C" for right, and "CSI D" for left. In Python, that looks like:
up = '\e[A'
down = '\e[B'
right = '\e[C'
left = '\e[D'
In macOS 10.13.2 you can use:
class Keyboard:
up = '\x1b[A'
down = '\x1b[B'
right = '\x1b[C'
left = '\x1b[D'
(I read them from sys.stdin)
I think in python you can do the following:
channel.sendall(chr(0x1b)+"[B")
0x1B is the ASCII Escape character for VT100 terminal.

escape sequence for all the keyboard keys in python

i am using telnetlib in python...
i have used '\r' for enter key,'\t' for TAB.
as same as this scenario i want the char sequence for SHIFT,PAGE UP,PAGE DOWN,F1,F2...F12.
pleas help me regarding this issue as i have to use all this keyboard keys in my code.
import telnetlib
HOST = "localhost"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.write("ls\n")
tn.write("exit\n")
tn.write("\r") #this is for enter
tn.write("\t") # this is for tab
#what should be here to other keys..pls
print tn.read_all()
Unfortunately not all key-presses result in one or more bytes being sent over the wire. This is really about terminal emulation, since control keys are "meant" to be interpreted by the terminal device (or emulator).
Different terminal types define different keys and map them to different byte values in the stream. For example, some terminals have F11 and F12, and some don't. Some define Ctrl+F keys, Shift+F keys, Alt+F keys, Command+F keys; others don't. And different terminals map these keys to completely different byte sequences over the wire. The issue is the same when it comes to arrow keys and cursor-mode keys like insert.
You may find that certain keys are trapped by the client and not transmitted at all, or that non-keyboard events (such as resizing the terminal window with the mouse) transmit terminal escape sequences.
You can always go with thistelnet client : https://www.redhat.com/archives/redhat-list/2000-August/msg00070.html

Categories

Resources