Linux实用命令及脚本

1、列出排名前10的内存占用进程

ps aux | sort -rk 4,4 | head -n 10  

2、iptables 自动屏蔽访问网站频繁的IP

使用场景:针对恶意访问网站情况

根据访问日志(以 nginx 的 logs 中记录访问的 access.log 日志文件为例,检测短期访问大于100的IP,并使用iptables命令进行屏蔽,同时将禁用的IP放到/tmp/deny_ip.log文件中)。

#!/bin/bash  
DATE=$(date +%d/%b/%Y:%H:%M)  
LOG_FILE=/usr/local/nginx/logs/demo2.access.log  
ABNORMAL_IP=$(tail -n5000 $LOG_FILE |grep $DATE |awk '{a[$1]++}END{for(i in a)if(a[i]>100)print i}')  
for IP in $ABNORMAL_IP; do  
    if [ $(iptables -vnL |grep -c "$IP") -eq 0 ]; then  
        iptables -I INPUT -s $IP -j DROP  
        echo "$(date +'%F_%T') $IP" >> /tmp/deny_ip.log  
    fi  
don  

3、自动发布 Java 项目(Tomcat)

#!/bin/bash  
DATE=$(date +%F_%T)  
  
TOMCAT_NAME=$1  
TOMCAT_DIR=/usr/local/$TOMCAT_NAME  
ROOT=$TOMCAT_DIR/webapps/ROOT  
  
BACKUP_DIR=/data/backup  
WORK_DIR=/tmp  
PROJECT_NAME=tomcat-java-demo  
  
# 拉取代码  
cd $WORK_DIR  
if [ ! -d $PROJECT_NAME ]; then  
   git clone https://github.com/xxxx/tomcat-java-demo  
   cd $PROJECT_NAME  
else  
   cd $PROJECT_NAME  
   git pull  
fi  
  
# 构建  
mvn clean package -Dmaven.test.skip=true  
if [ $? -ne 0 ]; then  
   echo "maven build failure!"  
   exit 1  
fi  
  
# 部署  
TOMCAT_PID=$(ps -ef |grep "$TOMCAT_NAME" |egrep -v "grep|$$" |awk 'NR==1{print $2}')  
[ -n "$TOMCAT_PID" ] && kill -9 $TOMCAT_PID  
[ -d $ROOT ] && mv $ROOT $BACKUP_DIR/${TOMCAT_NAME}_ROOT$DATE  
unzip $WORK_DIR/$PROJECT_NAME/target/*.war -d $ROOT  
$TOMCAT_DIR/bin/startup.sh  

4、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=$1  
echo "统计访问最多的10个IP"  
awk '{a[$1]++}END{print "UV:",length(a);for(v in a)print v,a[v]}' $LOG_FILE |sort -k2 -nr |head -10  
echo "----------------------"  
  
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 -10  
echo "----------------------"  
  
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 -nr  
echo "----------------------"  
  
echo "统计访问页面状态码数量"  
awk '{a[$7" "$9]++}END{for(v in a){if(a[v]>5)print v,a[v]}}'  

5、查看网卡实时流量脚本

#!/bin/bash  
NIC=$1  
echo -e " In ------ Out"  
while true; do  
    OLD_IN=$(awk '$0~"'$NIC'"{print $2}' /proc/net/dev)  
    OLD_OUT=$(awk '$0~"'$NIC'"{print $10}' /proc/net/dev)  
    sleep 1  
    NEW_IN=$(awk  '$0~"'$NIC'"{print $2}' /proc/net/dev)  
    NEW_OUT=$(awk '$0~"'$NIC'"{print $10}' /proc/net/dev)  
    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"  
    sleep 1  
done  

6、批量检测网站是否异常并邮件通知

#!/bin/bash    
URL_LIST="www.baidu.com www.ctnrs.com www.der-matech.net.cn www.der-matech.com.cn www.der-matech.cn www.der-matech.top www.der-matech.org"  
for URL in $URL_LIST; do  
    FAIL_COUNT=0  
    for ((i=1;i<=3;i++)); do  
        HTTP_CODE=$(curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}" $URL)  
        if [ $HTTP_CODE -eq 200 ]; then  
            echo "$URL OK"  
            break  
        else  
            echo "$URL retry $FAIL_COUNT"  
            let FAIL_COUNT++  
        fi  
    done  
    if [ $FAIL_COUNT -eq 3 ]; then  
        echo "Warning: $URL Access failure!"  
  echo "网站$URL坏掉,请及时处理" | mail -s "$URL网站高危" xxxxx@163.com  
    fi  
done  

7、目录入侵检测与告警

#!/bin/bash  
  
MON_DIR=/opt  
inotifywait -mqr --format %f -e create $MON_DIR |\  
while read files; do  
   #同步文件  
   rsync -avz /opt /tmp/opt  
  #检测文件是否被修改  
   #echo "$(date +'%F %T') create $files" | mail -s "dir monitor" xxx@163.com  
done  

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

#!/bin/bash  
function 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"  
}  
cpu  
memory  
disk  
tcp_status  

9、以 root 身份运行整个 shell 脚本

将 sudo 放在 shell 脚本的首中,会以 root 身份运行整个程序。对设计用于例如的脚本很有用 自动化系统升级或包管理器包装器——不再需要用 sudo 预先准备一切

#!/usr/bin/sudo /bin/bash  

10、获取Linux系统相关信息

这里列出了如何获取Linux系统相关信息的脚本,主要包括主机名称,运行时间,系统版本,内核版本等主要Linux系统信息以及相关CPU,内存,磁盘;ubuntu需使用bash执行,否则会输出-e 。

#!/bin/bash
echo -e "-------------------------------System Information----------------------------"
echo -e "Hostname:\t\t"$(hostname)
echo -e "uptime:\t\t\t"$(uptime | awk '{print $3,$4}' | sed 's/,//')
echo -e "Manufacturer:\t\t"$(cat /sys/class/dmi/id/chassis_vendor)
echo -e "Product Name:\t\t"$(cat /sys/class/dmi/id/product_name)
echo -e "Version:\t\t"$(cat /sys/class/dmi/id/product_version)
echo -e "Serial Number:\t\t"$(cat /sys/class/dmi/id/product_serial)
echo -e "Machine Type:\t\t"$(
    vserver=$(lscpu | grep Hypervisor | wc -l)
    if [ $vserver -gt 0 ]; then echo "VM"; else echo "Physical"; fi
)
echo -e "Operating System:\t"$(hostnamectl | grep "Operating System" | cut -d ' ' -f5-)
echo -e "Kernel:\t\t\t"$(uname -r)
echo -e "Architecture:\t\t"$(arch)
echo -e "Processor Name:\t\t"$(awk -F':' '/^model name/ {print $2}' /proc/cpuinfo | uniq | sed -e 's/^[ \t]*//')
echo -e "Active User:\t\t"$(w | cut -d ' ' -f1 | grep -v USER | xargs -n1)
echo -e "System Main IP:\t\t"$(hostname -I)
echo ""
echo -e "-------------------------------CPU/Memory Usage------------------------------"
echo -e "Memory Usage:\t"$(free | awk '/Mem/{printf("%.2f%"), $3/$2*100}')
echo -e "Swap Usage:\t"$(free | awk '/Swap/{printf("%.2f%"), $3/$2*100}')
echo -e "CPU Usage:\t"$(cat /proc/stat | awk '/cpu/{printf("%.2f%\n"), ($2+$4)*100/($2+$4+$5)}' | awk '{print $0}' | head -1)
echo ""
echo -e "-------------------------------Disk Usage >80%-------------------------------"
df -Ph | sed s/%//g | awk '{ if($5 > 80) print $0;}'
echo ""
echo -e "-------------------------------For WWN Details-------------------------------"
vserver=$(lscpu | grep Hypervisor | wc -l)
if [ $vserver -gt 0 ]; then
    echo "$(hostname) is a VM"
else
    cat /sys/class/fc_host/host?/port_name
fi

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值