Link to home
Start Free TrialLog in
Avatar of jpetter
jpetter

asked on

Python Issue - 'str' object has no attribute 'read_until'

Hi,

I'm trying to write a little script that will automate the process of checking to see if an SMTP server is acting as an open relay. I can't seem to get beyond the error "'str' object has no attribute 'read_until'" when I issue the tn.write_until statement. I probably have issues with the tn.write as well, but can't get passed this one. I've tried converting the string I'm sending to binary a number of different ways, but I get the same error. It's likely something right in front of me, but I can't see it, and I've dug around finding no solution on the net.

If anyone has any suggestions, I would greatly appreciate them.

Here is what I have now (though it has changed numerous times)
import telnetlib
import sys
import time


def main():
    host = ''
    port = ''
    tn = ''
    if len(sys.argv) < 2:
        prg = sys.argv[0]
        print('Usage: ' + prg + ' <filename> [port]')
        print('If port is omitted then default SMTP port 25 is assumed')
        sys.exit
    elif len(sys.argv) == 2:
        host = sys.argv[1]
        port = '25'
    elif len(sys.argv) == 3:
        host = sys.argv[1]
        port = sys.argv[2]

    try:
        tn = telnetlib.Telnet(host, port)
        time.sleep(3)
    except:
        print('Unable to connect to: ' + host + ' on port: ' + port)
        sys.exit
    tn.read_until('220')
    tn.write('helo mynet.com')
    tn.read_until('250')
    tn.write('mail from:<phantom@mynet.com>')
    tn.read_until('250')
    tn.write('rcpt to:<xxxxx@xxxxxx.com>')
    tn.read_until('250')
    tn.write('data')
    tn.read_until('354')
    tn.write('Subject: This Could be an Open Relay')
    tn.write('If this message is received, the SMTP server is an open relay')
    tn.write('.')
    tn.read_until('250')
    tn.close()

The traceback is:
Usage: ./ckOpenRelay.py <filename> [port]
If port is omitted then default SMTP port 25 is assumed
Unable to connect to:  on port:
Traceback (most recent call last):
  File "./ckOpenRelay.py", line 58, in <module>
    main()
  File "./ckOpenRelay.py", line 41, in main
    tn.read_until(b'220')
AttributeError: 'str' object has no attribute 'read_until'

Thank you,
ASKER CERTIFIED SOLUTION
Avatar of Walter Ritzel
Walter Ritzel
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jpetter
jpetter

ASKER

Walter and pepr, thank you both for you recommendations. That took care of the issues. I apologize for not getting back sooner, but was away from the office for a couple weeks.

Thanks again.