certbot-auto报错怎么办?免费泛域名ssl证书自动续期报错。Let’s Encrypt自动续期脚本。

Let’s Encrypt颁发的通配符证书每次续期需要验证一下DNS的TXT记录,这个TXT记录的值是会发生变化的。所以续期时候需要更改下DNS的TXT记录。网上流传的都是php版本的脚本。还要写shell+php,挺麻烦的。这里我自己用go写了一个更新DNS记录的小程序

The error was: PluginError(‘An authentication script must be provided with --manual-auth-hook when using the manual plugin non-interactively.’,). Skipping.
All renewal attempts failed. The following certs could not be renewed:

申请步骤

# 1. 安装certbot
yum -y install certbot
# 2. 申请证书
certbot certonly --manual --preferred-challenges=dns -d *.berstar.cn
  1. 先配置两个环境变量,最好写在/etc/profile,或者.bashrc里面,然后执行source生效
# 阿里云的access key和secret
export CERTBOT_ALI_KEY=""
export CERTBOT_ALI_SECRET=""
  1. 下载certbot-alidns放到/usr/bin/就可以了。
# 用法
certbot-auto renew --cert-name shuaibo.wang --manual-auth-hook /usr/bin/certbot-alidns
  1. 重新加载下ssl证书
nginx -s reload

源码

package main
import (
    "fmt"
    "github.com/denverdino/aliyungo/dns"
    "os"
    "time"
)
var certbot_ali_key string
var certbot_ali_secret string
func init() {
    // 定义阿里云的访问key和secret
    certbot_ali_key = os.Getenv("CERTBOT_ALI_KEY")
    certbot_ali_secret = os.Getenv("CERTBOT_ALI_SECRET")
    // 判断阿里云的key和secret是否存在
    if certbot_ali_key == "" || certbot_ali_secret == "" {
        fmt.Println("请设置环境变量CERTBOT_ALI_KEY和CERTBOT_ALI_SECRET")
        os.Exit(1)
    }
}
func main() {
    client := dns.NewClient(certbot_ali_key, certbot_ali_secret)
    var args = new(dns.DescribeDomainRecordsArgs)
    args.DomainName = os.Getenv("CERTBOT_DOMAIN")
    args.RRKeyWord = "_acme-challenge"
    args.TypeKeyWord = "TXT"
    res, err := client.DescribeDomainRecords(args)
    if err == nil {
        records := res.DomainRecords.Record
        // 记录大于1执行更新,小于1执行创建
        if len(records) > 0 {
            for i := 0; i < len(records); i++ {
                var update_args = new(dns.UpdateDomainRecordArgs)
                update_args.RecordId = records[i].RecordId
                update_args.RR = "_acme-challenge"
                update_args.Value = os.Getenv("CERTBOT_VALIDATION")
                update_args.Type = "TXT"
                res, err := client.UpdateDomainRecord(update_args)
                if err == nil {
                    fmt.Println("更新成功:", res.RecordId)
                    time.Sleep(time.Duration(20) * time.Second)
                } else {
                    fmt.Println("更新失败:", err.Error())
                    os.Exit(2)
                }
            }
        } else {
            // 执行创建操作
            var add_args = new(dns.AddDomainRecordArgs)
            add_args.DomainName = os.Getenv("CERTBOT_VALIDATION")
            add_args.RR="_acme-challenge"
            add_args.Value=os.Getenv("CERTBOT_VALIDATION")
            add_args.Type="TXT"
            res,err:=client.AddDomainRecord(add_args)
            if err == nil {
                fmt.Println("创建成功:", res.RecordId)
                time.Sleep(time.Duration(20) * time.Second)
            } else {
                fmt.Println("创建失败:", err.Error())
                os.Exit(2)
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值