![27f7ffb1bdaf78757e8cf6252934b410.png](https://i-blog.csdnimg.cn/blog_migrate/ce1fd8c4aea7fd399a9b728083490666.jpeg)
SSL Expire Scan
SSL 证书到期时间检测,使用 PHP 开发,需要安装 OpenSSL 扩展
有啥用呢?
提前提醒下,续签,防止忘记
使用方法
将项目 clone 到本地,然后在命令行输入
php main.php /path/to/ssl/directory/
示例(扫描 Nginx 的 SSL 目录下所有证书到期时间)
php main.php /usr/local/nginx/conf/ssl/
也可以对指定证书的到期时间进行扫描
php main.php /usr/local/nginx/conf/ssl/natfrp.org.crt
可以将以下内容加入到 /etc/bashrc 以方便随时使用:
alias sslscan='php /path/to/main.php'
源码
<?php /** * * SSL Expire Scan Tool by Akkariin * * 本代码使用 GPL v3 协议开源 * */$logo = << get_ssl_expire_time($dir));$abspath = realpath($dir);$expiretime = Array();if($abspath) {$list = scandir($dir);foreach($list as $file) {if($file !== "." && $file !== "..") {$ext = pathinfo("{$dir}{$file}");if($ext['extension'] == $extensionName) {$expiretime["{$file}"] = get_ssl_expire_time("{$dir}{$file}");}}}}return $expiretime;}// 程序开始echo $logo;echo "SSL expire time scan tool by Akkariin";if($argv[1] == "--help" || !isset($argv[1])) {exit("Usage: php {$argv[0]} [extension name]");}// 参数存到变量$scanpath = $argv[1] ?? "/usr/local/nginx/conf/ssl/";$scanexts = $argv[2] ?? "crt";// 扫描 SSL 证书$exps = scan_ssl_files($scanpath, $scanexts);if(count($exps) == 0) {echo "未扫描到 SSL 证书,请检查目录是否存在,以及目录下是否有证书文件";}// 获取最长的域名$max_length = 0;foreach($exps as $key => $value) {if(mb_strwidth($key) > $max_length) {$max_length = mb_strwidth($key);}}// 输出内容$result = "";$line_length = 0;$time_length = 0;// 遍历数组foreach($exps as $key => $value) {$expired = "正常";$exptime = $value - time();$dateime = $value !== 0 ? date("Y-m-d H:i:s