Linux系统监控脚本实战

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
[[email protected] ~]# ./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.
[[email protected] ~]# 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
[[email protected] ~]# cat /tmp/userandpw.log
username:zhang3          password:55ccd445
username:li4     password:85e66b5d
username:wang5   password:c39c6543
username:zhao6   password:022f3992
若需要创建大量不同姓氏的用户帐号密码,如何解决呢?可考虑先创建姓全拼,名简写的英文缩写用户帐号的文本文件,再通过for语句结合查看命令来实现循环创建用户,后续新增用户帐号只需修改文本文件即可!
[[email protected] ~]# 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

未完待续.....

本文出自 “Bruce_tan” 博客,请务必保留此出处http://380281.blog.51cto.com/370281/1759156

以上是关于Linux系统监控脚本实战的主要内容,如果未能解决你的问题,请参考以下文章

linux下怎么查看自己所在局域网的其他主机?

shell脚本监控(ping)主机是否存活

主动信息收集--二层发现(shell脚本)

Linux shell 脚本监控cpu,内存,硬盘,网络,是否存活

最近练习的一些经典Shell脚本实战30个

[na]tcp&udp扫描原理