【问题标题】:Create certificates with openssl from csv script.sh Ubuntu Server从 csv script.sh Ubuntu Server 使用 openssl 创建证书
【发布时间】:2022-10-24 20:42:16
【问题描述】:

我在 script.sh 中创建证书以生成包含用户所有数据的证书,但我不知道如何从 CSV 中获取该数据,我设法读取了数据,但我不知道如何把它放在命令中。

我的 CSV 包含(一千条记录):

Country, place, city, company, nameuser, email
EU,HOME,HOME1,DESKTOP,USERNAME,test@gmail.com
xx,xxx,xxxx,xxxx,xxxx,xxxx
xx,xxx,xxxx,xxxx,xxxx,xxxx
etc....
#!/bin/bash

openssl \
        req -x509 \
        -newkey rsa:4096 \
        -keyout user.key \
        -out user.crt \
        -days 365 \
        -nodes \
        -subj "/C=EU/ST=HOME/L=HOME1/O=Desktop/CN=USERNAME/emailAddress=test@gmail.com"

谢谢你!!!

【问题讨论】:

    标签: bash csv ubuntu openssl


    【解决方案1】:

    如果 CSV 数据真的那么简单,可以像这样用几行 bash 来完成(假设 CSV 数据在 data.csv 中):

    #!/bin/bash
    
    # Skip the first line, then read the comma-separated lines into individual variables
    tail -n +2 data.csv | while IFS=, read f1 f2 f3 f4 f5 f6; do
        echo openssl 
            req -x509 
            -newkey rsa:4096 
            -days 365 
            -keyout "$f5.key" 
            -out "$f5.crt" 
            -nodes 
            -subj "/C=$f1/ST=$f2/L=$f3/O=$f4/CN=$f5/emailAddress=$f6"
    done
    

    出于演示目的,我在它前面加上了echo,只需将其删除即可运行实际命令。

    输入如下...

    Country, place, city, company, nameuser, email
    EU,HOME,HOME1,DESKTOP,USERNAME,test@gmail.com
    x1,xx2,xxx3,xxx4,xxx5,xxx6
    y1,yy2,yyy3,yyy4,yyy5,yyy6
    

    ...脚本将生成这样的命令行(我假设您还需要唯一的*.crt*.key 文件名,顺便说一下,键入用户名):

    $ ./cert_gen.sh
    openssl req -x509 -newkey rsa:4096 -keyout USERNAME.key -out USERNAME.crt -days 365 -nodes -subj /C=EU/ST=HOME/L=HOME1/O=DESKTOP/CN=USERNAME/emailAddress=test@gmail.com
    openssl req -x509 -newkey rsa:4096 -keyout xxx5.key -out xxx5.crt -days 365 -nodes -subj /C=x1/ST=xx2/L=xxx3/O=xxx4/CN=xxx5/emailAddress=xxx6
    openssl req -x509 -newkey rsa:4096 -keyout yyy5.key -out yyy5.crt -days 365 -nodes -subj /C=y1/ST=yy2/L=yyy3/O=yyy4/CN=yyy5/emailAddress=yyy6
    

    【讨论】:

    • 朋友,你是最棒的,它按照你的指示工作,我把它添加到所有其他的,它是完美的,非常感谢你
    猜你喜欢
    • 1970-01-01
    • 2019-10-27
    • 2023-04-09
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 2010-09-20
    相关资源
    最近更新 更多