python realizes automatic online script

Procedure description:

In this program, the package files in the development program server are uploaded to the formal production environment through this script (Note: the production environment and development environment are not interconnected)

Basic idea of procedure:

  1. Copy the package in the development environment to the local bastion machine
  2. Unpack the package
  3. The extracted files are synchronized to the production server

    Main knowledge: basic use of python library os.system() to call xshell command with python

Procedure usage:

python addline.py development host ip package target host ip upload directory upload No
 For example: Python addline.py 240 / home / shaojinlong / July 17, 2018 / activityiqiyi bb10bd811e40732cf79dffde9a904.Tar.gz 165 / home / zhouja01 190122

Specific procedures:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/7/17 20:56
# @Author  : Zhou Jiaan
# @File    : addline.py
import os
import sys
import re
import time
##usage method:
# python addline.py 240 filename 165 directory runnum
## For example: Python addline.py 240 / home / shaojinlong / July 17, 2018 / activityiqiyi bb10bd811e40732cf79dffde9a904.Tar.gz 165 / home / zhouja01 190122


##Copy file
def cpfile(source_host,source_dir, runnum):
    os.system("mkdir -p /sx/%s" % (runnum))
    os.system("scp %s:%s /sx/%s " % (source_host,source_dir, runnum))

##Decompression file
def tarfile(source_dir,runnum):

    target_test = re.split(r'/', source_dir)
    print(target_test[-1])
    os.system(
        "tar zxvf /sx/%s/%s -C /sx/%s/" % (runnum,target_test[-1], runnum))
    time.sleep(1) #Sleep for one second because synchronization script takes one second to get log

# synchronize files
def syncfile(filename, runnum,target_host):

    os.system("ssh %s 'mkdir -p /home/zhouja01/sx/%s'" % (target_host,runnum))
    os.system("scp -r /sx/%s/%s %s:/home/zhouja01/sx/%s/" %
              (runnum, filename,target_host,runnum))
    # os.system("ssh 165 'sudo -u apps sh /home/zhouja01/bcp_web.sh /home/zhouja01/sx/%s/%s /home/apps/ananetest/%s'" %
    #           (runnum, filename, filename))

def delfile(runnum):
    os.system("mv /sx/%s /sx/wc"%(runnum))

def main():
    source_host=sys.argv[1] #Source host ip
    source_dir=sys.argv[2] #Source host file
    target_host=sys.argv[3] #Target host ip
    target_dir=sys.argv[4] #Target host file
    runnum=sys.argv[5] #oa serial number

    cpfile(source_host,source_dir,runnum) #Copy files from source host to Fortress
    tarfile(source_dir,runnum) #Decompression file

#Get the unzip filename
    with open('/var/log/sx.log', 'r') as f:
        lines = f.readlines()
        last_line = lines[-1]
        print(last_line)
        filename = re.split(r'/', last_line)
        print(filename[3])

    syncfile(filename[3], runnum,target_host) #Synchronize files to target host
    delfile(runnum) #Move fortress files to finish directory

if __name__ == '__main__':
    main()

Tags: Python ssh sudo

Posted on Sun, 09 Feb 2020 07:57:30 -0800 by bombas79