1 查找当前网段(192.168.159.0/24)内存活IP用户,重定向到/tmp/ip.txt文件中
#!/bin/bash
for i in {0..255};do(
ping -c1 -W1 192.168.159.$i > /dev/null 2>&1
if[ $? -eq 0 ]; then
echo "192.168.159.$i is alive" >/tmp/ip.txt
fi
)&
}
cat /tmp/ip.txt
192.168.159.1 is alive
192.168.159.2 is alive
192.168.159.129 is alive
2 自动创建用户student101-150,且密码为password101-150
#!/bin/bash
for i in {101..105};do(
id -u student$i > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "user student$i is existed, change password"
echo "password$i" | passwd --stdin student$i
else
useradd student$i
echo "password$i" | passwd --stdin student$i
echo "user student$i is added"
fi
)
done
3 编写一个shell脚本,检测当前服务器正在运行的网络服务,并输出服务名(netstat)
#!/bin/bash
netstat -tulnp | grep -E '^tcp|^udp' | awk -F\/ '{print$2}' | sort -u
4 根据passwd文件内容,输出“The line 1(行数) is root(用户名)”,依此类推
#!/bin/bash
n=1
while read line; do
user=`echo $line | awk -F: '{print $1}'`
echo "The line$n is $user"
let n++
done < /etc/passwd
5 创建一个shell脚本test6.sh:
- 当你输入“kernel”参数时,此shell脚本就输出“user“
- 当你输入”user“参数时,此shell脚本就输出”kernel”
- 当此脚本不输入任何参数或者输入的参数是按照要求输入的话,那么就会输出标准错误“usage:./test6.sh kernel|user
#!/bin/bash
t=$1
if [[ $1 = kernel ]]; then
echo user
elif [[ $1 = user ]]; then
echo kernel
else
echo "usage:./test6.sh kernel|user"
exit 1
fi
6 打印无密码用户
#!/bin/bash
while read line; do
pass=`echo $line | cut -d: -f2`
user=`echo $line | cut -d: -f1`
if [[ $pass = * || $pass = !! ]]; then
echo "user"
fi
done < /etc/shadow
7 写一个脚本,显示当前系统上shell为-s指定类型的shell,并统计其用户总数。-s选项后面跟的参数必须是/etc/shells文件中存在的shell类型,否则不执行此脚本。另外,此脚本还可以接受–help选项,以显示帮助信息。脚本执行形如:
- ./test8.sh -s bash
- bash,3users,they are:
- root redhat gentoo
#!/bin/bash
if [ $
echo "Error, missing parameter"
exit 1
fi
case $1 in
-s)
shell=$2
flag=`grep "$shell" /etc/shells`
if [ -n "$flag" ]; then
user_number=`grep "$shell$" /etc/passwd | wc -l`
user_list=`grep "$shell$" /etc/passwd | awk -F: '{print$1}'`
echo "$shell, ${user_number}user, they are:"
echo $user_list
else
echo "Wrong shell!!"
exit 1
fi
;;
-h|--help)
echo "Usage: $0 -s SHELL_TYPE"
exit 0
;;
*)
echo "syntax error, use -h or --help to get help"
exit 1
esac
8 监控磁盘的使用率,>90%时给root发邮件
df -P | sed '1d' | while read line; do
usage=`echo $line | awk '{print $5}'`
usage=`echo ${usage%%\%*}`
disk=`echo $line | awk '{print $6}'`
if [ $usage -gt 90 ]; then
echo "$disk is in a high usage!" | mail -s "Warnning!!" root@localhost
fi
done