简介
shell示例总结。
shell脚本示例
示例1:过滤出本系统的 IP 地址
代码:
ifconfig ens33|grep netmask|awk '{print $2}'
ifconfig ens33|awk 'NR==2{print $2}'
示例2 :打印出 passwd 文件中用户 UID 小于 10 的用户名和它登录使用的 shell
代码:
awk -F: '$3<10{print $1"\t"$NF}' /etc/passwd
示例3:打印出系统中 UID 大于等于 1000 且登录 shell 是/bin/bash 的用户
代码:
awk -F: '$3>=1000 && $NF=="/bin/bash"{print $1"\t"$NF}' /etc/passwd
示例4:统计当前内存的使用率
代码:
#!/bin/bash
echo "内存使用比:"
userfree=$(free -m |grep -i mem|awk '{print $3/$2*100"%"}')
echo -e "\033[31m${userfree}\033[0m"
示例5:使用 awk 查出以包括 root 字符的行
代码:
awk -F: '/root/{print $0}' /etc/passwd
示例6:将/etc/passwd第 1 列使用 15 个字符宽度左对齐输出,最后一列使用 15 个字符宽度右对齐输出
代码:
awk -F: '{printf "username:""%-15s %15s\n",$1,$NF}' /etc/passwd
示例7:检查服务器是否受到 DDOS 攻击
代码:
#!/bin/bash
ddos=$(netstat -ntpu|sed 1,2d|awk '{print $5}'|cut -d: -f1|uniq -c|sort -n)
echo -e "\033[42;31m 链的ip数量和地址\033[0m:"
echo "************************"
echo "$ddos"
echo "************************"
示例8:bash中使用随机密码的三种方法
代码:
1. openssl rand -base64 4|md5sum|cut -c 1-8
2. date +%N|md5sum|cut -c 1-8
3. echo $RANDOM|md5sum|cut -c 1-8
示例9:(新)批量创建用户
代码:
#!/bin/bash
USER_FILE=/root/a.txt
#颜色函数
echo_color(){
if [ $1=="red" ];then
echo -e "\033[31m $2\033[0m"
elif [ $1=="green" ];then
echo -e "\033[32m $2\033[0m"
fi
}
#用户文件是否存在
if [ -s $USER_FILE ];then
mv $USER_FILE $USER_FILE-$(date +"%F-%T").bak
echo_color green "$USER_FILE is exist,rename $USER_FILE-$(date +"%F-%T").bak"
else
touch $USER_FILE
echo_color green "$USER_FILE is created successfully!"
fi
#创建用户
echo -e "User Password\n---------------------">>$USER_FILE
for i in user{9..10};do
if ! id $i &>/dev/null;then
pass=$(echo $RANDOM|md5sum|cut -c 1-8)
useradd -M $i
echo $pass|passwd --stdin $i &>/dev/null
echo -e "$i $pass">> $USER_FILE
echo_color green "user $i create successfully!"
else
echo_color red "user $i already exist!"
fi
done
示例10:测试给出的ip地址列表,超过3个地址不通打印
代码:
IP_LIST="192.168.130.1 10.10.10.1 10.10.80.2"
for i in $IP_LIST;do
NUM=1
while [ $NUM -le 3 ];do
if ping -c1 $i &>/dev/null;then
echo "Ping $i is successfully!"
break
else
Fail_count[$NUM]=$i
let NUM++
echo "Ping $i is failure!"
fi
done
if [ ${#Fail_count[*]} -eq 3 ];then
echo "ping ${Fail_count[1]} is failure!"
unset Fail_count[*]
fi
done
示例11:输出系统信息
#!/bin/bash
#硬盘信息
disk_all=$(df -Th|grep "dev"|awk '{print $3}'|head -1)
disk_free=$(df -Th|grep "dev"|awk '{print $5}'|head -1)
#内存信息
mum_all=$(free -m|awk '/Mem/{print $2}')
mum_free=$(free -m|awk '/Mem/{print $4}')
swap_all=$(free -m|awk '/Swap/{print $2}')
swap_free=$(free -m|awk '/Swap/{print $4}')
#cpu信息
cpu_info=$(awk -F: '/model name/{print $2}' /proc/cpuinfo |sed 's/ //'|head -1)
cpu_heshu=$(cat /proc/cpuinfo |grep process|wc -l)
#cpu_heshu=$(awk -F: '/process/{print $2}' /proc/cpuinfo |sort|uniq|wc -l)
cpu_phy=$(cat /proc/cpuinfo |grep physical|sort|uniq|wc -l)
#网络信息
ip_local=$(ifconfig |egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}"|grep -v "127"|head -1)
net_curl=$([ `curl -o /dev/null -m 3 -s -w "%{http_code}" www.baidu.com` -eq 200 ]&&echo yes||echo no)
#版本信息
ver_sion=$(cat /etc/redhat-release |awk '{print $1,$4}')
Sys_info(){
echo "----------------------------------
主机名: $(hostname)
系统版本: $ver_sion
cpu信息: $cpu_info
cpu逻辑核数:$cpu_heshu
cpu物理核数:$cpu_phy
总硬盘: $disk_all
剩余硬盘: $disk_free
总内存: $mum_all
剩余内存: $mum_free
剩余Swp内存:$swap_free
主机地址: $ip_local
是否联网: $net_curl
-----------------------------------"
}
Sys_info
写文档不易,喜欢的点个赞吧!