July 2002
Intermediate to advanced
608 pages
15h 46m
English
Credit: Jeff Bauer
You need to send commands to one or more logins that can be on the local machine or on a remote machine, and the Telnet protocol is acceptable.
Telnet is one of the oldest protocols in the TCP/IP stack, but it may
still be serviceable (at least within an intranet that is
well-protected against sniffing and spoofing attacks). In any case,
Python’s standard module
telnetlib supports Telnet quite well:
# auto_telnet.py - remote control via telnet import os, sys, string, telnetlib from getpass import getpass class AutoTelnet: def _ _init_ _(self, user_list, cmd_list, **kw): self.host = kw.get('host', 'localhost') self.timeout = kw.get('timeout', 600) self.command_prompt = kw.get('command_prompt', "$ ") self.passwd = {} for user in user_list: self.passwd[user] = getpass("Enter user '%s' password: " % user) self.telnet = telnetlib.Telnet( ) for user in user_list: self.telnet.open(self.host) ok = self.action(user, cmd_list) if not ok: print "Unable to process:", user self.telnet.close( ) def action(self, user, cmd_list): t = self.telnet t.write("\n") login_prompt = "login: " response = t.read_until(login_prompt, 5) if string.count(response, login_prompt): print response else: return 0 t.write("%s\n" % user) password_prompt = "Password:" response = t.read_until(password_prompt, 3) if string.count(response, password_prompt): print response else: return 0 t.write("%s\n" % self.passwd[user]) response = t.read_until(self.command_prompt, ...Read now
Unlock full access