paramiko+threading实现主机批量管理

简介:
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env py
#coding:utf-8
import  os
import  os.path
import  re
import  sys
import  paramiko
import  threading
import  time
from  IPy  import  IP as ipy
 
 
 
def  syntax_check():     #配置文件检查
     return_value  =  os.system( 'python /etc/addresspool.conf' )
     if  return_value ! =  0 :
        exit()
 
 
def  ACL_check(value):       #获取用户定义的ip和网段
     if  value  = =  'ip' :
         value  =  re. compile ( '^allow_ip.*' #获取ip
     elif  value  = =  'network' :
         value  =  re. compile ( '^allow_network.*' )     #获取网段
     else :
         print  '\033[31mparameter error\033[0m'
         exit()    
 
     num  =  0
     conf_file   =  open ( '/etc/addresspool.conf' )  
     for  in  conf_file:           #读取配置文件内容
         ip_or_net  =  re.findall(value,i)
         if  ip_or_net ! =  []:
             num  + =  1
             ACL_add  =  ip_or_net[ 0 ]
             ACL_add  =  ACL_add.split( '=' )[ 1 ]
             ACL_add  =  ACL_add.split( ',' )   #获取ip地址池
     else :
         if  num  = =  0 :              #如果用户没有定义地址
             return  'None'
 
     conf_file.close()
     return  ACL_add     #返回地址池名称
 
     
def  GET_ip(ip_pool):    #处理地址池名称,生成ip地址(IP对象)
     path  =  sys.path[ 2 ]
     copy_mod  =  'cp /etc/addresspool.conf %s/addresspool.py'  % path
     os.system(copy_mod)
 
     import  addresspool as add
     ip_list  =  []
     for  in  ip_pool:
         =  i.strip()
         ACL_ip  =   getattr (add,i)
         for  ip  in  ACL_ip:
             ip_list.append(ip)
     return  ip_list      #返回ip地址池
 
 
def  GET_net(net_pool):      #处理地址池名称(NET对象)
     import  addresspool as network
     net_list  =  []
     for  in  net_pool:
         =  i.strip()
         ACL_net  =   getattr (network,i)
         for  net  in  ACL_net:
             net_list.append(net)
     return  net_list             #返回网段
 
     
def  IPY(net_list):      #将网段解析成ip地址
     net_pool  =  []
     net_add  =  []
     for  in  net_list: 
         ip_pool  =  []
         ip_list  =  ipy(i)
         for  ip  in  ip_list:
             ip  =  str (ip)
             ip_pool.append(ip)
         else :
             ip_pool.pop( 0 )
             ip_pool.pop()
         net_pool.append(ip_pool)
     return  net_pool         #返回ip地址池
 
'-----------------------------------config_file--------------------------------------------'
 
 
def  telnet(ip,port,user,passwd,comd):       #shell命名处理
     try :
         ssh  =  paramiko.SSHClient()
         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
         ssh.connect(ip,port,user,passwd)
         stdin,stdout,stderr  =  ssh.exec_command(comd)
        
         stderr  =  stderr.read()
         if  stderr.strip() ! =  '':
             print  '\033[34m%s %s\033[0m'  % (ip,stderr)
     except :
         print  '\033[31mconnect %s\033[0m'  % ip
 
 
 
def  get_or_put(file2, file4, ip, port, user, passwd):  #文件上传处理
     try :
         get_put  =  paramiko.Transport((ip,port))        #服务器ip和端口(使用SFTP时使用)
         get_put.connect(username = user, password = passwd)        #连接服务器,用户名和密码
         sftp  =  paramiko.SFTPClient.from_transport(get_put)     #使用SFTP协议
         sftp.put(file2,file4)
     except :
         print  '\033[31merror %s\033[0m'  % ip
 
 
'------------------------------------handle------------------------'
def  conf():         #生成配置文件内容
     content  =  '''
#default deny all
#allow appoint ip address or appoint network address
 
ACL_ip_name1 = [
     "192.168.0.2",
     "192.168.0.10",
     "192.168.0.40",
     "192.168.0.60",
]
 
#ACL_network_name1 = [
#    "192.168.1.0/24",
#]
 
allow_ip = ACL_ip_name1
#allow_network = ACL_network_name1
     '''
 
     if  os.path.isfile( '/etc/addresspool.conf' = =  False :
         os.system( 'touch /etc/addresspool.conf' )
         mod_content  =  "echo '%s' > /etc/addresspool.conf"  % content.strip()
         os.system(mod_content)
 
 
 
'----------------------------------confing_file_content------------'
 
if  __name__  = =  '__main__' :
     conf()   #首次执行生成配置文件
     def  Syntax():   #定义命令语法
         print  '''
         syntax error:
             -h display help info
             -t test config file syntax
             -s execute shell "command option argv"
             -p upload file upload_file_path  upload_preservation_path  
         '''
     
     try :
         put  =  ''
         comd  =  ''
         argv  =  sys.argv
         opt  =  argv[ 1 ]
         if  opt  = =  '-s' :
             comd  =  argv[ 2 ]
         elif  opt  = =  '-h' :
             Syntax()
         elif  opt  = =  '-t' :
             test  =  os.system( 'python /etc/addresspool.conf' )
             if  test  = =  0 :
                 print  '"/etc/addresspool.conf"  Syntax ok\n\n'
         elif  opt  = =  '-p' :
             put  =  'put'
             file2  =  argv[ 2 ]
             filename  =  file2.split( '/' ).pop()
 
             file  =  argv[ 3 ]
             file4  =  file  + '/' +  filename 
         else :
             Syntax()
     except :
             Syntax()
     
 
     syntax_check()
     ip  =  ACL_check( 'ip' )
     net  =  ACL_check( 'network' )
     if  ip ! =  'None' :
         ip_list  =  GET_ip(ip)
 
     if  net ! =  'None' :               
         net_list  =  GET_net(net)
         net_pool  =  IPY(net_list)
         for  netadd  in  net_pool:
             for  ipadd  in  netadd:
                 ip_list.append(ipadd)
 
     user  =  'root'
     passwd  =  '123.com'
     port  =  22
 
     if  comd ! =  '':        #多线程执行命令
         for  ip  in  ip_list:
             try :
                 sshd  =  threading.Thread(target = telnet,args = (ip ,port ,user ,passwd ,comd,))
                 sshd.start()
                 time.sleep( 0.1 #延迟打印
             except :
                 pass
  
     if  put ! =  '':         #多线程上传文件
         for  ip  in  ip_list:
             try :
                 exce  =  threading.Thread(target = get_or_put, args = (file2,file4,ip,port,user,passwd,))
                 exce.start()
                 time.sleep( 0.1 #延迟打印
             except :
                 pass
 
      
     path  =  sys.path[ 2 ]
     rm_mod  =  'rm -f %s/addresspool.py'  % path
     os.system(rm_mod)


本文转自  红尘世间  51CTO博客,原文链接:http://blog.51cto.com/hongchen99/1919969
相关文章
|
3月前
|
监控 Python
paramiko 模块 ---Python脚本监控当前系统的CPU、内存、根目录、IP地址等信息
paramiko 模块 ---Python脚本监控当前系统的CPU、内存、根目录、IP地址等信息
|
3月前
|
监控 网络协议 网络安全
ssh服务中如何批量管理100多台机器(Paramiko、 psutil模块)、跳板机(堡垒机)
ssh服务中如何批量管理100多台机器(Paramiko、 psutil模块)、跳板机(堡垒机)
|
5月前
|
运维 Shell 网络安全
第十八章 Python批量管理主机(paramiko、fabric与pexpect)
第十八章 Python批量管理主机(paramiko、fabric与pexpect)
|
Linux 网络安全 数据安全/隐私保护