[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2380
  • Last Modified:

Escape codes for function keys in a telnet session

I am connecting to a box via telnet that is running Operating System: SCO 5.07.

This telnet session is automated by a python script using the python TelnetLib library.  I need to be able to send function key and arrow key presses programatically with python through the telnet session.

The key presses I need include the up down arrows, and basically F1 through F10.  I have been unable to get this to work properly so my question is 2 fold.

1.  How can I determine what I need to send for example an F3 key press?

2.  In the python I do a telnet.write()  what do I put in there to properly send the command across?

Thanks,
0
Mr_Oz
Asked:
Mr_Oz
  • 2
  • 2
2 Solutions
 
LunarNRGCommented:
1. I believe this may depend on other factors in your environment, but if you fire up the python interactive interpreter in a terminal on a *nix system, type a single or double quote, type CTRL+V (literal escape)[1], the key for which you want to determine the value, end with the same type of quote that you started with, and hit ENTER. The result will most likely be the value you need to pass to telnet.write(). For example ...

>>> # typing: ', CTRL+V, F1, ', ENTER
>>> '^[OP'
'\x1bOP'
>>> # typing: ', CTRL+V, F2, ', ENTER
>>> '^[OQ'
'\x1bOQ'

Open in new window


2. Here are the results from _my_ system:

F1 => '\x1bOP'
F2 => '\x1bOQ'
F3 => '\x1bOR'
F4 => '\x1bOS'
F5 => '\x1b[15~'
F6 => '\x1b[17~'
F7 => '\x1b[18~'
F8 => '\x1b[19~'
F9 => '\x1b[20~'
F10 => '\x1b[21~'
F11 => '\x1b[23~'
F12 => '\x1b[24~'
Up Arrow => '\x1b[A'
Down Arrow => '\x1b[B'

[1] From http://en.wikipedia.org/wiki/Control-V

Unix interactive terminals use Control-V to mean "the next character should be treated literally" (the mnemonic here is "v is for verbatim"). This allows a user to insert a literal Control-C or Control-H or similar control characters that would otherwise be handled by the terminal. This behavior was copied by text editors like vi and Unix shells like bash and tcsh, which offer text editing on the command line.

0
 
LunarNRGCommented:
Also, don't forget to use an appropriate line ending, where you would hit the enter key interactively. For telnet I believe the line endings should be '\r\n' (CRLF).
0
 
Mr_OzAuthor Commented:
Unfortunately the box I am telneting into does not have python :(  I will try some of your suggestions though.
0
 
svsCommented:
These key codes should be in the terminfo (terminal definition) database; this database is indexed by terminal name, which is commonly stored in TERM environment variable.

You should be able to get current terminal definition with 'infocmp' command on the SCO box.
0
 
Mr_OzAuthor Commented:
Thanks I think a combination of these 2 answers should get me going.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now