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 # 邮箱授权码

相关推荐
大哥您好6 小时前
QEMU 11.1.2 多架构编译指南(x86_64 / aarch64 / riscv64)
linux·服务器
道满科技7 小时前
Function Calling 参数总出错?只修参数就行
linux·windows·python
Gauss松鼠会7 小时前
高斯数据库(openGauss)的物理存储结构
运维·服务器·数据库·sql·opengauss·经验总结
Android系统攻城狮7 小时前
Linux Gstreamer深度解析之gst_audio_ring_buffer_prepare_read调用流程与实战(七十四)
linux·运维·服务器·gstreamer音视频·音视频进阶
杨云龙UP8 小时前
PostgreSQL 18.4 数据库整库迁移实战:pg_dump 备份与 pg_restore 恢复完整操作手册
运维·服务器·数据库·postgresql·pg_dump·pg_restore
我科绝伦(Huanhuan Zhou)8 小时前
Linux 通用压力测试脚本合集:磁盘 + 网络
linux·网络·压力测试
聚美智数9 小时前
网站HTTP状态查询-SEO优化
网络·网络协议·http
treesforest9 小时前
数据包是如何找到回家路的?
网络·智能路由器·nat·ip归属地查询
土星云SaturnCloud9 小时前
土星云智慧服务区方案:AI视觉+边缘计算+视频存管一体化的全场景智能化落地解析
服务器·人工智能·ai·边缘计算
Soneswy10 小时前
Linux系统篇--重定向与文件操作命令
linux