快速脚本合集

最大20个文件

du -h / | sort -rh | head -20
find / -type f -exec du -sb {} + | awk '{ size_gb = $1 / (1024*1024*1024); printf "%.2fGB\t%s\n", size_gb, $2 }' | sort -nr | head -n 20

快速命令

查看所有文件名和内容

awk 'FNR==1 {print FILENAME} {print}' *.conf | less

查看去掉注释和空行

grep -Ev '^\s*($|#|;)' example.conf

删除除最近修改以外的所有zip文件

find . -type f -name "*.zip" -printf "%T@ %p\n" | sort -nr | tail -n +2 | awk '{print $2}' | xargs rm -f

多线程压缩

tar -cf - test | pigz -p 3 > test.tar.gz

分组查看请求类型

ss -ano | awk '{print $2}' | sort -n | uniq -c | sort

单行改密码

echo <passwd> | passwd root --stdin
# salt批量修改密码
python3 -c "import crypt; print(crypt.crypt('your_password', crypt.mksalt(crypt.METHOD_SHA512)))"
salt '*' shadow.set_password root '<hashed_password>'

删除30天前文件

find /data/app/tmp -mtime +30 -name "*.flv" -exec rm -Rf {} \;

关闭swap

sed -ri 's/.*swap.*/#&/' /etc/fstab && swapoff -a

随机密码

PASSWORD=$(base64 < /dev/urandom | head -c16); echo "$PASSWORD"; echo -n "$PASSWORD" | sha1sum | tr -d '-' | xxd -r -p | sha1sum | tr -d '-'

当前目录修改字符串

for file in $(ls); do sed -i 's/nmg/sz/g' "$file"; done

Docker Build构建使用http代理

docker build --build-arg https_proxy=127.0.0.1:8088

保存包并安装

debian12
centos7
apt-get install dpkg-repack

# 保存主软件包
dpkg-repack telnet
apt-get download telnet

# 保存主软件包和所有依赖
apt-cache depends telnet

# 导入包
dpkg -i telnet.deb

htpasswd 密码

USERNAME=muen-admin;
PASSWORD=$(base64 < /dev/urandom | head -c16);
echo "USERNAME:$USERNAME,PASSWORD:$PASSWORD";docker run --rm \
--entrypoint htpasswd \
httpd:alpine \
-mbn $USERNAME $PASSWORD > nginx.htpasswd

# -mbn Force MD5 encryption
# -Bbn Force bcrypt encryption

生成ssh证书

ssh-keygen -t rsa -P '' -f /root/.ssh/id_rsa

cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

# 然后保存/root/.ssh/id_rsa用于连接

线上排错

连接数占用

# 查看源端口为40000端口的,客户端连接按数量排序前50个
ss -tn sport = :40000 | awk '{print $5}' | cut -d':' -f1 | sort | uniq -c | sort -nr | head -n 50
# 去重连接数
ss -tn sport = :40000 | awk 'NR > 1 {print $4}' | cut -d':' -f1 | sort -u | wc -l
# 总连接数
ss -ano | grep 40000 | wc -l

ssh代理

原生ssh

ssh代理

自动重连

-M后面传0就是随机在远程主机上32768-65535开一个端口来监听,如果传自定义值就是指定端口监听

TIP

注:还是会监听22端口

yum install -y autossh

autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -NfR *:11000:172.168.1.1:6061 root@172.168.1.1

docker清理

清理镜像

docker image prune -a 

显示当前占用磁盘空间的 Docker 资源

docker system df

清理构建缓存

docker builder prune -a

删除未被使用的容器、网络和数据卷等其他资源

docker system prune -a

rsync断点续传

#!/bin/bash

cmd_status=1
while [ ${cmd_status} != 0 ]; do
# rsync -rP -e "ssh -i /root/test.pem" /data/app/ 192.168.1.1:/data/app/
# 1M/s
# yum install nc -y
# apt install -y netcat-openbsd
# 老版本 nc -x
# rsync -rP --bwlimit=1024 --rsh="ssh -o ProxyCommand='nc -x x.x.x.x:44066 %h %p'" ${des_dir}/ root@${ip}:${src_dir}/${sync_file}
# 新版本 nc --proxy-type socks5 --proxy ${ip}:${port}
# rsync -rP --bwlimit=1024 --rsh="ssh -o ProxyCommand='nc --proxy-type socks5 --proxy-auth username:password --proxy x.x.x.x:44066 %h %p'" ${des_dir}/ root@${ip}:${src_dir}/${sync_file}
rsync -rP --bwlimit=1024 --rsh="ssh"  /data/baksvn/20200906 root@172.31.37.123:/data/baksvn/
cmd_status=$?
done
TIP

如果要使用其他用户传输: rsync -e 'ssh -l <user>'

代理相关

curl使用代理

# socks5代理
curl -x socks5h://user:xxx@192.168.1.1:1080

脚本格式

# 需要用到dos2unix包
find . -type f -exec dos2unix {} \;

脚本传参

#!/bin/bash

parse_commandline() {
  while test $# -gt 0
  do
    key="$1"
    case "$key" in
      -i|--install-dir)
        PARSED_INSTALL_DIR="$2"
        shift 2
        ;;
      -b|--bin-dir)
        PARSED_BIN_DIR="$2"
        shift 2
        ;;
      -u|--update)
        PARSED_UPGRADE="yes"
        shift
        ;;
      -h|--help)
        usage
        exit 0
        ;;
      *)
        die "Got an unexpected argument: $1"
        ;;
    esac
  done
}

usage() {
  echo "Usage: scriptname [options]"
  echo "Options:"
  echo "  -i, --install-dir    Specify installation directory"
  echo "  -b, --bin-dir        Specify binary directory"
  echo "  -u, --update         Update the installation"
  echo "  -h, --help           Show this help message"
}

die() {
  echo "$@" >&2
  exit 1
}

parse_commandline "$@"
echo "Install directory: $PARSED_INSTALL_DIR"
echo "Bin directory: $PARSED_BIN_DIR"

在csv文件中排除

temp_file_name="temp_database.csv"
exclude_list="
aaa
"
function exclude_database {
    file=$1

    rm -f ${file}

    line_num=0
    for instance in $(cat ${temp_file_name}|egrep -v "^#");do
        id=$(echo "${instance}" |awk -F ',' '{print $1}')
        excluded=false

        for excluded_item in $exclude_list; do
            if [[ "$id" == "$excluded_item" ]]; then
                excluded=true
                break
            fi
        done

        if [ "$excluded" == false ]; then
            echo "${instance}" >> ${file}
        fi
    done

    rm -f ${temp_file_name}
}

连接数优化

cat << EOF >> /etc/sysctl.conf
net.ipv6.conf.all.disable_ipv6 = 1
net.core.somaxconn=65535
net.ipv4.tcp_max_tw_buckets=1440000
net.ipv4.ip_local_port_range=1024 65000
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_fin_timeout=15
EOF
sysctl -p

测试相关

测速

centos7
debian12
yum install -y epel-release
yum install -y iperf3
# 默认端口 5201
# 服务端
iperf3 -s
# 客户端
# 上传
iperf3 -c <server_ip>
# 下载
iperf3 -c <server_ip> -R