I am working on a script to parse a file containing a list of IP addresses and ports then do a bunch of test on the ports with netcat. The port argument can be a single port, a list of ports, or a range of ports (per netcat functionality).
In the case of the argument for netcat being a list or a range of ports, I get a line of result for each port on stderr (netcat sent everything on stderr). So my idea was to store the result in a variable and parse it with a loop (for each line), to get the status of the test on each port (succeeded, refused, timeout).
In order to store the result I redirect stderr to stdout and store it in a variable:
test=$(netcat -w 1 -zv 10.141.32.117 443-445 2>&1)
But that removed the newlines; I got this:
echo $test
netcat: connect to 10.141.32.117 port 443 (tcp) failed: Connection timed out netcat: connect to 10.141.32.117 port 444 (tcp) failed: Connection timed out netcat: connect to 10.141.32.117 port 445 (tcp) failed: Connection timed out
If I redirect stderr to a file, the result contain the lines. Is redirecting stderr to stdout removing the end of line?
For the moment I will redirect to a file.
Thank you for your explanations.
$test
:echo "$test" | ...
– Arnaud Valmary Aug 17 '23 at 15:48