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

相关推荐
AI 嗯啦12 分钟前
linux的用户操作(详细介绍)
linux·运维·服务器
AOwhisky26 分钟前
云计算一阶段Ⅱ——12. SELinux 加固 Linux 安全
linux·安全·云计算
Ronin30537 分钟前
【Linux系统】进程间通信:命名管道
linux·服务器·命名管道
东东今天敲代码了吗1 小时前
Ubuntu20.04 离线安装 FFmpeg 静态编译包
linux·运维·服务器·ubuntu·ffmpeg
The god of big data1 小时前
最新教程 | CentOS 7 下 MySQL 8 离线部署完整手册(含自动部署脚本)
linux·mysql·centos
青草地溪水旁1 小时前
Unix/Linux 系统编程中用于管理信号处理行为的核心概念或模型
linux·信号·进程间通信
Crazy learner2 小时前
深入理解 C 语言中的拷贝函数
服务器·c语言·网络
tjjingpan2 小时前
HCIP-Datacom Core Technology V1.0_6 IS-IS原理和配置
linux·运维·服务器
努力努力再努力wz2 小时前
【Linux内核系列】:信号(上)
java·linux·运维·服务器·c语言·开发语言·c++
景彡先生2 小时前
密码学分组模式详解:从ECB到GCM的进化之路
网络·密码学