写点什么

linux 运维常用脚本

作者:@下一站
  • 2022-12-23
    陕西
  • 本文字数:3764 字

    阅读完需:约 12 分钟

linux运维常用脚本

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第 20 天,点击查看活动详情


一、找出占用 CPU 内存过高的进程


#!/bin/bashecho "-------------------CUP占用前10排序--------------------------------"ps -eo user,pid,pcpu,pmem,args --sort=-pcpu  |head -n 10echo "-------------------内存占用前10排序--------------------------------"ps -eo user,pid,pcpu,pmem,args --sort=-pmem  |head -n 10
复制代码


二、查看网卡的实时流量


#!/bin/basheth0=$1echo  -e    "流量进入--流量传出    "while true; do    old_in=$(cat /proc/net/dev |grep $eth0 |awk '{print $2}')    old_out=$(cat /proc/net/dev |grep $eth0 |awk '{print $10}')    sleep 1    new_in=$(cat /proc/net/dev |grep $eth0 |awk '{print $2}')    new_out=$(cat /proc/net/dev |grep $eth0 |awk '{print $10}')    in=$(printf "%.1f%s" "$((($new_in-$old_in)/1024))" "KB/s")    out=$(printf "%.1f%s" "$((($new_out-$old_out)/1024))" "KB/s")    echo "$in $out"done
复制代码


三、一键部署 LNMP 网站平台脚本


#!/bin/bashNGINX_V=1.15.6PHP_V=5.6.36TMP_DIR=/tmp INSTALL_DIR=/usr/local PWD_C=$PWD echoecho -e "\tMenu\n"echo -e "1. Install Nginx"echo -e "2. Install PHP"echo -e "3. Install MySQL"echo -e "4. Deploy LNMP"echo -e "9. Quit" function command_status_check() {    if [ $? -ne 0 ]; then        echo $1        exit    fi } function install_nginx() {    cd $TMP_DIR    yum install -y gcc gcc-c++ make openssl-devel pcre-devel wget    wget http://nginx.org/download/nginx-${NGINX_V}.tar.gz    tar zxf nginx-${NGINX_V}.tar.gz    cd nginx-${NGINX_V}    ./configure --prefix=$INSTALL_DIR/nginx \    --with-http_ssl_module \    --with-http_stub_status_module \    --with-stream    command_status_check "Nginx - 平台环境检查失败!"    make -j 4     command_status_check "Nginx - 编译失败!"    make install    command_status_check "Nginx - 安装失败!"    mkdir -p $INSTALL_DIR/nginx/conf/vhost    alias cp=cp ; cp -rf $PWD_C/nginx.conf $INSTALL_DIR/nginx/conf    rm -rf $INSTALL_DIR/nginx/html/*    echo "ok" > $INSTALL_DIR/nginx/html/status.html    echo '<?php echo "ok"?>' > $INSTALL_DIR/nginx/html/status.php    $INSTALL_DIR/nginx/sbin/nginx    command_status_check "Nginx - 启动失败!"} function install_php() {    cd $TMP_DIR    yum install -y gcc gcc-c++ make gd-devel libxml2-devel \        libcurl-devel libjpeg-devel libpng-devel openssl-devel \        libmcrypt-devel libxslt-devel libtidy-devel    wget http://docs.php.net/distributions/php-${PHP_V}.tar.gz    tar zxf php-${PHP_V}.tar.gz    cd php-${PHP_V}    ./configure --prefix=$INSTALL_DIR/php \    --with-config-file-path=$INSTALL_DIR/php/etc \    --enable-fpm --enable-opcache \    --with-mysql --with-mysqli --with-pdo-mysql \    --with-openssl --with-zlib --with-curl --with-gd \    --with-jpeg-dir --with-png-dir --with-freetype-dir \    --enable-mbstring --enable-hash    command_status_check "PHP - 平台环境检查失败!"    make -j 4     command_status_check "PHP - 编译失败!"    make install    command_status_check "PHP - 安装失败!"    cp php.ini-production $INSTALL_DIR/php/etc/php.ini    cp sapi/fpm/php-fpm.conf $INSTALL_DIR/php/etc/php-fpm.conf    cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm    chmod +x /etc/init.d/php-fpm    /etc/init.d/php-fpm start    command_status_check "PHP - 启动失败!"} read -p "请输入编号:" numbercase $number in    1)        install_nginx;;    2)        install_php;;    3)        install_mysql;;    4)        install_nginx        install_php        ;;    9)        exit;;esac
复制代码


四、服务器系统配置初始化


#/bin/bash# 安装系统性能分析工具及其他yum install gcc make autoconf vim sysstat net-tools iostat iftop iotp wget lrzsz lsof unzip openssh-clients net-tool vim ntpdate -y# 设置时区并同步时间ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeif ! crontab -l |grep ntpdate &>/dev/null ; then    (echo "* 1 * * * ntpdate time.windows.com >/dev/null 2>&1";crontab -l) |crontab fi # 禁用selinuxsed -i '/SELINUX/{s/permissive/disabled/}' /etc/selinux/config # 关闭防火墙if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then    systemctl stop firewalld    systemctl disable firewalldelif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then    service iptables stop    chkconfig iptables offfi # 历史命令显示操作时间if ! grep HISTTIMEFORMAT /etc/bashrc; then    echo 'export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S  `whoami` "' >> /etc/bashrcfi # SSH超时时间if ! grep "TMOUT=600" /etc/profile &>/dev/null; then    echo "export TMOUT=600" >> /etc/profilefi # 禁止root远程登录 切记给系统添加普通用户,给su到root的权限sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config # 禁止定时任务向发送邮件sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab  # 设置最大打开文件数if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; thencat >> /etc/security/limits.conf << EOF    * soft nofile 65535    * hard nofile 65535EOFfi # 系统内核优化cat >> /etc/sysctl.conf << EOFnet.ipv4.tcp_syncookies = 1net.ipv4.tcp_max_tw_buckets = 20480net.ipv4.tcp_max_syn_backlog = 20480net.core.netdev_max_backlog = 262144net.ipv4.tcp_fin_timeout = 20  EOF # 减少SWAP使用echo "0" > /proc/sys/vm/swappiness
复制代码


五、批量创建多个用户并设置密码


#!/bin/bashUSER_LIST=$@USER_FILE=./user.infofor USER in $USER_LIST;do    if ! id $USER &>/dev/null; then        PASS=$(echo $RANDOM |md5sum |cut -c 1-8)        useradd $USER        echo $PASS | passwd --stdin $USER &>/dev/null        echo "$USER   $PASS" >> $USER_FILE        echo "$USER User create successful."    else        echo "$USER User already exists!"    fidone
复制代码


六、一键查看服务器利用率


#!/bin/bashfunction cpu(){        util=$(vmstat | awk '{if(NR==3)print $13+$14}')    iowait=$(vmstat | awk '{if(NR==3)print $16}')    echo "CPU -使用率:${util}% ,等待磁盘IO相应使用率:${iowait}:${iowait}%" }function memory (){     total=`free -m |awk '{if(NR==2)printf "%.1f",$2/1024}'`    used=`free -m |awk '{if(NR==2) printf "%.1f",($2-$NF)/1024}'`    available=`free -m |awk '{if(NR==2) printf "%.1f",$NF/1024}'`    echo "内存 - 总大小: ${total}G , 使用: ${used}G , 剩余: ${available}G"}disk(){        fs=$(df -h |awk '/^\/dev/{print $1}')    for p in $fs; do        mounted=$(df -h |awk '$1=="'$p'"{print $NF}')        size=$(df -h |awk '$1=="'$p'"{print $2}')        used=$(df -h |awk '$1=="'$p'"{print $3}')        used_percent=$(df -h |awk '$1=="'$p'"{print $5}')        echo "硬盘 - 挂载点: $mounted , 总大小: $size , 使用: $used , 使用率: $used_percent"    done }function tcp_status() {    summary=$(ss -antp |awk '{status[$1]++}END{for(i in status) printf i":"status[i]" "}')    echo "TCP连接状态 - $summary"}cpumemorydisktcp_status
复制代码


七、Nginx 访问日志分析


#!/bin/bash# 日志格式: $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"LOG_FILE=$1echo "统计访问最多的10个IP"awk '{a[$1]++}END{print "UV:",length(a);for(v in a)print v,a[v]}' $LOG_FILE |sort -k2 -nr |head -10echo "----------------------" echo "统计时间段访问最多的IP"awk '$4>="[01/Dec/2018:13:20:25" && $4<="[27/Nov/2018:16:20:49"{a[$1]++}END{for(v in a)print v,a[v]}' $LOG_FILE |sort -k2 -nr|head -10echo "----------------------" echo "统计访问最多的10个页面"awk '{a[$7]++}END{print "PV:",length(a);for(v in a){if(a[v]>10)print v,a[v]}}' $LOG_FILE |sort -k2 -nrecho "----------------------" echo "统计访问页面状态码数量"awk '{a[$7" "$9]++}END{for(v in a){if(a[v]>5)print v,a[v]}}' $LOG_FILE |sort -k3 -nr
复制代码


发布于: 刚刚阅读数: 3
用户头像

@下一站

关注

懒人 2020-11-22 加入

都是黄泉预约客,何必难为每一天,执念太强,无法豁然。

评论

发布
暂无评论
linux运维常用脚本_12月日更_@下一站_InfoQ写作社区