脚本

今日写了几个脚本,贴出来

1、批量替换nacos id

#!/bin/bash
# 此脚本用来批量替换yaml文件中的nacos id,默认的路径为当前路径

read -p "Please input the directory(default: pwd):" Dir

if [ -n $Dir ]; then
  Dir=./
fi
echo $Dir

read -p "Please input the nacos id:" Nacos_id
Files=`ls -l ${Dir} | grep -v total | awk '{print $NF}' | grep -E "yml|yaml"`

for file in $Files
do
  sed -i "s/-Dnamespace=[^ ]+/-Dnamespace=$Nacos_id/g" ${Dir}/${file}
done
View Code

 2、检测系统分区是否有异常目录或文件,有则发送邮件

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# author: Cabel

import smtplib,os,socket
from email.mime.text import MIMEText

dirs={'/': ['boot', 'dev', 'home', 'proc', 'run', 'sys', 'etc', 'root', 'var', 'tmp', 'usr', 'bin', 'sbin', 'lib', 'lib64', 'media', 'mnt', 'opt', 'srv'],
      '/root': ['clean_jenkins_slave.sh', 'gaea.env.agent', 'anaconda-ks.cfg',],
      '/home/ali-yunxiao': ['workspace', 'repository-dev', 'repository-online', 'slave.jar', 'package-lock.json', 'wavewisdom-bj-network-file-storage']}

mail_users=['zhangcaibao@wavewisdom-bj.com','zhangying1@wavewisdom.com']
content={}


def send_mail(content):
    smtpHost = 'smtp.sina.com'
    sender = 'dogotsn@sina.com'
    password = "35e**2ce****50e7"
    msg = MIMEText(content)
    msg['Subject'] = '分区发现异常'
    msg['From'] = sender
    smtpServer = smtplib.SMTP_SSL(smtpHost, 465)  # SMTP_SSL
    smtpServer.login(sender, password)
    for receiver in mail_users:
        msg['To'] = receiver
        smtpServer.sendmail(sender, receiver, msg.as_string())
    smtpServer.quit()


def listdir_nohidden(path):
    for f in os.listdir(path):
        if not f.startswith('.'):
            yield f


def check_dir(dir_name):
    real_dir=set(listdir_nohidden(dir_name))
    list_dir=set(dirs.get(dir_name))
    dif_dir=real_dir.difference(list_dir)
    if len(dif_dir):
        content["%s 分区异常" %(dir_name)] = dif_dir


def get_host_ip():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 80))
        ip = s.getsockname()[0]
    finally:
        s.close()
    return ip

for dir in dirs:
    check_dir(dir)
if len(content):
#    host_addr=os.popen("ifconfig | grep -A1 ens | tail -1 | awk '{print $2}'").read()
    host_addr=get_host_ip()
    content["主机IP"]=host_addr
    str_content=str(content)
    send_mail(str_content)
View Code
原文地址:https://www.cnblogs.com/caibao666/p/12022615.html