您好我从python以及powershell运行以下telnet脚本。 在python中,我使用telnetlib并运行以下命令:
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)
此脚本适用于Windows 7,但不适用于Windows Server 2012 R2。两个人甚至给了我相同的时间错误;但是从Windows 7机器执行时它仍然是成功的。错误如下:
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
我甚至运行此脚本的逐行版本,它适用于两台机器。 powershell中使用的逐行命令如下:
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
powershell版本,pycharm版本,python版本(anaconda 2.7.8)在两台机器中都是相同的。
我甚至尝试过这篇文章中提到的编码:telnetlib python example 但这不是问题。 我很想知道为什么即使在PowerShell中运行它也没有使用telnetlib运行。
感谢。