3

I have a 1 liner that will write the output of a telnet session to a file.

while sleep 5; do sleep 1 | telnet 11.22.33.44 443 | tee -a /var/tmp/test$(date +%Y%m%d).txt && date >> /var/tmp/test$(date +%Y%m%d).txt; done &

I do not want to see the output from the first command however would like to run this 1 liner and show the output:

awk '!/Trying/{count++}/Trying/{if (count == 4) print "Connected"; else if (count == !NR) print ""; else print "Failed"; count = 0}' test$(date +%Y%m%d).txt

Bonus if ! can get both in 1 line to show output as below:

while sleep 5; do sleep 1 | telnet 159.203.162.223 443 | tee -a /var/tmp/test$(date +%Y%m%d).txt && date >> /var/tmp/test$(date +%Y%m%d).txt; done & awk '!/Trying/{count++}/Trying/{if (count == 4) print "Connected"; else if (count == !NR) print ""; else print "Failed"; count = 0}' test$(date +%Y%m%d).txt
Connected
Connected
Connected
Connected
Connected
Connected
Connected
Connected
Connected
Connected
Anthony Fornito
  • 9,546
  • 1
  • 34
  • 124

1 Answers1

1

If possible, you might want to consider using netcat (nc) rather than trying to parse telnet output. For example:

$ while sleep 5; do nc -C 93.184.216.34 443 </dev/null && echo "Connected" | tee -a /var/tmp/test$(date +%Y%m%d).txt && date >> /var/tmp/test$(date +%Y%m%d).txt; done
Connected
Connected
Connected
Connected
^C

--

$ cat /var/tmp/test20180925.txt
Connected
Tue Sep 25 16:42:57 UTC 2018
Connected
Tue Sep 25 16:43:02 UTC 2018
Connected
Tue Sep 25 16:43:07 UTC 2018
Connected
Tue Sep 25 16:43:12 UTC 2018

Try this a little closer to what you are looking for

while sleep 5; do sleep 1 | nc -v 159.203.162.223 443 2>&1; echo $(date) >> /var/tmp/test$(date +%Y%m%d).txt; done >> /var/tmp/test$(date +%Y%m%d).txt & tail -f /var/tmp/test$(date +%Y%m%d).txt | grep 'Connected\|timed\|refused'
Anthony Fornito
  • 9,546
  • 1
  • 34
  • 124
guzzijason
  • 1,410
  • 8
  • 18