netcat是否可能有条件响应 [英] Is a conditional response possible with netcat

查看:86
本文介绍了netcat是否可能有条件响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行netcat( netcat-openbsd 1.105-7ubuntu1 )并模拟聊天序列.我想要netcat自动响应.

I want to run netcat (netcat-openbsd 1.105-7ubuntu1) and simulate a chat sequence. I want the netcat response automatically.

我想要的例子.

NETCAT: nc -l 8080

CLIENT: nc localhost 8080

CLIENT: hello

NETCAT: (if statment) 
        if hello
           do hello friend

        if bye
           do bye friend
           send a FIN tcp 

        default
           date()

我复制了这个问题的代码(在@wooghie的答覆中):

I copied the code of this question (in the asnwer by @wooghie): run a command conditionally with netcat and grep ...but the message wasn't sent to the client. Netcat was on listen mode.

#!/bin/bash

netcat -l 8080 | while read line
do
    match=$(echo $line | grep -c 'Hello')
    if [ $match -eq 1 ]; then
        printf "Hello friend\r\n\r\n"
    fi
done

推荐答案

我认为您要期望(1).类似于以下内容:

I think you want expect(1). Something along the lines of:

#!/usr/bin/env expect
spawn nc localhost 8080
expect {
  hello {
     send "hello dude"
  } bye {
     close
  } -re .* {
     send [date]
  }
}

请注意,expect实际上是Tcl,它本身具有非常强大的功能.
未经测试.YMMV.

Note that expect is really Tcl, which is very powerful in its own right.
Not tested. YMMV.

这篇关于netcat是否可能有条件响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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