【问题标题】:grepping the output of a curl command within bash scriptgrepping bash 脚本中 curl 命令的输出
【发布时间】:2020-04-30 08:13:03
【问题描述】:

我目前正在尝试编写一个脚本,当我输入漏洞名称时,它将返回给我来自tenable的 CVSS3 分数。

目前我的计划是:

  1. 卷曲页面
  2. Grep 我想要的内容
  3. 输出 grep 的 CVSS3 分数

运行 myscript 但是 grep 抛出以下错误:

~/Documents/Tools/Scripts ❯ ./CVSS3-Grabber.sh                             
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                Dload  Upload   Total   Spent    Left  Speed
  100 30964    0 30964    0     0  28355      0 --:--:--  0:00:01 --:--:-- 28355
  grep: unrecognized option '-->Nessus<!--'
  Usage: grep [OPTION]... PATTERNS [FILE]...
  Try 'grep --help' for more information.

这让我很困惑,因为当我在命令行中运行它时,我将内容卷曲到 sample.txt,然后使用完全相同的 grep 语法:

grep $pagetext -e CVSS:3.0/E:./RL:./RC:.

它返回给我我需要的内容,但是当我通过下面的脚本运行它时......

#! /bin/bash
pagetext=$(curl https://www.tenable.com/plugins/nessus/64784)
cvss3_temporal=$(grep $pagetext -e CVSS:3.0/E:./RL:./RC:.)
echo $cvss3_temporal

我收到上述错误!

我相信这是因为“--”导致 grep 认为文件中的文本是 grep 不知道的指令,因此出现错误。我已经尝试将 curl 的输出复制到一个文本文件中,然后从 curl 中获取它,而不是直接从 curl 中提取,但仍然没有乐趣。有谁知道在阅读文本时让 grep 忽略“--”或任何标志的方法?或者,如果我可以配置 curl 使其只带回文本而不带符号?

提前致谢!

【问题讨论】:

    标签: bash curl grep


    【解决方案1】:

    Grep 过滤给定的文件或标准输入(如果没有给出)。在 bash 中,您可以使用&lt;&lt;&lt; here-word 语法将变量内容发送到 grep 的输入:

    grep -e 'CVSS:3.0/E:./RL:./RC:.' <<< "$pagetext"
    

    或者,如果您在其他任何地方都不需要该页面,您可以将输出从 curl 直接传送到 grep

    curl https://www.tenable.com/plugins/nessus/64784 | grep -e 'CVSS:3.0/E:./RL:./RC:.'
    

    【讨论】:

      【解决方案2】:

      您不需要将curl 响应存储在变量中,只需像这样在curl 之后管道grep

      cvss3_temporal=$(curl -s https://www.tenable.com/plugins/nessus/64784 |
      grep -F 'CVSS:3.0/E:./RL:./RC:.')
      

      注意在curl 中使用-s 来抑制进度,在grep 中使用-F 以确保您搜索的是固定字符串。

      【讨论】:

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