Is there a way to send prompts to a netcat server Dynamically by scripting

145 Views Asked by At

I am given a problem by my professor to design a script for netcat prompts.. I need to calculate the sum of given two numbers and pass the sum as prompt for 1000 times. I developed a script using awk to get the requried numbers but don't know how to pass or echo that to nc server.

Welcome to the Ultimate Math Contest!!
Solve 1000 quick math problems to complete
You have 10 second for each problem.
What is the sum of 89187 + 26971:
You have 5 seconds to type in your stuff...
Exit...
116158

What i need is instead of typing the sum after the connection is closed it needs to dynamically type the sum for 1000 times. instead of exiting and then echoing the sum.

CODE

#!/bin/bash
extract_numbers(){
numbers=($(echo "$output" | awk 'NR==4 {gsub(/[^0-9]+/, " "); var1=$1; var2=$2; print var1, var2}'))
number1=${numbers[0]}
number2=${numbers[1]}
sum=$((number1 + number2))
echo $sum
}
output=$(echo "" | nc 65.2.97.115 8003)
echo "$output"
result=$(extract_numbers "$output")
echo $result

I tried echoing the sum and also wrapped it in a function but it doesn't seem to work

1

There are 1 best solutions below

0
Ivan On

As far as I understand you need something like this, send data via nc:

nc -q0 $address $port <<< "$data"

And receive something via nc:

result=$(nc -l -p $sport)
echo $result