run a command conditionally with netcat and grep

bash, command, grep, linux, netcat

Solution

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.

Problem

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!

Original source