编写一个扫描nginx证书是否过期的shell脚本
时间: 2023-08-19 17:54:47 浏览: 44
当然可以,以下是一个简单的 shell 脚本,用于扫描 nginx 证书是否过期:
```shell
#!/bin/bash
# 指定需要扫描的证书路径
cert_path="/etc/nginx/ssl"
# 获取当前日期
current_date=$(date +%s)
# 扫描证书目录下的所有文件
for cert_file in $(find $cert_path -type f -name "*.crt"); do
# 提取证书过期日期
expiration_date=$(openssl x509 -enddate -noout -in $cert_file | cut -d "=" -f 2)
# 转换过期日期为时间戳
expiration_timestamp=$(date -d "$expiration_date" +%s)
# 计算剩余天数
remaining_days=$((($expiration_timestamp - $current_date) / 86400))
# 打印证书路径和剩余天数
echo "证书路径: $cert_file"
echo "剩余天数: $remaining_days"
echo
done
```
使用方法:
1. 将上述脚本保存为 `check_cert.sh`
2. 使用终端进入脚本所在目录
3. 运行 `chmod +x check_cert.sh` 给予脚本执行权限
4. 运行 `./check_cert.sh` 执行脚本
脚本会扫描指定目录下的所有 `.crt` 文件,并打印证书路径和剩余天数。你可以根据实际情况修改 `cert_path` 变量来指定需要扫描的证书路径。