diff options
-rwxr-xr-x | pexpect/examples/bd_client_web.py | 101 | ||||
-rwxr-xr-x | pexpect/examples/bd_serv.py | 159 |
2 files changed, 191 insertions, 69 deletions
diff --git a/pexpect/examples/bd_client_web.py b/pexpect/examples/bd_client_web.py index 5b5e9c6..10a0bd0 100755 --- a/pexpect/examples/bd_client_web.py +++ b/pexpect/examples/bd_client_web.py @@ -11,45 +11,74 @@ import re import socket ROOTPATH="/tmp" - -def page (result = ''): - """Return the main form""" - return """<html> - <head> - <title>Og.</title> -<script language="JavaScript"> -function firstFocus() +CGISH_HTML="""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> +<html> +<head> +<title>Untitled Document</title> +<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> +<style type=text/css> +body { - if (document.forms.length > 0) - { - var TForm = document.forms[0]; - for (i=0;i<TForm.length;i++) - { - if ((TForm.elements[i].type=="text")|| - (TForm.elements[i].type=="textarea")|| - (TForm.elements[i].type.toString().charAt(0)=="s")) - { - document.forms[0].elements[i].focus(); - break; - } - } - } + font-family: "Courier New", Courier, mono; + font-size: 10pt; + color: #00cc00; + background-color: #002000; +} +.headline {font-size: 18pt} +a {color: #99ff99; text-decoration: none} +a:hover {color: #00FF00} +hr {color: #00ff00} +.cursor {color:#002000;background-color:#00cc00} +form { + font-family: "Courier New", Courier, mono; + color: #00CC00; + background-color: #003300; } +input { + font-family: "Courier New", Courier, mono; + color: #00CC00; + background-color: #003300; + padding: 3px; + border: 0; +} +textarea { + font-family: "Courier New", Courier, mono; + color: #00CC00; + background-color: #003300; +} +</style> +<script language="JavaScript"> +function firstFocus() +{if (document.forms.length > 0) +{var TForm = document.forms[0]; +for (i=0;i<TForm.length;i++){ +if ((TForm.elements[i].type=="text")|| +(TForm.elements[i].type=="textarea")|| +(TForm.elements[i].type.toString().charAt(0)=="s")) +{document.forms[0].elements[i].focus();break;}}}} </script> </head> + <body onLoad="firstFocus()"> -<hr noshade="1"> -<pre> -%s -</pre> -<hr noshade="1"> +<pre>%(SHELL_OUTPUT)s</pre> <form action="http://www.chocho.org/cgi-bin/bd_client_web.py" method="POST"> -<input name="command" type="text" size="80"> +<input name="command" type="text" size="80"><br> +<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"> + </form> + </body> </html> - """ % (result) +""" +def page (result = ''): + """Return the main form""" + return CGISH_HTML % {'SHELL_OUTPUT':result} def bd_client (command, host='localhost', port = 1666): HOST = 'localhost' # The remote host @@ -57,22 +86,22 @@ def bd_client (command, host='localhost', port = 1666): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) s.send(command) - data = s.recv(1024) + data = s.recv (1920) s.close() return data #fout = file ('/tmp/log2','w') #fout.write (command) #fout.write ('\n') - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect((host, port)) - s.send(command) - data = s.recv(1024) +# s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +# s.connect((host, port)) +# s.send(command) +# data = s.recv(1024) #fout.write (data) #fout.write ('\n') - s.close() +# s.close() #fout.close() - return data +# return data #def link (matchobject): # """Used in re.sub calls to replace a matched object with an HTML link.""" diff --git a/pexpect/examples/bd_serv.py b/pexpect/examples/bd_serv.py index 260a5d6..d88f487 100755 --- a/pexpect/examples/bd_serv.py +++ b/pexpect/examples/bd_serv.py @@ -1,36 +1,129 @@ #!/usr/bin/env python -import socket, pexpect -import time - -p = pexpect.spawn ('bash') -time.sleep (0.1) -p.expect ('\$') -time.sleep (0.1) - -HOST = '' # Symbolic name meaning the local host -PORT = 1666 # Arbitrary non-privileged port -s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -s.bind((HOST, PORT)) -print 'Listen' -s.listen(1) -print 'Accept' -while 1: - conn, addr = s.accept() - print 'Connected by', addr - data = conn.recv(1024) - print 'RECEIVED:' - print data -# if not data: -# break - time.sleep (0.1) - p.sendline (data) - time.sleep (0.1) - p.expect ('\$') - time.sleep (0.1) - response = p.before + p.after - print 'RESPONSE:' - print response - conn.send(response) +import socket, pexpect, ANSI +import time, sys, os + +# Clearly having the password on the command line is not a good idea, but +# then this entire enterprise is probably not the most security concious thing +# I've ever built. This should be considered an experimental tool. +# USER = sys.argv[1] +# PASSWORD = sys.argv[2] +# PORT = sys.argv[3] + +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 + is being started by inetd. In that case, stdin, stdout and stderr are + all set up for you to refer to the network connection, and the fork()s + and session manipulation should not be done (to avoid confusing inetd). + Only the chdir() and umask() steps remain as useful. + + References: + UNIX Programming FAQ + 1.7 How do I get my program to act like a daemon? + http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16 + + Advanced Programming in the Unix Environment + W. Richard Stevens, 1992, Addison-Wesley, ISBN 0-201-56317-7. + + The stdin, stdout, and stderr arguments are file names that + will be opened and be used to replace the standard file descriptors + in sys.stdin, sys.stdout, and sys.stderr. + These arguments are optional and default to /dev/null. + Note that stderr is opened unbuffered, so + if it shares a file with stdout then interleaved output + may not appear in the order that you expect. + ''' + + # Do first fork. + try: + pid = os.fork() + if pid > 0: + sys.exit(0) # Exit first parent. + except OSError, e: + sys.stderr.write ("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror) ) + sys.exit(1) + + # Decouple from parent environment. + os.chdir("/") + os.umask(0) + os.setsid() + + # Do second fork. + try: + pid = os.fork() + if pid > 0: + sys.exit(0) # Exit second parent. + except OSError, e: + sys.stderr.write ("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror) ) + sys.exit(1) + + # Now I am a daemon! -conn.close() + # Redirect standard file descriptors. + si = open(stdin, 'r') + so = open(stdout, 'a+') + se = open(stderr, 'a+', 0) + os.dup2(si.fileno(), sys.stdin.fileno()) + os.dup2(so.fileno(), sys.stdout.fileno()) + os.dup2(se.fileno(), sys.stderr.fileno()) + + # I now return as the daemon + return 0 + +def main (): + USER = sys.argv[1] + PASSWORD = sys.argv[2] + PORT = int(sys.argv[3]) + + #daemonize () + #daemonize('/dev/null','/tmp/daemon.log','/tmp/daemon.log') + + sys.stdout.write ('Daemon started with pid %d\n' % os.getpid() ) + + vs = ANSI.ANSI (24,80) + p = pexpect.spawn ('ssh %(USER)s@localhost'%locals(), timeout=9) + p.expect ('assword') + p.sendline (PASSWORD) + time.sleep (0.2) + #p.sendline ('stty -echo') + #time.sleep (0.2) + p.sendline ('export PS1="HAON "') + time.sleep (0.2) + p.expect (pexpect.TIMEOUT) + print p.before + vs.process (p.before) + HOST = '' # Symbolic name meaning the local host + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.bind((HOST, PORT)) + print 'Listen' + s.listen(1) + print 'Accept' + while 1: + conn, addr = s.accept() + print 'Connected by', addr + data = conn.recv(1024) + print data + + if data == 'exit': + p.sendline (exit) + break + if not data == "pass": + p.sendline (data) + time.sleep (0.1) + p.expect (['HAON ',pexpect.TIMEOUT]) + #response = p.before + sh_response = p.before.replace ('\r', '') + vs.process_list (sh_response) + response = str (vs) + print response + sent = conn.send(response) + if sent < len (response): + print "Sent is too short" + + +if __name__ == "__main__": +# daemonize('/dev/null','/tmp/daemon.log','/tmp/daemon.log') + main() + + |