python初试牛刀

时间:2022-09-02 13:43:55

需求:在L7的一台机器上做nginx配置,然后代码分发到别的所有的机器上。由于目录中有很多配置文件,而且防止误操作,需要修改配置之前先备份原配置。然后需要在运行修改配置的脚本之前,先弹出界面,告知操作哪一个脚本

python脚本

#!/user/bin/env python

import glob

import os

file = glob.glob("/home/work/nginx/site-enable/*.conf")

num = len(file)

for i in range(1, num+1):

   print "%d. %s" %(i, file[i-1])

choose = int(raw_input('Enter your choose:'))

if(choose>0) and (choose<num+1):

  file1 = file[choose-1].split('/')

  num1 = len(file1)

  filename = file1[num1-1]

  os.system("sh /root/cpconf.sh %s" %filename)

else:

  print "Input error!"

  exit()

shell脚本:

#!/bin/bash
Date=`date +%Y%m%d%H%M`
cp /home/work/nginx/site-enable/$1 /home/work/opdir/nginx_bak/$1.$Date
cp /home/work/nginx/conf/$1 /home/work/opdir/nginx_bak/$1.$Date
cp /home/work/nginx/site-enable/$1 /root/
vim $1
echo -ne "\n\n###########   diff conf    ###############\n\n"
diff $1 /home/work/opdir/nginx_bak/$1.$Date