网络编程
位置:首页>> 网络编程>> Python编程>> 对python使用telnet实现弱密码登录的方法详解

对python使用telnet实现弱密码登录的方法详解

作者:leonard_wang  发布时间:2023-12-28 02:52:46 

标签:python,telnet,登录

系统环境:

64位win7企业版

python2.7.10

2016.08.16修改内容:

1)read_until()函数是可以设置timeout的,之前不能获取到password之后的返回是因为调用read_some()函数次数不够,没有读取到返回信息

2)如果不设置read_until()函数的timeout值,那么程序将一直建立连接而不会关闭,导致程序永远没有返回,所以设置timeout还是有必要的

3)不同服务器返回的内容是不一样的,例如网络设备radware返回就不是"login:",而是"user:",有的password的p是大写的,所以匹配"assword:"比较好。并且,不同服务器返回到的用户密码错误提示也是不一样的,可以在python控制台先试试再去写代码。


def check_23_port(ip,username,passwd):
try:
 tn=telnetlib.Telnet()#
 #tn.set_debuglevel(0),设置为2可以看到更多信息
 tn.open(host=ip)
 tn.read_until('login:')#这里设置timeout=10比较好,否则如果不能匹配字符串,这个连接将一直建立,程序不会返回
 tn.write(username+'\r\n')
 tn.read_until('password:')
 tn.write(passwd+'\r\n')
 tn.read_some()#为result准备数据,可能需要多调用几次才能够获取到服务器返回的信息"Login Failed"
 result = tn.read_some()#这里调用两次,不同的操作系统返回的不一样,AIX linux返回的是invalid
 rex = r'Login Failed'
 tmp = re.search(rex,result)
 if tmp == None:
  return True
 else:
  return False
 tn.write("exit\r\n")
 tn.close()
except Exception as e:
 pass
 print "connect error:" + str(e)

来源:https://blog.csdn.net/Leonard_wang/article/details/52160674

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com