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