1、常用功能:连接、登陆、执行命令、退出:
连接目标主机并执行命令且退出
import telnetlib
hostname= "10.13.0.9"
user = 'xxxx'
password = 'xxxx'
tn = telnetlib.Telnet(hostname)
tn.read_until(b"login: ")
tn.write(user.encode('ascii') + b"\n")
if password:
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
tn.write(b"ls\n")
tn.write(b"exit\n")
print(tn.read_all().decode('ascii'))
2、端口嗅探的使用:
查看目标主机的短空是否打开或者是否能连接,连接成功与失败是根据连接函数是否报异常。
def telnet(self,hostname,port):
try:
tn = Telnet (hostname,port,timeout=1)
re = hostname+":"+str(port)+" Connect Succeed.
except:
re = hostname+":"+str(port)+" Connect Error\n\n"
return re