You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在单次Netcat连接中获取输出、处理数据并回传输入

How to Fetch Numbers, Sum Them, and Send Back in a Single Netcat Connection

Got it, let's solve this problem! Since the numbers change when you close the connection, we need to handle the entire flow—fetching the two numbers, calculating their sum, and sending the result—all within one persistent nc session. Here are a few solid, practical methods to pull this off:

Method 1: Use Bash Coprocesses (Clean, Bash-Only)

Bash has a built-in coproc feature that lets you run a command in the background and interact with its standard input/output via file descriptors. Perfect for bidirectional communication with nc:

# Start nc as a coprocess
coproc nc example.com 1234

# Read the two numbers from nc's output
read -u "${COPROC[0]}" num1
read -u "${COPROC[0]}" num2

# Calculate the sum
sum=$((num1 + num2))

# Send the sum back to nc's input
echo "$sum" >&"${COPROC[1]}"

# Wait for the nc process to finish
wait

How it works:

  • coproc launches nc in the background and sets up two file descriptors: ${COPROC[0]} (for reading from nc) and ${COPROC[1]} (for writing to nc).
  • We use read -u to fetch the two numbers from the coprocess's output.
  • After computing the sum, we echo it to the coprocess's input, which sends it directly to the server.
  • wait ensures we don't exit before nc finishes handling the response.

Method 2: Use a Named Pipe (Shell-Agnostic)

If you're not using Bash (or prefer a more portable approach), a named pipe (FIFO) lets you connect nc's input and output to a processing script:

# Create a temporary named pipe
mkfifo nc_pipe

# Run nc with input from the pipe, and pipe its output to our processing logic
nc example.com 1234 < nc_pipe | {
    read num1
    read num2
    echo $((num1 + num2)) > nc_pipe
}

# Clean up the pipe
rm nc_pipe

How it works:

  • The named pipe nc_pipe acts as a bridge: nc reads its input from the pipe, and our script writes the sum to the pipe after processing the numbers.
  • The subshell ({ ... }) reads the two numbers from nc's output, calculates the sum, and writes it back to the pipe—sending it straight to the server without closing the connection.

Method 3: Use Expect (For More Complex Interactions)

If you need to handle more nuanced prompts or regex matching, expect (a tool for automating interactive programs) is a great option. Note: You may need to install it first (e.g., apt install expect on Debian/Ubuntu):

#!/usr/bin/expect
# Spawn the nc connection
spawn nc example.com 1234

# Expect two lines of digits, capture them with regex
expect -re {(\d+)\n(\d+)\n}

# Calculate the sum
set sum [expr $expect_out(1,string) + $expect_out(2,string)]

# Send the sum back to the server
send "$sum\r"

# Optional: Keep the connection open to see the server's response
interact

Save this as sum_nc.exp, make it executable (chmod +x sum_nc.exp), and run it. It will automatically handle the entire interaction flow.


内容的提问来源于stack exchange,提问作者user873528

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:14:42