Linux系统监控脚本实战

1、局域网网段内主机是否存活脚本
ping.sh
#!/bin/bash
#ping and test hosts of lans is alive!
for i in {1..254}
do
host=10.57.36.$i
ping -c3 $host >& /dev/null
if [ $? = 0 ];
then
   echo "$host is alive!" >> /tmp/hosts.log
else
   echo "$host is down!" >> /tmp/hosts.log
fi
done
chmod +x ping.sh

2、Linux下批量创建人事部门指定用户帐号并设置8位随机密码
addusers.sh
#!/bin/bash
##for i in `seq -w 10`
for name in zhang3 li4 wang5 zhao6
do
passwd=`echo $RANDOM | md5sum | cut -c 1-8`
useradd $name && echo "$passwd" | passwd --stdin $name
echo -e "username:$name \t password:$passwd" >> /tmp/userandpw.log
##useradd bruce$i && echo "$passwd" | passwd --stdin bruce$i
##echo -e "user:bruce$i \t password:$passwd" >> /tmp/userandpw.log
done
chmod +x addusers.sh
[root@Titu-BackupFS ~]# ./addusers.sh
Changing password for user zhang3.
passwd: all authentication tokens updated successfully.
Changing password for user li4.
passwd: all authentication tokens updated successfully.
Changing password for user wang5.
passwd: all authentication tokens updated successfully.
Changing password for user zhao6.
passwd: all authentication tokens updated successfully.
[root@Titu-BackupFS ~]# tail -5 /etc/passwd
impmonitor:x:501:501::/home/impmonitor:/bin/bash
zhang3:x:502:502::/home/zhang3:/bin/bash
li4:x:503:503::/home/li4:/bin/bash
wang5:x:504:504::/home/wang5:/bin/bash
zhao6:x:505:505::/home/zhao6:/bin/bash
[root@Titu-BackupFS ~]# cat /tmp/userandpw.log
username:zhang3          password:55ccd445
username:li4     password:85e66b5d
username:wang5   password:c39c6543
username:zhao6   password:022f3992
若需要创建大量不同姓氏的用户帐号密码,如何解决呢?可考虑先创建姓全拼,名简写的英文缩写用户帐号的文本文件,再通过for语句结合查看命令来实现循环创建用户,后续新增用户帐号只需修改文本文件即可!
[root@Titu-BackupFS ~]# cat > realusers.txt << EOF
> jack
> tom
> joe
> jane
> bruce
> EOF
修改addusers.sh脚本中for语句,引用外部文件的记录
for name in `cat /root/realusers.txt`
其他保持不变!
addusers-new.sh
#!/bin/bash
for name in `cat /root/realusers.txt`
do
passwd=`echo $RANDOM | md5sum | cut -c 1-8`
useradd $name && echo "$passwd" | passwd --stdin $name
echo -e "username:$name \t password:$passwd" >> /tmp/userandpw.log
done
chmod +x addusers-new.sh

未完待续.....

你可能感兴趣的:(linux,系统监控,小脚本)