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

相关推荐
郝亚军17 小时前
如何在Ubuntu和win10/11之间通过samba访问对方的文件
linux·服务器·ubuntu
曦云沐17 小时前
【避坑指南】Ubuntu更新报错“Repository is not signed”的快速修复
linux·ubuntu·docker
带土118 小时前
10. .out文件
linux
Exquisite.18 小时前
企业高性能web服务器(4)
运维·服务器·前端·网络·mysql
STCNXPARM18 小时前
Linux camera之V4L2子系统详解
android·linux·camera·v4l2架构
yueyuexiaokeai119 小时前
linux kernel常用函数整理
linux·c语言
qq_4112624220 小时前
用 ESP32-C3 直接连 Starlink 路由器/热点并完成配网
网络·智能路由器
郝亚军21 小时前
ubuntu-18.04.6-desktop-amd64安装步骤
linux·运维·ubuntu
Konwledging21 小时前
kernel-devel_kernel-headers_libmodules
linux
Web极客码21 小时前
CentOS 7.x如何快速升级到CentOS 7.9
linux·运维·centos