【问题标题】:How to write an output of a grep from an openssl command?如何从 openssl 命令编写 grep 的输出?
【发布时间】:2017-11-06 19:07:53
【问题描述】:

假设您在一个文本文件中有一千个 IP 地址的列表 - 每行一个。我希望能够使用 openssl s_client 命令从每个 IP 地址中获取所有可能的异常。到目前为止,异常是证书过期、自签名证书和颁发者 CN 包括 emailAddress=root@localhost.localdomain。

总的来说,如果有的话,我希望能够获得每个 IP 地址的简明错误消息。我当前的 bash 脚本如下所示:

for ip in `awk '{print $1}' < general_certs.txt`; do 
# Print IP address currently checking
echo -e $ip;
    if timeout 30 openssl s_client -connect $ip:443| grep -i 'error' ; then
# Write error information and IP address to a file
        echo `openssl s_client -connect $ip:443| grep -i 'error'` $ip >> general_errors;
    else
# Write noerror information and IP address to another file
        echo "noerror" $ip >> general_noerror;
    fi;
done

我对代码的第一个问题是它没有优化,我怀疑它是否返回准确的结果。上述脚本的最终目标是识别所有包含 IP 的不受信任的证书。

我在上面的代码中遇到的第二个问题是我无法先回显 $ip,因为它会被消息本身截断。所以,我最终在错误消息之后写出了 $ip 。

如果我的问题有更真实的解决方案,就不用用openssl了。

【问题讨论】:

    标签: bash grep echo


    【解决方案1】:

    您可以尝试通过将进程置于后台来一次性运行它们:

    #!/bin/bash
    max=100
    counter=0
    for ip in `awk '{print $1}' < general_certs.txt`; do 
        (( counter++ ))
        (
            results=$( timeout 30 openssl s_client -connect $ip:443 2> /dev/null )
            if [ "$results" = "" ]; then
                echo "$ip noresponse"
            else 
                echo -n "$ip "
                echo "$results" | grep -i 'error' || echo "noerror"
            fi
        )  >> general_errors &
        if [ $counter -eq $max ]; then
            wait
            counter=0
        fi
    done
    wait
    

    这是输入:

    $ cat general_certs.txt 
    stackoverflow.com
    redhat.com
    google.com
    8.8.8.8
    vt.edu
    bls.gov
    8.8.4.4
    

    这是输出:

    $ cat general_errors
    8.8.4.4 noerror
    stackoverflow.com noerror
    google.com noerror
    vt.edu noerror
    bls.gov noerror
    redhat.com noerror
    8.8.8.8 noresponse
    

    如果你有一些失败的,我可以测试它们。

    【讨论】:

    • 我想选择您的正确答案,但如果我的 IP 列表过大,您的方法可能会冻结操作系统。
    • 只需在循环中添加一个计数器。当计数器达到最大值时,致电wait 并重置计数器。
    • IP 地址仍被截断。您是否尝试将其与 8.8.8.8 或 8.8.4.4 等 IP 地址一起使用?
    • 只要把它们放进去。输出显示“noerror”,即使根本没有输出。 (需要添加代码来检查。)但它没有截断输出中的地址。
    【解决方案2】:

    如果您想获取哪个 ip 的证书不受信任,您可以尝试使用 curl,即使这不是最佳选择。

    类似:

    for ip in `awk '{print $1}' < general_certs.txt`; do 
      echo -e $ip                                                                                                                                                     
    
      curl https://${ip}:443 &> /dev/null                                                                                                                             
    
      if [ $? == 51 ]; then                                                             
        echo "upsis, https://${ip}:443 has a untrusted certificate" >> general_err
      else                                                                              
        echo "yai, https://${ip}:443 doesn't have a untrusted certificate" >> general_noerr
      fi
    done
    

    请注意,这里您只检查不受信任的证书(curl 中的错误 51),但该命令可能会发送任何其他错误,它会转到 general_noerror

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多