Shell编程实际应用

一、脚本编程步骤

  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=100

while [ $hostadd -le 107 ]
do
    ping -c 2 -i 0.2 -W 2 ${netadd}${hostadd}
if [ $? -eq 0 ]
then
  arp -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 ]
then
     echo $IP
 rm -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 ] then echo "磁盘利用率:${dug}%">>$alog fi if [ $cug -gt 80 ] then echo "cug利用率:${cug}%">>$alog fi if [ $mug -gt 80 ] then echo "内存利用率:${mug}%">>$alog fi if [ -f $alog ] then cat $alog | mail -s "Host Alert" $amail rm -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 # 邮箱授权码

相关推荐
wj3055853786 小时前
课程 9:模型测试记录与 Prompt 策略
linux·人工智能·python·comfyui
星寂樱易李7 小时前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
abigriver7 小时前
打造 Linux 离线大模型级语音输入法:Whisper.cpp + 3090 显卡加速与 Rime 中英混输终极调优指南
linux·运维·whisper
wangqiaowq7 小时前
windows下nginx的安装
linux·服务器·前端
YYRAN_ZZU8 小时前
Petalinux新建自动脚本启动
linux
charlie1145141918 小时前
嵌入式Linux驱动开发pinctrl篇(1)——从寄存器到子系统:驱动演进之路
linux·运维·驱动开发
于小猿Sup8 小时前
VMware在Ubuntu22.04驱动Livox Mid360s
linux·c++·嵌入式硬件·自动驾驶
cen__y9 小时前
Linux12(Git01)
linux·运维·服务器·c语言·开发语言·git
不仙52010 小时前
VMware Workstation 26.0.0 在 Ubuntu 24.04 (内核 6.17.0) 上的安装与内核模块编译问题
linux·ubuntu·elasticsearch
随身数智备忘录10 小时前
什么是设备管理体系?设备管理体系包含哪些核心模块?
网络·数据库·人工智能