tags:

views:

129

answers:

3

i have to send F2 key to telnet host how do i send it using python...using getch() i found that this '<' used for F2 key but while sending its not working..i think there will be some way to send special function keys but i am not able to find it..if somebody knows please help me.thanks in advance

+1  A: 

I can't comment (need +1 rep), but you should start by posting the source code you have at the moment.

Leafy
here you go buddy, comment away :)
digitaldreamer
import getpassimport sysimport telnetlibtn = telnetlib.Telnet(HOST)tn.open(HOST,PORT)tn.write(idpass) #geting connected here#now i just want send f2 key
sagar
@sagar: ...in the original question.
detly
+3  A: 

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.

Ignacio Vazquez-Abrams
thanks a lot lgnacio Vazquez-Abrams for ur quick reply i tried with sequence which u have given its still not working..for enter its working with esquape seq \r but i am still not geting it for f2
sagar
So then find out which terminal the remote side expects, and use *that* terminal's sequence.
Ignacio Vazquez-Abrams
+1  A: 

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.

Tamás
thanks for ur info Tamasits worked at my end with key combination "\x1b2m"
sagar