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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
使用paramiko,pymysql批量管理主机, 用户信息,及主机信息存在数据库中 例子: #!/usr/bin/python # --*-- coding:utf-8 --*-- import paramiko
import pymysql
from threading import Thread, RLock
import sys
def connect_user_tb():
"""返回执行sql结果"""
conn = pymysql.Connect(host = '127.0.0.1' , port = 3306 , user = 'root' , password = 'aixocm' , database = 'oldboy' , charset = 'utf8' )
cursor = conn.cursor()
cursor.execute( 'select user_tb.id,username,passwd,email,deptment_tb.name from user_tb left join deptment_tb on user_tb.deptId=deptment_tb.id' )
db_res = cursor.fetchall()
cursor.close()
conn.close()
return db_res
def auth():
"""用户登录"""
count = 0
while count < 3 :
username = input ( "请输入用户名: " )
passwd = input ( "请输入密码: " )
user_info = connect_user_tb()
for i in user_info:
if username = = i[ 1 ] and passwd = = i[ 2 ]:
return (i)
else :
count + = 1
print ( '您输入的用户名密码不正确,请重新输入' )
continue
else :
return False
def connect_host_user_tb(data):
conn = pymysql.Connect(host = '127.0.0.1' , port = 3306 , user = 'root' , password = 'aixocm' , database = 'oldboy' ,
charset = 'utf8' )
cursor = conn.cursor()
cursor.execute( 'select host_tb.ip,host_tb.port,host_tb.username,host_tb.passwd from host_user_tb inner join host_tb on host_user_tb.hostId=host_tb.id and host_user_tb.userId=%s' ,[data])
host_info = cursor.fetchall()
cursor.close()
conn.close()
return host_info
class Mythread(Thread):
def __init__( self , host, port, username, passwd, lock, cmd):
super ().__init__()
self .host = host
self .port = port
self .username = username
self .passwd = passwd
self .lock = lock
self .cmd = cmd
def run( self ):
with self .lock:
try :
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy) # 忽略密钥认证,使用密码认证
ssh.connect(hostname = self .host, username = self .username, password = self .passwd)
stdin, stdout, stderr = ssh.exec_command( self .cmd)
print ( "#######################主机%s开始执行名########################" % ( self .host))
print (stdout.read().decode( 'utf-8' ))
print (stderr.read().decode( 'utf-8' ))
ssh.close()
print ( "#######################主机执行结束########################" )
except Exception as e:
print (e)
def main():
"""程序入口"""
lock = RLock()
auth_res = auth()
if auth_res:
host_info = connect_host_user_tb(auth_res[ 0 ])
str = "用户名: %s 邮 箱: %s 部 门: %s" % (auth_res[ 1 ],auth_res[ 3 ],auth_res[ 4 ])
print ( str )
print ( 'ip port username passwd' )
for host in host_info:
print (host[ 0 ] + ' ' ,host[ 1 ], ' ' + host[ 2 ] + ' ' + host[ 3 ])
t_list = []
while True :
try :
select_ip = input ( "请输入要执行命令的主机ip,多个主机以逗号分隔,未选择表示执行所有主机:>>" ).strip()
ip_list = select_ip.split( ',' )
cmd = input ( "请输入执行的命令>>: " ).strip()
if not select_ip:
for host in host_info:
t = Mythread(host[ 0 ],host[ 1 ],host[ 2 ],host[ 3 ],lock,cmd)
t.daemon = True
t.start()
t_list.append(t)
for t in t_list:
t.join()
else :
for ip in ip_list:
for host in host_info:
if ip = = host[ 0 ]:
t = Mythread(host[ 0 ], host[ 1 ], host[ 2 ], host[ 3 ], lock, cmd)
t.daemon = True
t.start()
t_list.append(t)
for t in t_list:
t.join()
is_exit = input ( "是否退出,输入yY/nN" )
if is_exit.upper() = = 'Y' :
break
except Exception as e:
print (e)
break
else :
print ( '登录失败,退出程序' )
if __name__ = = '__main__' :
main()
|