Question

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!

Was it helpful?

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.

OTHER TIPS

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.

Depending on what you have on the client side, it might sit there waiting for netcat/the server to respond.

I did a similar thing to the above but used

while true do
   netcat -l 1234 < 404file.txt

Where 404file.txt has HTTP/1.1 404 FILE NOT FOUND

This disconnects the client and since netcat has no 'k' it terminates and restarts because of the while true, all ready to receive and send the 404 again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top