run a command conditionally with netcat and grep

时光怂恿深爱的人放手 提交于 2019-12-09 01:21:43

问题


I need netcat to listen on incomming HTTP requests, and depending on the request, I need to run a script.

So far I have this;

netcat -lk 12345 | grep "Keep-Alive"

So every time netcat recieves a package that contains a "keep-alive", I need to fire a script.

It needs to run in the crontab...

Thanks for your help!


回答1:


How about this?

    #!/bin/bash

    netcat -lk -p 12345 | while read line
    do
        match=$(echo $line | grep -c 'Keep-Alive')
        if [ $match -eq 1 ]; then
            echo "Here run whatever you want..."
        fi
    done

Replace the "echo" command with the script you want to execute.




回答2:


How about:

#!/bin/bash
netcat -lk -p 12345 | grep 'Keep-Alive' | while read unused; do
    echo "Here run whatever you want..."
done

Or

#!/bin/bash
netcat -lk -p 12345 | sed -n '/Keep-Alive/s/.*/found/p' | xargs -n 1 -I {} do_something
# replace do_something by your command.


来源:https://stackoverflow.com/questions/17990099/run-a-command-conditionally-with-netcat-and-grep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!