赞
踩
- # 安装net-tools工具
- apt install net-tools
- # 查看所有进程信息
- netstat -atunp
- # 查看fileserver进程信息
- netstat -atunp | grep fileserver
- # tcp6 0 0 :::8088 :::* LISTEN 17787/./fileserver
- lsof -i:8088
-
- # COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
- # fileserve 17787 root 3u IPv6 181633 0t0 TCP *:omniorb (LISTEN)
- # 统计输出信息的行数
- wc -l
-
- grep // 过滤、筛选
- ^ // 代表行首。例如,以-开头的一行,^-;以#开头的一行^#
- $ // 代表行位。例如,以#结尾的一行,#$;以-结尾的一行-$
- ^$ // 代表空行。
- -v // 代表排除。排除空行 -v ^$
-
-
- # 1.统计文件夹下文件个数
- ls -l | grep "^-" | wc -l
-
- # 2.统计文件夹下文件夹的个数
- ls -l | grep "^d" | wc -l
-
- # 3.统计文件夹下文件的个数,包含子文件夹里面的
- ls -lR | grep "^-" | wc -l
-
- # 4.统计文件夹下的文件夹个数,含子文件夹里面的
- ls -lR | grep "^d" | wc -l
- #方法一
- # 1.禁止ICMP协议访问--禁ping
- echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
- # 2.恢复ICMP协议访问--启用ping
- echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all
-
- #方法二
- chmod +w /etc/sysctl.conf
- echo "net.ipv4.icmp_echo_ignore_all=1" > /etc/sysctl.conf
- chmod -w /etc/sysctl.conf
- sysctl -p
-
- # 如果两个同时设置,方法一必须更改,才能生效
- ips=$(ip addr | grep 'inet' | grep -v 'inet6\|127.0.0.1' | grep -v grep | awk -F '/' '{print $1}' | awk '{print $2}')
-
- echo ${ips}
- #shell判断文件夹是否存在
-
- #如果文件夹不存在,创建文件夹
- if [ ! -d "/my_dir" ]; then
- mkdir -p /my_dir
-
- #shell判断文件,目录是否存在或者具有权限
-
- folder="/my_dir"
- file="/my_dir/test.txt"
-
- # -x 参数判断 $folder 是否存在并且是否具有可执行权限
- if [ ! -x "$folder"]; then
- mkdir "$folder"
- fi
-
- # -d 参数判断 $folder 是否存在
- if [ ! -d "$folder"]; then
- mkdir "$folder"
- fi
-
- # -f 参数判断 $file 是否存在
- if [ ! -f "$file" ]; then
- touch "$file"
- fi
- # -n 判断一个"变量"是否有值
- if [ ! -n "$file" ]; then
- echo "$file 变量为空!"
- exit 0
- fi
-
- # 判断两个变量的字符串内容是否相同
- if [ "$file1" = "$file2" ]; then
- echo "$file1 equal $file2"
- else
- echo "$file1 not equal $file2"
- fi
- sudo su
- apt install gcc make autoconf sysstat net-tools sysstat iftop wget lrzsz lsof unzip openssh-client openssh-server net-tools vim ntpdate -y
-
- # 服务器时间一定要调整为 UTC+8 (北京时间)
- date -R
- tzselect
- # 选择Asia->China->Beijing
- cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
- date -R
- # 设置定时同步时区
- if ! crontab -l |grep ntpdate &>/dev/null ; then
- (echo "* 1 * * * ntpdate time.windows.com >/dev/null 2>&1";crontab -l) |crontab
- # 或使用阿里云ntp服务器
- # (echo "* 1 * * * ntpdate ntp1.aliyun.com >/dev/null 2>&1";crontab -l) |crontab
- fi
sed -i '/SELINUX/{s/permissive/disabled/}' /etc/selinux/config
- if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then
- systemctl stop firewalld
- systemctl disable firewalld
- elif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then
- service iptables stop
- chkconfig iptables off
- fi
- if ! grep HISTTIMEFORMAT /etc/profile; then
- echo 'export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S `whoami` "' >> /etc/profile
- fi
- if ! grep "TMOUT=600" /etc/profile &>/dev/null; then
- echo "export TMOUT=600" >> /etc/profile
- fi
sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab
- # 允许root用户登录
- sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
- # 如果root没密码,需要设置root密码,否则root无密码无法登录
- sudo su
- passwd
- # 最后重启
- reboot
-
- # 不允许root用户登录
- sed -i 's/PermitRootLogin yes/#PermitRootLogin prohibit-password/' /etc/ssh/sshd_config
- reboot
-
原理:在客户机生成RSA公私钥对,把公钥给服务器,服务器接受了客户机公钥,就默认客户机可以免密登录
- # 1. 在客户机上生成rsa公私钥。
- rsa-keygen
-
- # 2. 将客户机的公钥复制到服务器用户的.ssh/authorized_keys文件中。
- # 例如:root用户是:/root/.ssh/authorized_keys
- cat /root/.ssh/id_rsa.pub # 复制公钥内容
- # 进入服务器
- vim /root/.ssh/authorized_keys # 将复制内容粘贴进去即可
-
- # 3. 第2步也可以一步操作完毕
- # 在客户机上执行命令
- ssh-copy-id root@ip # 输入密码,后面就可以免密登录了
- vim /etc/ssh/sshd_config
-
- # 允许密码登录
- PasswordAuthentication yes
-
- # 不允许密码登录
- PasswordAuthentication no
- if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then
- cat >> /etc/security/limits.conf << EOF
- * hard nofile 65535
- * soft nofile 65535
- root hard nofile 65535
- root soft nofile 65535
- EOF
- fi
-
- # 重启服务器生效
- reboot
- cat >> /etc/sysctl.conf << EOF
- net.ipv4.tcp_syncookies = 1
- net.ipv4.tcp_max_tw_buckets = 20480
- net.ipv4.tcp_max_syn_backlog = 20480
- net.core.netdev_max_backlog = 262144
- net.ipv4.tcp_fin_timeout = 20
- EOF
-
- # 重启系统生效
- reboot
- # 减少SWAP使用
- echo "0" > /proc/sys/vm/swappiness
- #!/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
- #!/bin/bash
- echo "-------------------CUP占用前10排序--------------------------------"
- ps -eo user,pid,pcpu,pmem,args --sort=-pcpu |head -n 10
- echo "-------------------内存占用前10排序--------------------------------"
- ps -eo user,pid,pcpu,pmem,args --sort=-pmem |head -n 10
- #!/bin/bash
- # 参数传入网卡名称
- eth0=$1
- echo -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
- #!/bin/bash
- # 需要定义host.info
- HOST_INFO=host.info
- for IP in $(awk '/^[^#]/{print $1}' $HOST_INFO); do
- #取出用户名和端口
- USER=$(awk -v ip=$IP 'ip==$1{print $2}' $HOST_INFO)
- PORT=$(awk -v ip=$IP 'ip==$1{print $3}' $HOST_INFO)
- #创建临时文件,保存信息
- TMP_FILE=/tmp/disk.tmp
- #通过公钥登录获取主机磁盘信息
- ssh -p $PORT $USER@$IP 'df -h' > $TMP_FILE
- #分析磁盘占用空间
- USE_RATE_LIST=$(awk 'BEGIN{OFS="="}/^\/dev/{print $NF,int($5)}' $TMP_FILE)
- #循环磁盘列表,进行判断
- for USE_RATE in $USE_RATE_LIST; do
- #取出等号(=)右边的值 挂载点名称
- PART_NAME=${USE_RATE%=*}
- #取出等号(=)左边的值 磁盘利用率
- USE_RATE=${USE_RATE#*=}
- #进行判断
- if [ $USE_RATE -ge 80 ]; then
- echo "Warning: $PART_NAME Partition usage $USE_RATE%!"
- echo "服务器$IP的磁盘空间占用过高,请及时处理" | mail -s "空间不足警告" 你的qq@qq.com
- else
- echo "服务器$IP的$PART_NAME目录空间良好"
- fi
- done
- done
- # 安装mailutils
- sudo apt-get install mailutils
- # 测试发送邮件
- echo '网站异常,请及时处理' | mail -s '网站高危' lamborg@mail.test.io
-
- # 以上邮件如果发送失败(垃圾邮件箱也没有),就尝试mutt
- # 安装mutt
- sudo apt-get install mutt
- # 测试发送邮件
- echo "高危" | mutt -s "网站异常" lamborg@mail.test.io
- #!/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坏掉,请及时处理" | mutt -s "$URL网站高危" lamborg@mail.ipsb.io
- fi
- done
- #!/bin/bash
- COMMAND=$*
- HOST_INFO=host.info
- for IP in $(awk '/^[^#]/{print $1}' $HOST_INFO); do
- USER=$(awk -v ip=$IP 'ip==$1{print $2}' $HOST_INFO)
- PORT=$(awk -v ip=$IP 'ip==$1{print $3}' $HOST_INFO)
- PASS=$(awk -v ip=$IP 'ip==$1{print $4}' $HOST_INFO)
- expect -c "
- spawn ssh -p $PORT $USER@$IP
- expect {
- \"(yes/no)\" {send \"yes\r\"; exp_continue}
- \"password:\" {send \"$PASS\r\"; exp_continue}
- \"$USER@*\" {send \"$COMMAND\r exit\r\"; exp_continue}
- }
- "
- echo "-------------------"
- done
- #!/bin/bash
- HOST=localhost
- USER=root
- PASSWD=123.com
- IO_SQL_STATUS=$(mysql -h$HOST -u$USER -p$PASSWD -e 'show slave status\G' 2>/dev/null |awk '/Slave_.*_Running:/{print $1$2}')
- for i in $IO_SQL_STATUS; do
- THREAD_STATUS_NAME=${i%:*}
- THREAD_STATUS=${i#*:}
- if [ "$THREAD_STATUS" != "Yes" ]; then
- echo "Error: MySQL Master-Slave $THREAD_STATUS_NAME status is $THREAD_STATUS!" |mail -s "Master-Slave Staus" xxx@163.com
- fi
- done
- #!/bin/bash
- # mysqldump -uroot -pxxx -B A > A.sql
- DATE=$(date +%F_%H-%M-%S)
- HOST=localhost
- USER=backup
- PASS=123.com
- BACKUP_DIR=/data/db_backup
- DB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys")
-
- for DB in $DB_LIST; do
- BACKUP_NAME=$BACKUP_DIR/${DB}_${DATE}.sql
- if ! mysqldump -h$HOST -u$USER -p$PASS -B $DB > $BACKUP_NAME 2>/dev/null; then
- echo "$BACKUP_NAME 备份失败!"
- fi
- done
- mysqldump -uroot -pxxx -A t > t.sql
- #!/bin/bash
- DATE=$(date +%F_%H-%M-%S)
- HOST=localhost
- USER=backup
- PASS=123.com
- BACKUP_DIR=/data/db_backup
- DB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys")
-
- for DB in $DB_LIST; do
- BACKUP_DB_DIR=$BACKUP_DIR/${DB}_${DATE}
- [ ! -d $BACKUP_DB_DIR ] && mkdir -p $BACKUP_DB_DIR &>/dev/null
- TABLE_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "use $DB;show tables;" 2>/dev/null)
- for TABLE in $TABLE_LIST; do
- BACKUP_NAME=$BACKUP_DB_DIR/${TABLE}.sql
- if ! mysqldump -h$HOST -u$USER -p$PASS $DB $TABLE > $BACKUP_NAME 2>/dev/null; then
- echo "$BACKUP_NAME 备份失败!"
- fi
- done
- done
- #!/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]}}' $LOG_FILE |sort -k3 -nr
- #!/bin/bash
- #nginx日志目录
- LOG_DIR=/www/server/nginx/logs
- #获取到上一天的时间
- YESTERDAY_TIME=$(date -d "yesterday" +%F)
- #归档日志取时间
- LOG_MONTH_DIR=$LOG_DIR/$(date +"%Y-%m")
- #归档日志的名称
- LOG_FILE_LIST="access.log"
-
- for LOG_FILE in $LOG_FILE_LIST; do
- [ ! -d $LOG_MONTH_DIR ] && mkdir -p $LOG_MONTH_DIR
- mv $LOG_DIR/$LOG_FILE $LOG_MONTH_DIR/${LOG_FILE}_${YESTERDAY_TIME}
- done
-
- kill -USR1 $(cat $LOG_DIR/nginx.pid)
- #!/bin/bash
-
- # 四个需要自定义的参数
- gitUrl=ssh://git@git.xxx.com/web3-dev/my_shop.git # 具体的git地址路径
- tmpPath=/opt/backup/ # 缓存备份路径,这里可以挂载硬盘,授权普通用户权限,可以自定义
- linewww=/usr/share/nginx/html/web3_dev/my_shop/ # 正式部署的项目路径,路径中不能有特殊字符,-也不行,可以用_。目录可以自定义
- www_user=www-data # Nginx代理的用户,有的需要设置用户属性,才能正常运行
-
- ##############################################################################
-
- # 以下是无须修改的代码
- projectName=`echo ${gitUrl##*/}|awk -F ".git" '{print $1}'` # 项目目录名称
- bkwww=${tmpPath}${projectName}_bk/
- YMD=$(date +%F)
- mdate=$(date +%T)
- mH=`echo ${mdate}|awk -F ':' '{print $1}'`
- mi=`echo ${mdate}|awk -F ':' '{print $2}'`
- ms=`echo ${mdate}|awk -F ':' '{print $3}'`
- bkdate=${YMD}_${mH}_${mi}_${ms}
-
- if [ ! -d ${linewww} ]; then
- mkdir ${linewww} -p
- fi
-
- if [ ! -d ${bkwww} ]; then
- mkdir ${bkwww} -p
- fi
-
- # 备份
- linewwwPath=${linewww}${projectName}
- if [ -d ${linewwwPath} ]; then
- cp -rf ${linewwwPath} ${bkwww}/${projectName}'_'${bkdate}
- fi
-
-
- # 拉取代码
- cd ${tmpPath}
- if [ ! -d ${projectName} ]; then
- # 自动clone仓库
- git clone ${gitUrl}
- cd ${projectName}
- else
- cd ${tmpPath}${projectName}
- echo ${tmpPath}${projectName}
- # 自动拉取仓库更新
- git pull
- fi
-
-
- # 部署会复制clone下来的所有文件和文件夹,包括隐藏文件
- # 需要忽略的文件,添加 --exclude= 参数,多个添加多个参数
- mcmd="rsync -avpgolr --exclude=.git --exclude=.bashrc ${tmpPath}${projectName}/ ${linewww}"
- if [ ! -d ${linewww} ]; then
- mkdir -p ${linewww}
- ${mcmd}
- else
- ${mcmd}
- fi
- # 设置文件用户,提供Nginx识别
- chown -R ${www_user}:${www_user} ${linewww}*
- # 部署脚本给执行权限,以便后面升级
- chmod -R 777 ${linewww}*.sh
-
- #!/bin/bash
- DATE=$(date +%d/%b/%Y:%H:%M)
- #nginx日志
- LOG_FILE=/usr/local/nginx/logs/demo2.access.log
- #分析ip的访问情况
- ABNORMAL_IP=$(tail -n5000 $LOG_FILE |grep $DATE |awk '{a[$1]++}END{for(i in a)if(a[i]>10)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/drop_ip.log
- fi
- done
- #!/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" lamborg@mail.test.io
- done
grep "bash$" /etc/passwd | wc -l
- #!/bin/bash
-
- # 编写脚本测试 192.168.1.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机
- # 状态(for 版本)
- for i in {1..254}
- do
- # 每隔0.3秒ping一次,一共ping2次,并以1毫秒为单位设置ping的超时时间
- ping -c 2 -i 0.3 -W 1 192.168.1.$i &>/dev/null
- if [ $? -eq 0 ];then
- echo "192.168.1.$i is up"
- else
- echo "192.168.1.$i is down"
- fi
- done
- #!/bin/bash
- # 编写脚本:提示用户输入用户名和密码,脚本自动创建相应的账户及配置密码。如果用户
- # 不输入账户名,则提示必须输入账户名并退出脚本;如果用户不输入密码,则统一使用默
- # 认的 123456 作为默认密码。
-
- read -p "请输入用户名: " user
- #使用‐z 可以判断一个变量是否为空,如果为空,提示用户必须输入账户名,并退出脚本,退出码为 2
- #没有输入用户名脚本退出后,使用$?查看的返回码为 2
- if [ -z $user ]; then
- echo " 您不需要输入账户名"
- exit 2
- fi
- #使用 stty ‐echo 关闭 shell 的回显功能
- #使用 stty echo 打开 shell 的回显功能
- stty -echo
- read -p "请输入密码: " pass
- stty echo
- pass=${pass:-123456}
- useradd "$user"
- echo "$pass" | passwd --stdin "$user"
使用该命令,需要安装jq
apt install jq -y
- awk '{print $1}' /var/log/nginx/access.log | while read -r ip; do
- location=$(curl -s "http://ip-api.com/json/$ip?fields=country,regionName,city" | jq -r '"\(.country), \(.regionName), \(.city)"')
- echo "$ip - $location"
- done
去重后再查询
- awk '!seen[$1]++ {print $1}' /var/log/nginx/access.log | while read -r ip; do
- location=$(curl -s "http://ip-api.com/json/$ip?fields=country,regionName,city" | jq -r '"\(.country), \(.regionName), \(.city)"')
- echo "$ip - $location"
- done
去重后,统计IP访问次数,并列出归属地
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | while read -r count ip; do location=$(curl -s "http://ip-api.com/json/$ip?fields=country,regionName,city" | jq -r '"\(.country), \(.regionName), \(.city)"'); echo "IP地址: $ip, 出现次数: $count, 归属地: $location"; done
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。