创建用户
#!/usr/bin/bash
read -p "Please input a username: " user
# 用户是否存在
if id $user &>/dev/null;then
echo "user $user already exists"
else
useradd $user
if[ $? -eq 0 ];then
echo "$user is created."
fi
fi
- 批量创建用户
#!/usr/bin/bash
read -p "Please input number: " num
read -p "Please input prefix: " prefix
# seq能创建一个数值序列
for i in `seq $num`
do
user=$prefix$i
useradd $user
# 设置密码
ehco "123" |passwd --stdin $user
if [ $? -eq 0 ];then
echo "$user is created."
fi
done
- 使用文件批量创建用户
文件类型为
用户名 密码
ljw1 12344
ljw23 32133
...
#!/usr/bin/bash
#v1.0 by ljw
#判断有没有输入文件参数
if [ $# -eq 0 ];then
echo "usage: `basename $0` file"
exit 1
fi
if [ ! -f $1 ];then
echo "error file"
exit 2
fi
#希望for处理文件时按回车分割,而不是空格或者tab空格
#重新定义分隔符
#IFS内部字段分隔符
IFS=$'\n'
for line in `cat $1`
do
#循环会自动忽略空行
user= `echo "$line" |awk '{print $1 }' `
pass= `echo "$line" |awk '{print $2 }' `
if[ $ -eq 0 ];then
echo "user $user alreadly exists"
else
useradd $user
echo "$pass" |passwd --stdin $user &>/dev/null
if[ $? -eq 0 ];then
echo "$user is created."
fi
fi
done
磁盘内存警告
#!/usr/bin/bash
# 第一个管道符用grep挑选出以'/'结尾的一行
# 第二个管道符用awk挑选出倒数第二个参数(NF是倒数最后一个)
# 第三个管道符用awk以%为分隔符分隔出数字
disk_use=`df -Th |grep '/$' | awk '{print $(NF-1)}' |awk -F"%" '{print $1}'`
mail_user=ljw
#磁盘超过90%
if [ $disk_use -ge 90 ];then
#发送警告邮件给ljw用户
echo "`date +%F-%H`disk:${disk_use}%" | mail -s "disk war..." ${mail_user}
fi
类似的可以查看内存
#!/usr/bin/bash
mem_used=`free -m |grep '^Mem' |awk '{print $3}'`
mem_total=`free -m |grep '^Mem' |awk '{print $2}'`
mem_percent=$((mem_used*100/mem_total))
war_file=/tmp/mem_war.txt
mail_user=ljw
rm -rf ${war_file}
if [ $mem_percent -ge 80 ];then
echo "`date +%F-%H`memory:${mem_percent}%" > ${war_file}
fi
if [ -f $war_file ];then
mail -s "men war..." ${mail_user} < ${war_file}
rm -rf ${war_file}
fi
可以将该类型脚本放入crontab定期执行
脚本安装apache
#!/usr/bin/bash
#install apache
gateway=192.168.1.1
ping -c1 www.baidu.com &>/dev/null
if[ $? -eq 0 ];then
yum -y install httpd
systemctl start httpd
systemctl enable httpd
#开启防火墙
firewall-cmd --permanent -add-service=http
firewall-cmd --permanent -add-service=https
firewall-cmd --reload
#查找以SELINUX=为开头的行换成SELINUX=disabled
sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
#设置SELinux 成为permissive模式 临时关闭selinux的
setenforce 0
curl http://127.0.0.1 &>/dev/null
if[ $? -eq 0 ];then
echo "Install Apache ok..."
else
echo "Install Apache error..."
fi
# 尝试ping网关
elif ping -c1 $gateway &>/dev/null;then
echo "check dns..."
exit
else
echo "check ip address!"
fi
实现简易跳板机功能
物理机通过一个远程的跳板机登陆web服务器
免密登陆可以使用ssh-keygen ,通过ssh-copy-id来实现
[alice@hostname ~]$ ssh-keygen
[alice@hostname ~]$ ssh-copy-id -i .ssh/id_rsa.pub 用户名字@192.168.x.xxx
#!/usr/bin/bash
#jumpserver
# 捕捉脚本信号什么都不做,防止客户等ctrl+c退出
trap "" HUP INT OUIT TSTP
web1=192.168.1.241
web2=192.168.1.210
mysql1=192.168.1.52
clear
while :
do
cat <<-EOF
1. web1
2. web2
3. mysql1
EOF
read -p "input number:"num
case in
1)
ssh alice@$web1
;;
2)
ssh alice@$web2
;;
3)
ssh alice@$mysql1
;;
"")
;;
*)
echo "error"
esac
done
在.bash_profile添加脚本路径就能登陆自动执行
统计不同类型的shell数量
#!/bin/bash
#count shells
#声明关联数组
declare -A shells
while read line
do
if [ ${#line} -eq 0 ];then
continue
fi
type=`echo $line |awk -F":" '{print $NF}'`
if [ ${#type} -eq 0 ];then
continue
fi
let shells[$type]++
done </etc/passwd
for i in ${!shells[@]}
do
echo "$i: ${shells[$i]}"
done
- 统计TCP状态
#!/bin/bash
#count tcp status
while :
do
declare -A status
type=`ss -an |grep :80 |awk '{print $2}'`
for i in $type
do
let status[$i]++
done
for j in ${!status[@]}
do
echo "$j: ${status[$j]}"
done
sleep 1
clear
unset status
done