Skip to content

Commit 34befb7

Browse files
committed
ssh_thread.py
1 parent a8a3930 commit 34befb7

1 file changed

Lines changed: 143 additions & 0 deletions

File tree

ssh_thread.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#!/usr/bin/python
2+
#coding:utf-8
3+
import paramiko
4+
import sys
5+
import datetime
6+
import threading
7+
import Queue
8+
import getopt
9+
10+
def usage():
11+
12+
print """
13+
14+
-h,-H,--help 帮助页面
15+
-C, --cmd 执行命令模式
16+
-M, --command 执行命令模式
17+
-S, --sendfile 传输文件模式
18+
-L, --localpath 本地文件路径
19+
-R, --remotepath 远程服务器路径
20+
21+
IP列表格式:
22+
23+
IP地址 用户名 密码 端口
24+
192.168.1.1 root 123456 22
25+
26+
e.g.
27+
批量执行命令格式: -C "IP列表" -M '执行的命令'
28+
批量传送文件: -S "IP列表" -L "本地文件路径" -R "远程文件路径"
29+
错误日志文件:$PWD/ssh_errors.log
30+
31+
"""
32+
33+
def ssh(queue_get,cmd):
34+
try:
35+
hostip=queue_get[0]
36+
username=queue_get[1]
37+
password=queue_get[2]
38+
port=queue_get[3]
39+
s=paramiko.SSHClient()
40+
s.load_system_host_keys()
41+
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
42+
s.connect(hostname=hostip,port=port,username=username, password=password)
43+
stdin,stdout,stderr=s.exec_command(cmd)
44+
print "\033[42m---------------------------------%s---------------------------\033[0m \n %s" %(hostip,stdout.read())
45+
s.close()
46+
except Exception,ex:
47+
print "\033[42m---------------------------------%s---------------------------\033[0m\n %s : \t%s" %(hostip,hostip,ex)
48+
#print "\n",hostip,":\t",ex,"\n"
49+
ssh_errors=open("ssh_errors.log","a")
50+
ssh_errors.write("%s\t%s:\t%s\n"%(now,hostip,ex))
51+
ssh_errors.close()
52+
pass
53+
def sftp(queue_get,localpath,remotepath):
54+
try:
55+
hostip=queue_get[0]
56+
username=queue_get[1]
57+
password=queue_get[2]
58+
port=int(queue_get[3])
59+
t=paramiko.Transport((hostip,port))
60+
t.connect(username=username,password=password)
61+
sftp=paramiko.SFTPClient.from_transport(t)
62+
sftp.put(localpath,remotepath)
63+
print "Upload file %s to %s : %s: %s" %(localpath,hostip,remotepath,now)
64+
sftp.close()
65+
t.close()
66+
except Exception,ex:
67+
print "\n",hostip,":\t",ex,"\n"
68+
ssh_errors=open("ssh_errors.log","a")
69+
ssh_errors.write("%s\t%s:\t%s\n"%(now,hostip,ex))
70+
ssh_errors.close()
71+
pass
72+
73+
if __name__ == '__main__':
74+
try:
75+
opts,args= opts, args = getopt.getopt(sys.argv[1:], "(hH)C:M:S:L:R:", ["help","cmd=","command=","sendfile=","localpath=","remotepath="])
76+
now=datetime.datetime.now()
77+
if len(sys.argv) == 1 :
78+
usage()
79+
sys.exit()
80+
if sys.argv[1] in ("-h","-H","--help"):
81+
usage()
82+
sys.exit()
83+
elif sys.argv[1] in ("-C","--cmd"):
84+
for opt,arg in opts:
85+
if opt in ("-C","--cmd"):
86+
iplist=arg
87+
if opt in ("-M","--command="):
88+
cmd=arg
89+
90+
file=open(iplist)
91+
threads = []
92+
myqueue = Queue.Queue(maxsize = 0)
93+
for l in file.readlines():
94+
if len(l)==1 or l.startswith('#'):
95+
continue
96+
f=l.split()
97+
myqueue.put(f)
98+
file.close()
99+
for x in xrange(0,myqueue.qsize()):
100+
if myqueue.empty():
101+
break
102+
mutex = threading.Lock()
103+
mutex.acquire()
104+
mutex.release()
105+
threads.append(threading.Thread(target=ssh, args=(myqueue.get(),cmd)))
106+
for t in threads:
107+
t.start()
108+
t.join()
109+
elif sys.argv[1] in ("-S","--sendfile"):
110+
for opt,arg in opts:
111+
if opt in ("-S","--sendfile"):
112+
iplist=arg
113+
if opt in ("-L","--localpath="):
114+
localpath=arg
115+
if opt in ("-R","--remotepath="):
116+
remotepath=arg
117+
118+
file=open(iplist)
119+
threads = []
120+
myqueue = Queue.Queue(maxsize = 0)
121+
for l in file.readlines():
122+
if len(l)==1 or l.startswith('#'):
123+
continue
124+
f=l.split()
125+
myqueue.put(f)
126+
file.close()
127+
for x in xrange(0,myqueue.qsize()):
128+
if myqueue.empty():
129+
break
130+
mutex = threading.Lock()
131+
mutex.acquire()
132+
mutex.release()
133+
threads.append(threading.Thread(target=sftp, args=(myqueue.get(),localpath,remotepath)))
134+
for t in threads:
135+
t.start()
136+
t.join()
137+
138+
else:
139+
print "\033[31m非法参数,请重新输入!\033[0m"
140+
#usage()
141+
except Exception,ex:
142+
usage()
143+
print ex

0 commit comments

Comments
 (0)