I'm trying to automatically query a status via telnet (this isn't something I'm able to work around in this case). The idea is to grep for something and assign the result to a variable, to pass to a conditional statement later. The complication is the link to the target device may not be up, and therefore the script could hang indefinitely if not killed via a timeout.
output to what I'd expect, but does not handle the link being down:
output=$(telnet 126.96.36.199 1234 | grep "something")
This will spit out the expected output while handling the link being down:
timeout --signal=9 3 telnet 188.8.131.52 1234 | grep "something"
I can even direct the output to a file and the file will contain the output:
timeout --signal=9 3 telnet 184.108.40.206 1234 | grep "something" > /tmp/tmpfile.txt
Unfortunately rapid write/read of a file like this isn't an option because of how much it'll fill up log files.
But, when I try to combine everything, the variable doesn't set:
output=$(timeout --signal=9 3 telnet 220.127.116.11 1234 | grep "something")
Or, rather, it sets it to a blank value, because if I set it before running the above, the variable is blank afterwards.