task1 脚本

image
以下是为每个任务编写的脚本和配置步骤,基于最小化安装的 CentOS 7.9 系统:


任务 6:脚本 /root/6.sh

#!/bin/bash
cpu_cores=$(grep -c 'processor' /proc/cpuinfo)
mem_mb=$(free -m | awk '/Mem:/ {print $2}')
disk_gb=$(df -BG / | awk 'NR==2 {gsub("G","",$2); print $2}')

echo "本机CPU核心数是$cpu_cores,本机内存容量是$mem_mb MB,本机磁盘容量是$disk_gb GB。"

权限设置:

chmod +x /root/6.sh

任务 7:脚本 /root/7.sh

#!/bin/bash
internal_ip=$(ip route get 1 | awk '{print $NF;exit}')
external_ip=$(curl -s icanhazip.com)

echo "本机内网IP地址是:$internal_ip,本机访问互联网的出口IP地址是:$external_ip。"

权限设置:

chmod +x /root/7.sh

任务 8:安装 Docker 并运行 Nginx 容器

# 安装依赖
yum install -y yum-utils device-mapper-persistent-data lvm2

# 添加 Docker 仓库
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

# 安装 Docker
yum install -y docker-ce docker-ce-cli containerd.io

# 启动 Docker 服务
systemctl start docker
systemctl enable docker

# 创建自定义 HTML 文件
echo 'Do difficult but meaningful things and keep doing them !!!' > /root/index.html

# 运行 Nginx 容器
docker run -d --name mynginx -p 8080:80 -v /root/index.html:/usr/share/nginx/html/index.html nginx

验证:

curl http://127.0.0.1:8080  # 应返回自定义文本

任务 9:脚本 /root/9.sh

#!/bin/bash
network="203.107.6"

for ip in {2..254..2}; do
    if ping -c 1 -W 1 "$network.$ip" &> /dev/null; then
        echo "$network.$ip"
    fi
done

权限设置:

chmod +x /root/9.sh

任务 10:开机自动创建 /root/boottime.txt

# 创建 systemd 服务
cat > /etc/systemd/system/boottime.service <<EOF
[Unit]
Description=Record boot time

[Service]
ExecStart=/bin/bash -c 'date +"%%Y-%%m-%%d %%H:%%M:%%S" > /root/boottime.txt'

[Install]
WantedBy=multi-user.target
EOF

# 启用服务
systemctl enable boottime.service

验证:
重启后检查 /root/boottime.txt 是否生成。


任务 11:配置 iptables 防火墙

# 停止并禁用 firewalld
systemctl stop firewalld
systemctl disable firewalld

# 安装 iptables
yum install -y iptables-services

# 启用 iptables
systemctl start iptables
systemctl enable iptables

# 开放 8080 端口
iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
service iptables save

任务 12:日志分析脚本 /root/12.sh

假设需求是统计 /root/secure 中的登录失败记录(根据常见场景)

#!/bin/bash
log_file="/root/secure"

# 统计登录失败的IP和次数
grep "Failed password" "$log_file" | awk '{print $11}' | sort | uniq -c | sort -nr | while read count ip; do
    printf "%-15s  %-4s\n" "$ip" "$count"
done

权限设置:

chmod +x /root/12.sh

使用说明:

  1. 解压日志:unzip /root/secure.zip -d /root
  2. 运行脚本:/root/12.sh
  3. 输出格式示例:
    192.168.1.10   12
    10.0.0.5       5
    

验证所有任务

  1. 执行脚本 /root/6.sh/root/7.sh/root/9.sh 检查输出。
  2. 访问 http://127.0.0.1:8080 验证 Nginx 容器。
  3. 重启后检查 /root/boottime.txt
  4. 运行 iptables -L -n 确认 8080 端口开放。
  5. 解压日志后运行 /root/12.sh 检查分析结果。

注意:

  • 任务 12 的日志分析逻辑可根据实际需求调整(如修改 grep 关键词)。
  • Docker 安装需联网,确保 curl icanhazip.com 能获取公网 IP。
  • 所有脚本需赋予执行权限(chmod +x /root/*.sh)。
posted on 2025-08-06 10:09  爱尔奎特‘殷’  阅读(9)  评论(0)    收藏  举报