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: [email protected]\r\n")
tn.write("RCPT TO: [email protected]\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: [email protected]
RCPT TO: [email protected]
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.