【问题标题】:using grep to get IP and Port out of a banner使用 grep 从横幅中获取 IP 和端口
【发布时间】:2015-05-20 21:35:37
【问题描述】:

我有一个这样格式的横幅列表:

Hostname: []
IP: xxx.xxx.xxx.xxx
Port: xx
HTTP/1.0 301 Moved Permanently
Location: /login.html
Content-Type: text/html
Device-Access-Level: 255
Content-Length: 3066
Cache-Control: max-age=7200, must-revalidate

为了获取ip,我使用了下面的grep语句:

grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}"

我必须在语句中添加什么才能获取端口? (同时仍然获得 IP。)。

感谢您的回答..!

【问题讨论】:

    标签: linux grep ip port


    【解决方案1】:

    为什么不使用awk

    awk '/IP:/ {ip=$2} /Port:/ {print ip,$2}' file
    

    当它找到 IP: 的行时,它将 IP 存储在变量 ip
    找到端口后,打印ip和端口号。


    例子

    cat file
    Hostname: []
    IP: 163.248.1.20
    Port: 843
    HTTP/1.0 301 Moved Permanently
    Location: /login.html
    Content-Type: text/html
    Device-Access-Level: 255
    Content-Length: 3066
    Cache-Control: max-age=7200, must-revalidat
    

    awk '/IP:/ {ip=$2} /Port:/ {print ip,$2}' file
    163.248.1.20 843
    

    【讨论】:

    • 我没有使用,因为我不知道如何使用。我会尝试使用它。!
    猜你喜欢
    • 1970-01-01
    • 2021-09-20
    • 2018-11-28
    • 2014-12-29
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    相关资源
    最近更新 更多