Python views script sharing for multiple server processes
- 2020-04-02 13:42:46
- OfStack
Recently, I wrote this script as a checklist of relevant services for my own development, which is used to check whether the relevant services on each server are normal on the skip machine
Log in to each machine using expect (SSH trust cannot be used directly because of security issues), and then read the service name and number of processes started based on the configuration of the yaml file to check whether each service is normal PS: the difficulty is that there is no port forwarding and only normal user privileges
Checklist. Py
#coding=utf-8
import sys
# Because I'm going to make this script work for a lot of people, but I can't show them my cryptographic algorithm , So it is pyc
# I'm going to use this script for a lot of other regular users. It's mine ssh Login operation cannot be placed on mine home Directory, so put tmp
sys.path.append('/tmp/local/lib/python2.6/site-packages/PyYAML-3.10-py2.6-linux-x86_64.egg') # Rely on yaml
sys.path.append('/tmp/local/lib/python2.6/site-packages/pexpect-2.4-py2.6.egg') # Rely on pexpect
import yaml
import pexpect
dataDict = yaml.load(open('/tmp/config.yaml')) # Will my yaml configuration load Come in
def myprint(color,mes): # Previously wrote a terminal color print function
''' use ANSI The control code terminal displays color '''
d = dict(r=31, g=32, gb=36, y=33, b=34, p=35, o=37)
color = "x1B[%d;%dm" % (1, d[color])
print "%s%sx1B[0m" % (color, mes)
def main():
list = ['g', 'b', 'y', 'gb', 'p']
light = 0
for k in dataDict:
if k.startswith('bj-'):
color = list[light%5] # Wheel the color according to the server
SERVER = dataDict[k]
# I'm using it -F Because I didn't. root Permission cannot be modified hosts File, but I'm in config.yaml Using aliases,
And this definition is custom sshconfig The default is ~/.ssh/config
child = pexpect.spawn('ssh -F /tmp/sshconfig dongwm@{0}'.format(SERVER['host']))
# Because there are other users, maybe he has not linked to a server, the first will ask you to confirm the server identity, need to point yes
f = child.expect(['Password: ', 'password: ', 'continue connecting (yes/no)?'])
if f == 2:
# When the flag for 2 Indicates that the user has not logged on to a server
child.sendline('yes')
child.expect('password:')
child.sendline('{0}'.format(mypasswd(SERVER['host']))) #mypasswd Is a function that encrypts my server permissions. Each server has a different password
if f == 1:
child.sendline('{0}'.format(mypasswd(SERVER['host'])))
child.expect('~')
for service in SERVER['service']:
flag = 0
# I'll add services to the configuration , The number of processes for the service is typically specified to determine if it is normal
if isinstance(service, dict):
data =service.items()[0]
service = data[0]
num = data[1]
else:
# If I specify only the service and not the number of processes in the configuration, then just make sure the process runs Don't care about the number of processes
num = 0
flag = 1
child.expect('~')
child.sendline('ps -ef|grep {0}|grep -v grep|wc -l'.format(
service))
child.readline()
# Number of processes
pro_num = child.readline().split('rn')[0]
if int(pro_num) == num or flag:
# The number of processes corresponds to the value of the configuration annotation
myprint(color, '[{0}] [{1}] [{2}] [{3}]'.format(k.center(12),
SERVER['ip'].center(14), service.center(20), 'ok'.center(4)))
else:
myprint('r', '[{0}] [{1}] [{2}] [{3}] [{4}!={5}]'.format(k.center(12),
SERVER['ip'].center(14), service.center(20), 'fail',
pro_num, num))
light += 1
child.sendline('exit')
if __name__ == '__main__':
main()
Config.yaml I'm just intercepting one of these segments here
bj-2:
host: s233 # this s233 in sshconfig The specified
ip: XXX.XXX.XXX.233 # Just to show ip Good to confirm
service: # service load After that is a list
# to XX with
- nginx: 5
- uwsgi: 25
- supervisord: 1
# To the native XX provide mysql service
- mysql: 3 #django
# To the native XX provide XX
- celery: 12
# To the native XX provide XX
- rabbitmq: 9
- redis: 1
- mongod: 2