So I need to repeatedly try submitting a 4 digit number to a port on the local host, and then evaluate the response I get from the port if it contains a specific string, which I do with grep. I used something like echo {0000...9999} | nc localhost port. However, I cannot get this nice sequence expansion that I get from this command to work in a bash script. How could I go about writing a bash script for this problem?
Add a comment
|
2 Answers
You can use within a for-loop like following:
for i in {0001..9999}; do
grep -q 'specificString' <<<"$(nc localhost $i)" && \
echo "found on $i" || echo "not found on $i";
done
For shells that does not support brace expansions, or that does not preserve zero padding:
i=0
while [ "$i" -le 9999 ]; do
printf '%04d\n' "$i"
i=$(( i + 1 ))
done | nc ...other options... | grep ...etc...