【问题标题】:How can I return grep results one line at a time in order to use the parsed data in another script?如何一次返回一行 grep 结果以便在另一个脚本中使用解析的数据?
【发布时间】:2013-06-07 09:40:49
【问题描述】:

我有一个我在工作中使用的脚本,我想稍微扩展一下。我当前使用的脚本将 IP 地址作为 arg 并将 zip 文件推送到该设备上的特定目录。该设备是 Android 设备,我推送到的特定目录用于使用推送的新数据触发设备恢复。

我正在寻找一种方法来扫描网络,识别所有连接的 android 设备,然后将新固件推送到每个设备。由于推送部分已经很容易完成,我需要它来找到一种方法来扫描和识别每个连接设备的 IP。

我想出了:

nmap -sL 10.111.1.0/24 | grep android | grep -oE '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'

它将通过网络反弹,找到行中带有“android”的任何内容,然后解析并返回这些行的 IP 地址。问题是,我不知道如何一次获取一个 IP 地址并将它们从这里提供给脚本。有什么想法吗?

【问题讨论】:

    标签: linux bash shell scripting terminal


    【解决方案1】:

    你可以在任何时候使用 for 循环和子shell,只要你有这种模式,你想对管道的结果进行操作:

    for ip in $( nmap -sL 10.111.1.0/24 | grep android | grep -oE '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}' ); do
      echo "W00t! I found this ${ip}"
      /path/to/script "${ip}"
    done
    

    【讨论】:

      【解决方案2】:

      我会使用一个while循环并用一个集合解析你从grep中得到的任何东西。

      (nmap -sL 10.111.1.0/24 | grep android | grep -oE '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}' | \
      while read -a device; do
          echo ${device[0]}
          echo ${device[1]}
          ...
          /path/to/script ${device[2]} #the number that represents the ip address (I don t know wich one it will be depends what your grep gives)
       done)
      

      现在你可以获取IP地址并在脚本中推送它

      【讨论】:

        【解决方案3】:

        将其读入数组:

        read -a devices < <(nmap -sL 10.111.1.0/24 | grep android | grep -oE '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}')
        

        然后将其发送到脚本:

        /path/to/script "${devices[@]}"
        

        【讨论】:

          猜你喜欢
          • 2013-03-08
          • 1970-01-01
          • 1970-01-01
          • 2010-09-30
          • 1970-01-01
          • 1970-01-01
          • 2011-04-15
          • 1970-01-01
          • 2018-01-19
          相关资源
          最近更新 更多