diff options
author | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2006-08-22 22:02:52 +0000 |
---|---|---|
committer | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2006-08-22 22:02:52 +0000 |
commit | 86a46b773b2d72d9231d3733a407a2867411c92e (patch) | |
tree | 1c9607570d2f25cad08c345dca39dbc6334f23c0 | |
parent | 17bef94c73a8a047e691f7b8b19f73c24885eeec (diff) | |
download | pexpect-86a46b773b2d72d9231d3733a407a2867411c92e.tar.gz |
Added AJAX for fun. Looks kinda cool already.
git-svn-id: http://pexpect.svn.sourceforge.net/svnroot/pexpect/trunk@413 656d521f-e311-0410-88e0-e7920216d269
-rwxr-xr-x | pexpect/examples/bd_client.cgi | 250 | ||||
-rwxr-xr-x | pexpect/examples/bd_serv.py | 83 |
2 files changed, 187 insertions, 146 deletions
diff --git a/pexpect/examples/bd_client.cgi b/pexpect/examples/bd_client.cgi index 4f6a1eb..c8c8b7c 100755 --- a/pexpect/examples/bd_client.cgi +++ b/pexpect/examples/bd_client.cgi @@ -1,7 +1,4 @@ #!/usr/bin/env python -"""This is a CGI interface to bd_serv.py. -""" - # #sys.path.insert (0,"/var/www/cgi-bin") import sys @@ -36,7 +33,7 @@ form { font-family: "Courier New", Courier, mono; font-size: 8pt; color: #00CC00; - background-color: #003300; + background-color: #002000; } input { font-family: "Courier New", Courier, mono; @@ -51,8 +48,12 @@ textarea { font-size: 8pt; color: #00CC00; background-color: #003300; + border-width: 1; + border-color: #00cc00; + border-style: solid; } </style> + <script language="JavaScript"> function firstFocus() {if (document.forms.length > 0) @@ -67,8 +68,7 @@ if ((TForm.elements[i].type=="text")|| <script language="JavaScript"> // JavaScript Virtual Keyboard // Noah Spurrier <noah@noah.org> -// If you like or use the code then buy me a sandwich. -// 20040303 +// If you like this code then buy me a sandwich. var flag_shift=0; var flag_shiftlock=0; var flag_ctrl=0; @@ -80,9 +80,61 @@ function init () TextDestination = "typed_text"; // Hack to set quote key to show both single and double quote. document.form['quote'].value = "'" + ' "'; - focus_typed_text(); + //focus_typed_text(); } +function loadurl(dest) +{ + xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP"); + xmlhttp.onreadystatechange = update_virtual_screen; + xmlhttp.open("GET", dest); + xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); + xmlhttp.send(null); +} +function update_virtual_screen() +{ + if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) + { + var screen_text = xmlhttp.responseText; + document.form[TextDestination].value = screen_text; + //var json_data = json_parse(xmlhttp.responseText); + } +} +function refresh_screen() +{ + loadurl('bd_client.cgi?ajax=refresh'); +} +function query_cursor() +{ + loadurl('bd_client.cgi?ajax=cursor'); +} + +function type_key (chars) +{ + var ch = '?'; + if (flag_shiftlock || flag_shift) + { + ch = chars.substr(1,1); + } + else if (flag_ctrl) + { + ch = chars.substr(2,1); + } + else + { + ch = chars.substr(0,1); + } + + loadurl('bd_client.cgi?ajax=send&arg=' + escape(ch)); + //insert_at_cursor (document.form[TextDestination], ch); + if (flag_shift || flag_ctrl) + { + flag_shift = 0; + flag_ctrl = 0; + } + update_button_colors(); + //focus_typed_text(); +} function focus_typed_text() { document.form[TextDestination].focus(); @@ -239,130 +291,89 @@ function insert_at_cursor (field, str) } } - -function type_key (chars) -{ - var ch = '?'; - if (flag_shiftlock || flag_shift) - { - ch = chars.substr(1,1); - } - else if (flag_ctrl) - { - ch = chars.substr(2,1); - } - else - { - ch = chars.substr(0,1); - } - - insert_at_cursor (document.form[TextDestination], ch); - if (flag_shift || flag_ctrl) - { - flag_shift = 0; - flag_ctrl = 0; - } - update_button_colors(); - focus_typed_text(); -} </script> </head> -<body onLoad="firstFocus()"> -<pre>%(SHELL_OUTPUT)s</pre> -<form action="/cgi-bin/bd_client_web.cgi" method="POST"> -<input name="command" type="text" size="80"><br> +<!-- <body onLoad="firstFocus()"> --> +<body onload="init()"> +<form id="form" name="form" action="/cgi-bin/bd_client.cgi" method="POST"> +<textarea name="typed_text" cols="80" rows="24">%(SHELL_OUTPUT)s</textarea> <hr noshade="1"> -<input name="submit" type="submit" value="Enter"> -<input name="ctrl_c" type="submit" value="CTRL-C"> -<input name="ctrl_d" type="submit" value="CTRL-D"> -<input name="ctrl_z" type="submit" value="CTRL-Z"> -<input name="esc" type="submit" value="ESC"> -<input name="refresh" type="submit" value="REFRESH"> - -<p align="center"> -<table border="0" align="center"> - <tr> - <td width="86%%" align="center"> - <input type="button" value="ESC" onclick="type_key('\x1b\x1b')" /> +<input name="command" type="text" size="80"><br> +<p align="left"> +<table border="0" align="left"> +<tr> +<td width="86%%" align="center"> + <input name="submit" type="submit" value="Enter"> + <input name="esc" type="submit" value="ESC"> + <input name="refresh" type="button" value="REFRESH" onclick="refresh_screen()"> + <input name="refresh" type="button" value="CURSOR" onclick="query_cursor()"> + <br> + <input type="button" value="Esc" onclick="type_key('\\x1b\\x1b')" /> <input type="button" value="` ~" onclick="type_key('`~')" /> <input type="button" value="1!" onclick="type_key('1!')" /> - <input type="button" value="2@" onclick="type_key('2@\x00')" /> + <input type="button" value="2@" onclick="type_key('2@\\x00')" /> <input type="button" value="3#" onclick="type_key('3#')" /> - <input type="button" value="4$" onclick="type_key('4$')" /> <input type="button" value="5%%" onclick="type_key('5%%')" /> - <input type="button" value="6^" onclick="type_key('6^\x1E')" /> + <input type="button" value="6^" onclick="type_key('6^\\x1E')" /> <input type="button" value="7&" onclick="type_key('7&')" /> <input type="button" value="8*" onclick="type_key('8*')" /> <input type="button" value="9(" onclick="type_key('9(')" /> <input type="button" value="0)" onclick="type_key('0)')" /> - <input type="button" value="-_" onclick="type_key('-_\x1F')" /> + <input type="button" value="-_" onclick="type_key('-_\\x1F')" /> <input type="button" value="=+" onclick="type_key('=+')" /> - - <input type="button" value="BkSp" onclick="key_backspace()" /> + <input type="button" value="BkSp" onclick="type_key('\\x08\\x08\\x08')" /> <br> - <input type="button" value="TAB" onclick="type_key('\t\t')" /> - <input type="button" value="Q" onclick="type_key('qQ\x11')" /> - <input type="button" value="W" onclick="type_key('wW\x17')" /> - <input type="button" value="E" onclick="type_key('eE\x05')" /> - <input type="button" value="R" onclick="type_key('rR\x12')" /> - <input type="button" value="T" onclick="type_key('tT\x14')" /> - <input type="button" value="Y" onclick="type_key('yY\x19')" /> - - <input type="button" value="U" onclick="type_key('uU\x15')" /> - <input type="button" value="I" onclick="type_key('iI\x09')" /> - <input type="button" value="O" onclick="type_key('oO\x0F')" /> - <input type="button" value="P" onclick="type_key('pP\x10')" /> - <input type="button" value="[ {" onclick="type_key('[{\x1b')" /> - <input type="button" value="] }" onclick="type_key(']}\x1d')" /> - <input type="button" value="\ |" onclick="type_key('\\|\x1c')" /> + <input type="button" value="Tab" onclick="type_key('\\t\\t')" /> + <input type="button" value="Q" onclick="type_key('qQ\\x11')" /> + <input type="button" value="W" onclick="type_key('wW\\x17')" /> + <input type="button" value="E" onclick="type_key('eE\\x05')" /> + <input type="button" value="R" onclick="type_key('rR\\x12')" /> + <input type="button" value="T" onclick="type_key('tT\\x14')" /> + <input type="button" value="Y" onclick="type_key('yY\\x19')" /> + <input type="button" value="U" onclick="type_key('uU\\x15')" /> + <input type="button" value="I" onclick="type_key('iI\\x09')" /> + <input type="button" value="O" onclick="type_key('oO\\x0F')" /> + <input type="button" value="P" onclick="type_key('pP\\x10')" /> + <input type="button" value="[ {" onclick="type_key('[{\\x1b')" /> + <input type="button" value="] }" onclick="type_key(']}\\x1d')" /> + <input type="button" value="\\ |" onclick="type_key('\\\\|\\x1c')" /> <br> - <input type="button" id="ShiftLock" value="CAPS" onclick="key_shiftlock()" /> - - <input type="button" value="A" onclick="type_key('aA\x01')" /> - <input type="button" value="S" onclick="type_key('sS\x13')" /> - <input type="button" value="D" onclick="type_key('dD\x04')" /> - <input type="button" value="F" onclick="type_key('fF\x06')" /> - <input type="button" value="G" onclick="type_key('gG\x07')" /> - <input type="button" value="H" onclick="type_key('hH\x08')" /> - <input type="button" value="J" onclick="type_key('jJ\x0A')" /> - <input type="button" value="K" onclick="type_key('kK\x0B')" /> - <input type="button" value="L" onclick="type_key('lL\x0C')" /> - + <input type="button" id="Ctrl" value="Ctrl" onclick="key_ctrl()" /> + <input type="button" value="A" onclick="type_key('aA\\x01')" /> + <input type="button" value="S" onclick="type_key('sS\\x13')" /> + <input type="button" value="D" onclick="type_key('dD\\x04')" /> + <input type="button" value="F" onclick="type_key('fF\\x06')" /> + <input type="button" value="G" onclick="type_key('gG\\x07')" /> + <input type="button" value="H" onclick="type_key('hH\\x08')" /> + <input type="button" value="J" onclick="type_key('jJ\\x0A')" /> + <input type="button" value="K" onclick="type_key('kK\\x0B')" /> + <input type="button" value="L" onclick="type_key('lL\\x0C')" /> <input type="button" value="; :" onclick="type_key(';:')" /> - <input type="button" id="quote" value="'" onclick="type_key('\x27\x22')" /> - <input type="button" value="ENTER" onclick="type_key('\n\n')" /> + <input type="button" id="quote" value="'" onclick="type_key('\\x27\\x22')" /> + <input type="button" value="Enter" onclick="type_key('\\n\\n')" /> <br> - <input type="button" id="Ctrl" value="Ctrl" onclick="key_ctrl()" /> + <input type="button" id="ShiftLock" value="Caps Lock" onclick="key_shiftlock()" /> <input type="button" id="Shift" value="Shift" onclick="key_shift()" /> - <input type="button" value="Z" onclick="type_key('zZ\x1A')" /> - <input type="button" value="X" onclick="type_key('xX\x18')" /> - <input type="button" value="C" onclick="type_key('cC\x03')" /> - - <input type="button" value="V" onclick="type_key('vV\x16')" /> - <input type="button" value="B" onclick="type_key('bB\x02')" /> - <input type="button" value="N" onclick="type_key('nN\x0E')" /> - <input type="button" value="M" onclick="type_key('mM\x0D')" /> + <input type="button" value="Z" onclick="type_key('zZ\\x1A')" /> + <input type="button" value="X" onclick="type_key('xX\\x18')" /> + <input type="button" value="C" onclick="type_key('cC\\x03')" /> + <input type="button" value="V" onclick="type_key('vV\\x16')" /> + <input type="button" value="B" onclick="type_key('bB\\x02')" /> + <input type="button" value="N" onclick="type_key('nN\\x0E')" /> + <input type="button" value="M" onclick="type_key('mM\\x0D')" /> <input type="button" value=", <" onclick="type_key(',<')" /> <input type="button" value=". >" onclick="type_key('.>')" /> <input type="button" value="/ ?" onclick="type_key('/?')" /> <input type="button" id="Shift2" value="Shift" onclick="key_shift()" /> <input type="button" id="Ctrl2" value="Ctrl" onclick="key_ctrl()" /> - <br> - <input type="button" value=" FINAL FRONTIER " onclick="type_key(' ')" /> - </td> - <!-- <td align="center"><input name="button" type="button" onclick="type_key('\\|\x1c')" value="U" /> - <br> - <input name="button2" type="button" onclick="type_key('\\|\x1c')" value="L" /> - <input name="button2" type="button" onclick="type_key('\\|\x1c')" value="H" /> - <input name="button3" type="button" onclick="type_key('\\|\x1c')" value="R" /> - <br> - <input name="button4" type="button" onclick="type_key('\\|\x1c')" value="D" /></td> --> + <input type="button" value=" FINAL FRONTIER " onclick="type_key(' ')" /> +</td> </tr> - </table> +</table> </p> </form> @@ -419,21 +430,34 @@ def escape_shell_meta_chars(s): #sys.path.insert (0,"/usr/local/apache/cgi-bin") sys.stderr = sys.stdout - -print "Content-type: text/html" -print - +print "Content-type: text/html;charset=utf-8\r\n" try: form = cgi.FieldStorage() - if form.has_key("command"): + if form.has_key('ajax'): + ajax_cmd = form['ajax'].value + if ajax_cmd == 'send': + command = ':xsend' + arg = form['arg'].value.encode('hex') + result = bd_client (command + ' ' + arg) + print result + elif ajax_cmd == 'refresh': + command = ':refresh' + result = bd_client (command) + print result + elif ajax_cmd == 'cursor': + command = ':cursor' + result = bd_client (command) + print result + + elif form.has_key('command'): command = form["command"].value result = bd_client (command) print page(result) else: print page() - except: - print "\n\n<pre>" - traceback.print_exc() - print "</pre>" + tb_dump = traceback.format_exc() + print "Content-type: text/html" + print + print page(str(tb_dump)) diff --git a/pexpect/examples/bd_serv.py b/pexpect/examples/bd_serv.py index 10b6578..30746bb 100755 --- a/pexpect/examples/bd_serv.py +++ b/pexpect/examples/bd_serv.py @@ -1,7 +1,7 @@ #!/usr/bin/env python """Back door shell server -This exposes a shell terminal emulator on a socket. +This exposes an shell terminal on a socket. --hostname : sets the remote host name to open an ssh connection to. --username : sets the user name to login with @@ -10,7 +10,7 @@ This exposes a shell terminal emulator on a socket. """ # Having the password on the command line is not a good idea, but # then this entire project is probably not the most security concious thing -# I've ever built. This should be considered an experimental tool. +# I've ever built. This should be considered an experimental tool -- at best. import pxssh, pexpect, ANSI import socket import time, sys, os, getopt, getpass @@ -39,6 +39,20 @@ class roller (threading.Thread): # self.finished.wait(self.interval) self.function(*self.args, **self.kwargs) +def endless_poll (child, prompt, screen, refresh_timeout=0.1): + """This keeps the screen updated with the output of the child. + This runs in a separate thread. + See roller(). + """ + child.logfile_read = screen + while True: + #child.prompt (timeout=refresh_timeout) + try: + #child.read_nonblocking(1,timeout=refresh_timeout) + child.read_nonblocking(4000, 0.1) + except: + pass + def daemonize (stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): '''This forks the current process into a daemon. Almost none of this is necessary (or advisable) if your daemon @@ -104,15 +118,6 @@ def add_cursor_blink (response, row, col): i = (row-1) * 80 + col return response[:i]+'<img src="http://www.noah.org/cursor.gif">'+response[i:] -def endless_poll (child, prompt, screen, refresh_timeout=1): - """This keeps the screen updated with the output of the child. - This runs in a separate thread. - """ - child.logfile_read = screen - while True: - child.prompt (timeout=refresh_timeout) - time.sleep(1) - def main (): try: optlist, args = getopt.getopt(sys.argv[1:], 'h?d', ['help','h','?', 'hostname=', 'username=', 'password=', 'port=']) @@ -145,10 +150,10 @@ def main (): else: password = getpass.getpass('password: ') - #if daemon_mode: - # print "daemonizing server" - # daemonize () - #daemonize('/dev/null','/tmp/daemon.log','/tmp/daemon.log') + if daemon_mode: + print "daemonizing server" + daemonize() + #daemonize('/dev/null','/tmp/daemon.log','/tmp/daemon.log') sys.stdout.write ('server started with pid %d\n' % os.getpid() ) @@ -183,7 +188,7 @@ def main (): # conn.close() # continue if data[0]!=':': - cmd = ':command' + cmd = ':sendline' arg = data.strip() else: request = data.split(' ', 1) @@ -193,15 +198,24 @@ def main (): else: cmd = request[0].strip() - if cmd == ':command': + if cmd == ':exit': + r.cancel() + break + elif cmd == ':sendline': child.sendline (arg) child.prompt(timeout=2) - shell_window = pretty_box(virtual_screen.rows,virtual_screen.cols,str(virtual_screen)) - elif cmd == ':send': + shell_window = str(virtual_screen) #pretty_box(virtual_screen.rows,virtual_screen.cols,str(virtual_screen)) + elif cmd == ':send' or cmd==':xsend': + if cmd==':xsend': + arg = arg.decode("hex") child.send (arg) - shell_window = pretty_box(virtual_screen.rows,virtual_screen.cols,str(virtual_screen)) + time.sleep(0.2) + shell_window = str(virtual_screen) #pretty_box(virtual_screen.rows,virtual_screen.cols,str(virtual_screen)) + elif cmd == ':cursor': + shell_window = '%x%x' % (virtual_screen.cur_r, virtual_screen.cur_c) elif cmd == ':refresh': - shell_window = pretty_box(virtual_screen.rows,virtual_screen.cols,str(virtual_screen)) + shell_window = str(virtual_screen) #pretty_box(virtual_screen.rows,virtual_screen.cols,str(virtual_screen)) + #elif cmd == ':skip': # # Wait until the TIMEOUT then throw away all data from the child. # # Use to catch up the screen with the shell if state gets out of sync. @@ -209,8 +223,6 @@ def main (): # sh_response = child.before.replace ('\r', '') # virtual_screen.write (sh_response) response = [] - #response.append ('%03d' % virtual_screen.rows) - #response.append ('%03d' % virtual_screen.cols) response.append (shell_window) #response = add_cursor_blink (response, row, col) sent = conn.send('\n'.join(response)) @@ -218,12 +230,10 @@ def main (): if sent < len (response): print "Sent is too short. Some data was cut off." conn.close() - if arg == 'exit': - s.close() - break finally: print "cleaning up socket" s.close() + print "done!" def pretty_box (rows, cols, s): """This puts an ASCII text box around the given string, s. @@ -233,21 +243,19 @@ def pretty_box (rows, cols, s): def error_response (msg): response = [] - response.append ('000') - response.append ('000') response.append ("""All commands start with : :{REQUEST} {ARGUMENT} {REQUEST} may be one of the following: - :command : Run the ARGUMENT as a shell command. + :sendline: Run the ARGUMENT followed by a line feed. :send : send the characters in the ARGUMENT without a line feed. :refresh : Use to catch up the screen with the shell if state gets out of sync. Example: - :command ls -l + :sendline ls -l You may also leave off :command and it will be assumed. Example: ls -l is equivalent to: - :command ls -l + :sendline ls -l """) response.append (msg) return '\n'.join(response) @@ -268,6 +276,15 @@ def parse_host_connect_string (hcs): return d if __name__ == "__main__": -# daemonize('/dev/null','/tmp/daemon.log','/tmp/daemon.log') - main() + try: + start_time = time.time() + print time.asctime() + main() + print time.asctime() + print "TOTAL TIME IN MINUTES:", + print (time.time() - start_time) / 60.0 + except Exception, e: + print str(e) + tb_dump = traceback.format_exc() + print str(tb_dump) |