当前位置: 首页> 教育> 高考 > Shell编程实际应用

Shell编程实际应用

时间:2025/7/19 9:26:15来源:https://blog.csdn.net/zheshijiuyue/article/details/139395062 浏览次数:0次

 一、脚本编程步骤

1. 需求分析
2. 命令测试
3. 脚本编程
4. 测试调优

二、案例分析

1.MAC记录与端口扫描

实验要求:
(1)统计网络中服务器MAC。
(2)检查哪些主机开启FTP。(21)(wget)($?)

netadd="192.168.10."
file="/opt/ethers"
[ -f $file ] && /bin/mv -f $file $file.bak
hostadd=100while [ $hostadd -le 107 ]
doping -c 2 -i 0.2 -W 2 ${netadd}${hostadd}
if [ $? -eq 0 ]
thenarp -n | grep ${netadd}${hostadd} | awk '{print $1,$3}'>>$file
fi
let hostadd++
done
echo "主机列表是:"
cat $file
echo "以下主机开启FTP"
target=$(awk '{print $1}' /opt/ethers)
for IP in $target
do
wget ftp://$IP &>/dev/null
if [ $? -eq 0 ]
thenecho $IPrm -rf index.*
fi
done

 备注:
 [root@localhost ~]# arp -n   (查看MAC地址)

2. 开发监控脚本

实验要求:
(1)CPU,内存,磁盘l利用率
(2)报警
(3)通过邮箱发送邮件

dug=$(df | grep "/$" | awk '{print $5}' | awk -F% '{print $1}')
cug=$(expr 100 - $(mpstat | tail -1 | awk '{print $12}' | awk -F. '{print $1}'))
mug=$(expr $(free | grep Mem | awk '{print $3}') \* 100 / $(free | grep Mem | awk '{print $2}'))
alog="/tmp/alert.txt"
amail="xxxxx@qq.com"
if [ $dug -gt 80 ]
thenecho "磁盘利用率:${dug}%">>$alog
fi
if [ $cug -gt 80 ]
thenecho "cug利用率:${cug}%">>$alog
fi
if [ $mug -gt 80 ]
thenecho "内存利用率:${mug}%">>$alog
fiif [ -f $alog ]
thencat $alog | mail -s "Host Alert" $amailrm -rf $alog
fi

 备注:
[root@localhost ~]# df     (查看磁盘利用率)
[root@localhost ~]# mpstat  (查看cpu)
[root@localhost ~]# free    (查看内存)
[root@localhost ~]# expr 235044 \* 100 / 3988652  (计算内存利用率,已使用内存空间*100 /总空)

[root@localhost ~]# yum -y install mailx
[root@localhost ~]# vim /etc/mail.rc 
set from=xxxxx@.com smtp=smtp.qq.com
set smtp-auth-user=xxxxx@qq.com smtp-auth-password=xxxx邮箱授权码
set smtp-auth=login

 备注:
set from=user@xxx.com               # 发信人邮箱
set smtp=smtps://smtp.xxx.com:465   #smtp地址
set smtp-auth=login                 # 认证方式
set smtp-auth-user=user@xxx.com     # 邮箱账号
set smtp-auth-password=password     # 邮箱授权码

关键字:Shell编程实际应用

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: