Is a conditional response possible with netcat

浪尽此生 提交于 2019-12-12 00:40:07

问题


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

Example of what I want.

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()

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]
  }
}

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



来源:https://stackoverflow.com/questions/41743304/is-a-conditional-response-possible-with-netcat

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