43tw2e2 发表于 2015-1-20 08:40:13

python脚本 对批量机器执行命令和发送文件

背景:对linux服务器批量执行命令和批量发送文件是运维自动化过程中的最基础的,本脚本就是实现这个功能,shell通过expect也可以实现类似功能。

本脚本用到了pexpect模块,没有该模块的需要手动安装。

该脚本有4个功能:

[*]对被管理的服务器批量执行命令
[*]批量发送本地文件到被管理机器上
[*]支持服务器分组
[*]支持IP地址序列

脚本配置文件如下:

1
2
3
4
5
6

192.168.56.102 root liu123
192.168.56.103 root liu123

192.168.56.101 root liu123
192.168.56.102 root liu123




WEB 和 DB 为组名,其余第一列为IP地址,第二列为用户名,第三列为用户名对应的密码

功能运行截图如下:

[*]IP地址序列批量执行命令
IP地址之间用逗号隔开,IP地址对应的密码需要在配置文件中定义

[*]服务器分组批量执行命令
服务器组名需要在配置文件中定义



[*]批量发送文件
使用组名或者IP地址序列都可以



python代码实现如下:

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
# -*- coding: utf-8 -*-
#作用:对服务器批量执行命令,或者发送文件到其他机器
#用法:
#执行命令:python bat_exec.py DB uptime 或者 python bat_exec.py DB "ls | wc -l"
#发送文件:
#python bat_exec.py DB pexpect-3.3.tar.gz /home/

#配置文件的格式如下 下面例子前面的#需要去掉,配置好密码文件后,需要修改变量ip_list_file的值:
#
#192.168.56.102 root liu123
#
#192.168.56.101 root liu123
#192.168.56.102 root liu123

#
#注意:如果发行版中没有pexpect模块 需要安装pexpect模块
#警告:密码需要明文写进配置文件,可能不安全
#
import pexpect
import re
import sys


def format_ip_str(ip_str):
    """
    检查字符串,符合IP地址序列的,则将该字符串处理为列表。否则,视该字符串为群组名,调用srv_list()查找IP列表文件是否符合该群组名的IP.
    最后返回IP地址列表.
    """

    def check_ip(ip_addr):
      """
      接收ip_addr变量,判断其是否为合法的IP地址,如果为合法则返回真,否则返回False
      """
      ip_pattern = re.compile(r'^(d{1,2}|1dd|2d|25).(d{1,2}|1dd|2d|25).(d{1,2}|1dd|2d|25).(d{1,2}|1dd|2d|25)$')
      ip_match = ip_pattern.match(ip_addr)
      if ip_match:
            result = True
      else:
            result = False
      return result

    def proc_srv_list(groupname):
      """
      接收参数服务器组名,处理ip_list_file配置文件,返回IP地址列表。
      格式类似:['', '172.16.7.17', '172.16.7.183', '172.16.7.184']
      其中第一个元素为组名
      """
      f = open(ip_list_file,'r')
      iplist = []
      flag = 0
      while True:
            line = f.readline()
            if not line:break
            line = line.strip()
            flag_pattern = re.compile(r'^[(.*)]$')
            flag_match = flag_pattern.match(line)
            if flag_match:
                if flag_match.group(1) == groupname:
                  flag = 1
                elif (flag == 1) andflag_match.group(1) != groupname:
                  break
            if flag == 1:
                iplist.append(line)
      f.close()
      if len(iplist) != 0:
            del iplist
      return iplist

    list = ip_str.split(',')
    logInfoList = []
    ip_string = check_ip(list)
    if ip_string:
      for element in list:
            flag = check_ip(element)
            if not flag:
                break
      if flag:
            for element in list:
                f = open(ip_list_file,'r')
                while True:
                  line = f.readline()
                  if not line:break
                  line = line.strip()
                  line_list = line.split(' ')
                  if line_list == element:
                        logInfoList.append(line)
                        break                              #匹配到第一行即中断
                f.close()
    else:
      logInfoList = proc_srv_list(ip_str)
    return logInfoList


def print_Highlighted_Red(str):
    print ' %s ' %str


class Batexec:
    def __init__(self,ip,user,passwd):
      self.IP = ip
      self.USER = user
      self.PASSWD = passwd

    def cmd(self,command):
      child = pexpect.spawn('ssh%s@%s' %(self.USER,self.IP))
      print_Highlighted_Red(self.IP)
      try:
            i = child.expect(['s password: ', 'continue connecting (yes/no)?'])
            if i == 0:
                child.sendline(self.PASSWD)
            elif i == 1:
                child.sendline('yes')
                child.expect('s password: ')
                child.sendline(self.PASSWD)
      except pexpect.EOF:
            child.close()
      child.expect('#')
      child.sendline(command)
      child.expect('#')
      print child.before

    def scpfile(self,localpath,remotepath):
      child = pexpect.spawn("scp %s %s@%s:%s"%(localpath,self.USER,self.IP,remotepath))
      print_Highlighted_Red(self.IP)
      try:
            i = child.expect(['s password: ', 'continue connecting (yes/no)?'])
            if i == 0:
                child.sendline(self.PASSWD)
                child.read()
            elif i == 1:
                child.sendline('yes')
                child.expect('s password: ')
                child.sendline(self.PASSWD)
                child.read()
      except pexpect.EOF:
            child.close()
      print child.before


global ip_list_file
ip_list_file = '/root/batexec/ip_password.conf'

if len(sys.argv) == 3:
    ipstr = sys.argv
    command = sys.argv
    loginfolist = format_ip_str(ipstr)
    for loginfo in loginfolist:
      ip = loginfo.split(' ')
      user = loginfo.split(' ')
      passwd = loginfo.split(' ')
      batexec = Batexec(ip,user,passwd)
      batexec.cmd(command)
elif len(sys.argv) == 4:
    ipstr = sys.argv
    localpath = sys.argv
    remotepath = sys.argv
    loginfolist = format_ip_str(ipstr)
    for loginfo in loginfolist:
      ip = loginfo.split(' ')
      user = loginfo.split(' ')
      passwd = loginfo.split(' ')
      batexec = Batexec(ip,user,passwd)
      batexec.scpfile(localpath,remotepath)
else:
    print '输入有误'





注意:你可能需要修改ip_list_file变量的值来指向合法的配置文件
警告:密码写到明文可能不安全,由此带来的风险,需要你自己把控

页: [1]
查看完整版本: python脚本 对批量机器执行命令和发送文件