脚本整理

编写脚本 checkint.sh,判断用户输入的参数是否为正整数

#! /bin/bash

#Determines whether the parameter is an integer

read -p " Enter your parameters: " NUM

if [[ $NUM =~ [[:digit:]] ]]
    then
    echo "The parameters you enter are integers"
else
    echo "The parameters you enter are not integers"
fi
~     

编写脚本age.sh,判断年龄

#! /bin/bash

read -p "Input your age: " AGE
if [[ ! "$AGE" =~ [[:digit:]]+ ]];then
    echo "please input a digit age"
    exit
elif [[ $AGE -lt 18 ]];then
    echo "好好学习,天天向上"
elif [[ $AGE -le 60 ]];then
    echo "好好工作,努力赚钱"
elif [[ $AGE -le 120 ]];then
    echo "好好享受你的生活"
else
    echo “您是个长寿的人,或者您来自外星球?”
fi

3、编写脚本 filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)

#! /bin/bash

#Determine file type

read -p " Enter your file path: " FILE
if [[ -z "$FILE" ]];then
    echo "Please Enter your file path!!"
elif [[ -d "$FILE" ]];then
    echo "This file is directory file"
elif [[ -f "$FILE" ]];then
    echo "This file is common file"
elif [[ -L "$FILE" ]];then
    echo "This file is chained file"
elif [[ ! -a "$FILE" ]];then
    echo "This is not a file"
else
    echo "This file is Other file types"
fi


Python 运维脚本整理归纳是对在 Linux 运维工作中所能用到的 Python 脚本的归纳和总结。这些脚本大部分出自生产系统并经过比较严谨的测试,少部分脚本是用于学习或者测试目的。这些脚本遵循实用并尽可能的 Pythonic 的原则,可以用于实现自动化运维、一键自动化软件安装、优化、监控报警脚本、软件启动脚本、日志分析脚本等。 以下是 Python 运维脚本整理归纳的一些例子: 1. 自动化运维脚本 ```python import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('hostname', username='username', password='password') stdin, stdout, stderr = ssh.exec_command('ls') print(stdout.readlines()) ssh.close() ``` 2. 一键自动化软件安装脚本 ```python import subprocess subprocess.call(['apt-get', 'install', 'nginx']) ``` 3. 优化脚本 ```python import os os.system('echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse') ``` 4. 监控报警脚本 ```python import smtplib from email.mime.text import MIMEText msg = MIMEText('This is a test email.') msg['Subject'] = 'Test Email' msg['From'] = 'sender@example.com' msg['To'] = 'recipient@example.com' s = smtplib.SMTP('localhost') s.sendmail('sender@example.com', ['recipient@example.com'], msg.as_string()) s.quit() ``` 5. 软件启动脚本 ```python import subprocess subprocess.call(['/etc/init.d/nginx', 'start']) ``` 6. 日志分析脚本 ```python import re log_file = open('/var/log/nginx/access.log', 'r') for line in log_file: match = re.search(r'(\d+\.\d+\.\d+\.\d+) - - \[(.*?)\] "(.*?)" (\d+) (\d+) "(.*?)" "(.*?)"', line) if match: ip_address = match.group(1) date_time = match.group(2) request = match.group(3) status_code = match.group(4) bytes_sent = match.group(5) referer = match.group(6) user_agent = match.group(7) # Do something with the log data log_file.close() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值