Telnetlib help. I need a case function.
higgeldy Piggeldy
andys at collegenet.com
Tue Nov 19 16:10:05 EST 2002
Hi,
I wrote this script a while ago to renew my library books for me.
Our library here has a telnet interface where you can renew your
books.
So I tried to automate that for myself, now I need to upgrade it
because there are cases where it hangs on the read_until because the
server is sending something unexpected, and I am wondering if anyone
could help me get it to react to different input.
here is the script:
#!/usr/bin/python
import telnetlib
import sys
import string
import re
import regsub
import regex
import smtplib
succeed = re.compile("( *Successful renewal for title '.*'\.)")
maynot = re.compile("( *You may not renew '.*\.)")
noneleft = re.compile(" *No items were renewed .*")
due = re.compile ("( *Due Date = \d\d \w\w\w \d\d\d\d)")
tn = telnetlib.Telnet("dyna.multnomah.lib.or.us")
tn.read_until("login: ")
print "logging in!\n"
tn.write("fastcat\n")
tn.read_until(">>")
tn.write("\n")
#tn.read_until("<<VT100>> ")
tn.write("\n")
tn.read_until("N")
tn.write("\n")
tn.read_until("DYNA -")
tn.write("\n")
tn.read_until("<Enter> :")
def renew(thiscard,thispin,thisemail):
fileout = open ('temp.txt','w')
tn.write("17\n")
tn.read_until("barcode")
tn.write(thiscard+"\n")
print "\n\nrenewing ",
print "for ",thisemail
tn.read_until("PHONE")
tn.write(thispin+"\n")
tn.read_until("below")
tn.write("1\n")
i=1
for i in range(24):
tmp = open('dyna.tmp','w')
i=i+1
tn.read_until("below")
tn.write("r\n")
(tn.read_until('renew'))
tn.write(`i` + "\n")
tmp.write (tn.read_until("Enter"))
tn.write("\n")
tmp.close()
file = open('dyna.tmp','r')
while 1:
line = file.readline()
if not line: break
successobj = succeed.search(line)
maynotobj = maynot.search(line)
dueobj = due.search(line)
if successobj:
fileout.write
(repr(successobj.groups())+"\n")
if maynotobj:
fileout.write
("ATTENTION"+"\n"+repr(maynotobj.groups())+"\nATTENTION\n")
if dueobj:
fileout.write
(repr(dueobj.groups())+"\n")
file.close()
# tn.read_until("below")
tn.write("so\n")
fileout.close()
filein = open ("temp.txt","r")
fileout = open ("temp2.txt","w")
msg = "Subject:Automatic Library Renewal Notice
For:"+thisuser+".\r\n\r\nThank You for using my service.\nPlease feel
free to send any donations or suggestions to me at
sweetfamily at qwest.net\n\n"
while 1:
line = filein.readline()
if not line: break
matchobj = string.replace (line,"(","")
matchobj = string.replace (matchobj,")","")
matchobj = string.replace (matchobj,'"','')
matchobj = string.replace (matchobj,"'","")
matchobj = string.replace (matchobj,",","")
matchobj = string.replace (matchobj,"/","")
print matchobj,
msg = msg+matchobj
fileout.write(matchobj)
filein.close()
fileout.close()
fromaddr = "From:andys at collegenet.com"
toaddr = "To: "
toaddr = toaddr + thisemail
print toaddr
sending = smtplib.SMTP('mail.collegenet.com')
sending.set_debuglevel(0)
sending.sendmail(fromaddr,toaddr,msg)
sending.quit()
cardpatt = regex.compile ('^card:\(\w*\)')
pinpatt = regex.compile ('^pin:\(\w*\)')
emailpatt = regex.compile ("email:\(.*\)")
userpatt = regex.compile ("user:\(.*\)")
config = open("telnetter.cfg")
status =0
while 1:
line = config.readline()
if not line:break
card = cardpatt.match(line)
pin = pinpatt.match(line)
email = emailpatt.match(line)
user = userpatt.match(line)
if card>=0:
thiscard = str(cardpatt.group(1))
status = status +1
if pin>=0:
thispin = str(pinpatt.group(1))
status = status +1
if email>=0:
thisemail = str(emailpatt.group(1))
status = status +1
if user>=0:
thisuser = str(userpatt.group(1))
status = status+1
if status == 4:
status =0
renew(thiscard,thispin,thisemail)
#renew("21168013143409","6422")
#renew("21168018228841","6422")
tn.read_until("Enter>")
tn.write("19\n")
tn.close()
print "DONE!!"
It trips up on the tn.read_until("below") because the server is
sending something else since there are no books to renew.
Thanks
--Andrew
More information about the Python-list
mailing list