使用 netcat 和 grep 有条件地运行命令 [英] run a command conditionally with netcat and grep

查看:33
本文介绍了使用 netcat 和 grep 有条件地运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要 netcat 来监听传入的 HTTP 请求,并且根据请求,我需要运行一个脚本.

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

到目前为止我有这个;

netcat -lk 12345 | grep "Keep-Alive"

所以每次 netcat 收到一个包含keep-alive"的包时,我都需要触发一个脚本.

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

它需要在 crontab 中运行...

It needs to run in the crontab...

感谢您的帮助!

推荐答案

这个怎么样?

    #!/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

将echo"命令替换为您要执行的脚本.

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

这篇关于使用 netcat 和 grep 有条件地运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆