0

I have an application which accepts commands over telnet and performs action and then return the result.

We wrote this following line in a script to automate the action:

{ echo -e '\02~NH02'; sleep 5; }  | telnet $ipaddr $port

This command works most of the time, but sometimes it fails, I mean the receiver doesn't receive the command will not perform the action..

So, the idea is to read the return value sent after the command is executed.. If the receiver receives the command it sends "OK", If no command is received, then no response.

Is there any way to read the response in a shell script..

Thanks for your time

md.jamal
  • 4,067
  • 8
  • 45
  • 108
  • You can check the return status of a command by simply using an `if`-clause, see: `http://wiki.bash-hackers.org/syntax/ccmd/if_clause` and `http://mywiki.wooledge.org/BashGuide/TestsAndConditionals#Conditional_Blocks` – Rany Albeg Wein Apr 06 '18 at 02:33
  • I need to check whether the receiver's response.. One way is to redirect the output to a file and then check whether the string exist or not with grep.. Is there any other better way.. – md.jamal Apr 06 '18 at 02:36
  • 1
    Can you edit your question to include a success and fail response? – Rany Albeg Wein Apr 06 '18 at 02:39
  • I'm not sure If I understand you correctly, but my guess is that you want something like `while read -r line; do printf '%s\n' "$line"; done < <(telnet "$ipaddr" "$port")` – Rany Albeg Wein Apr 06 '18 at 02:55
  • No.. First I have to send the request "NH02" to the remote server, wait few seconds and then read the response and verify whether I have received the response or not – md.jamal Apr 06 '18 at 02:57
  • 1
    Process Substitution `<(...)` or `>(command ...)` is replaced by a temporary filename. Writing or reading that file causes bytes to get piped to the command inside. See `http://mywiki.wooledge.org/ProcessSubstitution` `http://mywiki.wooledge.org/BashFAQ/024` – Rany Albeg Wein Apr 06 '18 at 03:01
  • Can you use `expect`? Here's a similar question where the user sends a string and waits for a response, with a timeout: https://stackoverflow.com/questions/12315151/how-to-handle-tcl-expect-output – Mark Plotnick Apr 06 '18 at 04:12
  • [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Apr 06 '18 at 07:54

2 Answers2

1

Try using nc, it gives you simple and better way inside script.

#echo -e "\n" | nc 54.160.138.189 80

#echo $?   #### IF the output is 0 then connection is success , else it's failed to connect. 
SAB
  • 307
  • 4
  • 9
0

Add this to the end to check for the word ok.

   | grep ok

This logs the result to the file logfile and checks it for exactly ok.

{ echo -e '\02~NH02'; sleep 5; }  | telnet $ipaddr $port | (set -o pipefail && tee -a logfile | grep ok ;)
mncl
  • 161
  • 1
  • 5