那些年我们一起做过的shell面试题(二)

企业真实shell面试题,一起来挑战把!

声明:如有雷同,纯属抄袭

 

 

1、批量创建系统用户oldboy01-oldboy10,并随机生成密码

[root@xiaoya ~]# cat /server/scripts/user_add.sh
#!/bin/bash
# date 2016-1-5
# author tom
# mail [email protected]
# function useradd
# version 4.1.2

A=oldboy
for n in {01..10}
do
useradd $A$n -s /bin/bash
passwd=$(echo $RANDOM|md5sum|cut -c 3-10)
echo $passwd|passwd --stdin $A$n
echo $A$n >>~/oldboy.txt
echo $passwd >>~/oldboy.txt
done

###########批量删除系统用户#############

[root@xiaoya ~]# cat /server/scripts/user_del.sh
#!/bin/bash
# date 2016-1-5
# author tom
# mail [email protected]
# function delete user from os
# version 4.1.2

for n in {01..10}
do
id oldboy$n &>/dev/null
  if [ $? -eq 0 ]
  then
    userdel -r oldboy$n
    echo "delete oldboy$n successful"
  else
    echo "oldboy$n is not exit"
  fi
> ~/oldboy.txt
done

 

2、写一个脚本,实现判断10.0.0.0/24网段里,有哪些用户的IP在使用

[root@xiaoya scripts]# cat ping.sh
#!/bin/bash
# date 2016-1-5
# author tom
# mail [email protected]
# function delete user from os
# version 4.1.2

. /etc/init.d/functions
echo "##########`date` start##########">>~/true.txt
for n in {1..254}
do
  ping -c 1 -w 1 10.0.0.$n &>/dev/null
  if [ $? -eq 0 ]
  then
    action "10.0.0.$n is exit" /bin/true >>~/true.txt
  fi
done
echo "##########`date` stop###########">>~/true.txt
##此脚本可以记录脚本的执行时间,贴图为证

[root@xiaoya ~]# cat true.txt
##########2016年 01月 07日 星期四 17:08:30 CST start##########
10.0.0.1 is exit                                           [确定]
10.0.0.2 is exit                                           [确定]
10.0.0.4 is exit                                           [确定]
##########2016年 01月 07日 星期四 17:12:42 CST stop###########

你可能感兴趣的:(server,用户,密码)