Skip to content
Snippets Groups Projects
Commit cd46fc87 authored by Per Cederqvist's avatar Per Cederqvist
Browse files

Simple telnet demo.

parent 48792a07
No related branches found
No related tags found
No related merge requests found
# 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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment