diff --git a/demo/telnet.py b/demo/telnet.py new file mode 100644 index 0000000000000000000000000000000000000000..e1e9037a199028adfaaf7107e10c2844667200e0 --- /dev/null +++ b/demo/telnet.py @@ -0,0 +1,56 @@ +# Simple telnet demo. Expects a host and user name on the command line, +# reads a password from stdin, logs in on that host, runs "uname -a", +# and prints the result. + +import os +import sys + +import pcl_expect + +host = sys.argv[1] +user = sys.argv[2] + +sys.stdout.write("Password for %s at %s? " % (user, host)) +sys.stdout.flush() + +try: + os.system("stty -echo") + password = sys.stdin.readline() +finally: + os.system("stty echo") +print + +if len(password) > 0 and password[-1] == "\n": + password = password[:-1] + +t = pcl_expect.telnet(host, "23") + +x = pcl_expect.impl() +while x.loop(): + if x.re(t, "ogin:"): + t.send(user + "\n") + +x = pcl_expect.impl() +while x.loop(): + if x.re(t, "assword:"): + t.send(password + "\n") + +x = pcl_expect.impl() +while x.loop(): + if x.re(t, ">|\\$|#"): + t.send("uname -a\n") + elif x.re(t, "incorrect"): + print "Wrong password" + t.close() + sys.exit(1) + +x = pcl_expect.impl() +while x.loop(): + if x.re(t, "(.*)\r?\n.*(>|\\$|#)"): + print "SYSTEM ID:", t.match.group(1) + t.send("exit\n") + +x = pcl_expect.impl() +while x.loop(): + if x.eof(t): + t.close()