0

Hi I'm running the following telnet script from python as well as from powershell. In python I'm using telnetlib and running the following:

import telnetlib

HOST = "hostname"
PORT = 25
TIMEOUT = 5


def test_telnet(Host, Port, Timeout):
    tn = telnetlib.Telnet(host=Host, port=Port, timeout=Timeout)
    print 'Client Connecton Made.'
    tn.write("HELO <domainName>\r\n")
    tn.write("Mail From: senderemail@domain.com\r\n")
    tn.write("RCPT TO: rcpt@gmail.com\r\n")
    tn.write("DATA\r\n")
    tn.write("Subject: Some Subject.\r\n")
    tn.write("First Line of Body\r\n")
    tn.write("\r\n")
    tn.write("Thanks\r\n")
    tn.write("\r\n")
    tn.write("Name\r\n")
    tn.write(".\r\n")
    tn.write("exit\r\n")
    print tn.read_all()
    print 'Client Connection Lost.'

test_telnet(HOST, PORT, TIMEOUT)

This script is working in Windows 7 but not in Windows Server 2012 R2. Both even gave me the same time out error; however it still is successful when executed from windows 7 machine. The error goes like:

C:\Anaconda\python.exe C:/telnetTest.py
Client Connecton Made.
Traceback (most recent call last):
  File "C:/telnetTest.py", line 34, in <module>
    test_telnet(HOST, PORT, TIMEOUT)
  File "C:/telnetTest.py", line 31, in test_telnet
    print tn.read_all()
  File "C:\Anaconda\lib\telnetlib.py", line 384, in read_all
    self.fill_rawq()
  File "C:\Anaconda\lib\telnetlib.py", line 575, in fill_rawq
    buf = self.sock.recv(50)
socket.timeout: timed out

    Process finished with exit code 1

I even run the line-by-line version of this script and it works on both the machines. The line-by-line commands used in powershell are as follows:

Telnet <hostname> 25
Helo domainName
Mail From: senderemail@domain.com
RCPT TO: rcpt@gmail.com
DATA
Subject: Some Subject. 
First Line of Body

Thanks

Name
.
quit

The powershell version, pycharm version, python version (anaconda 2.7.8) are the same in both machines.

I even tried encoding as mentioned in this post: telnetlib python example But it wasn't the issue. I'm curious to know why it is not running using telnetlib even when it's running in powershell.

Thanks.

Community
  • 1
  • 1
Aks
  • 932
  • 2
  • 17
  • 32
  • If you are sending email, why not use [`Send-MailMessage`](http://technet.microsoft.com/en-us/library/hh849925.aspx)? – vonPryz Nov 18 '14 at 07:13
  • Because it's not working for me at the moment and giving me insecure connection issues. I'm using my company machine and IT also recommended me this method. In addition, this is a part of a bigger python project hence wanted to keep the platform constant. – Aks Nov 18 '14 at 14:45
  • So why not use Python's `email` package? Using telnet to send mail is really inventing the wheel again. – vonPryz Nov 18 '14 at 14:50
  • Sure I can. I can look up the module and take some time in testing my scripts. But I have already tried and tested telnetlib which took me a while to figure out. From my question it's evident that I'm getting satisfactory results and only stuck at one final issue. Hence I would currently prefer to nail my current problem and get on with it. – Aks Nov 18 '14 at 14:59

0 Answers0