-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindHost.py
More file actions
53 lines (43 loc) · 1.32 KB
/
Copy pathFindHost.py
File metadata and controls
53 lines (43 loc) · 1.32 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#-*- encoding: gb2312 -*-
import os,sys
import paramiko
import threading
rightHostAddr = "Not Available"
def try_sshHost(hostAddr, hostUser, hostPswd, testCmds):
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostAddr, 22, hostUser, hostPswd, timeout=3)
for m in testCmds:
stdin, stdout, stderr = ssh.exec_command(m)
# stdin.write("Y") #简单交互,输入 ‘Y’
out = stdout.readlines()
#屏幕输出
for o in out:
print o,
print '%s\t!!!!!!!!!! Host Fount !!!!!!!!!!\n' % (hostAddr)
global rightHostAddr
rightHostAddr = hostAddr;
ssh.close()
except :
print '%s\tError\n' % (hostAddr)
if __name__=='__main__':
hostIpSeg= "192.168.0"
if len(sys.argv)>1:
hostIpSeg= sys.argv[1]
testCmds = ['echo hello world!'] #执行命令列表
#hostIpSeg= "10.10.15"
hostUser = "linaro" #主机用户
hostPswd = "lijiajun" #主机密码
#hostUser="hiwifi"
#hostPswd="admin"
threads = [] #多线程
print "Begin......"
for i in range(1,255):
hostAddr = hostIpSeg + "." + str(i)
thread = threading.Thread(target=try_sshHost,args=(hostAddr, hostUser, hostPswd, testCmds))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
print "hostAddr is %s." % (rightHostAddr)