Search code examples
bashshellsshtelnetzebra-printers

How to adjust this bash script to run telnet commands successfully while being in SSH?


I am attempting to write a bash script that will do the following work flow:

  1. Telnet into networked device via IP address on port 9100 telnet x.x.x.x 9100
  2. Run SGD command ! U1 getvar \"internal_wired.ip.timeout.value\".
  3. Expect output value of "10".

Here is the bash script I've written so far:

#!/bin/bash

IP=(x.x.x.x) 


    for i in ${IP}
    do
      echo " "
      echo "Welcome! This script will check the timeout value of this networked device."
      echo "The expected output should be `"10`". Let's get started!!"
      echo " "
      sleep 4
      echo "5....."
      sleep 1
      echo "4...."
      sleep 1
      echo "3..."
      sleep 1
      echo "2.."
      sleep 1
      echo "1."
      sleep 1
      echo " "
      telnet ${i} 9100 << END_SSH
        sleep 5
        getvar \"internal_wired.ip.timeout.value\"
        sleep 5
    END_SSH
    done

When I run this script via bash mycode.sh, I get the following output in Terminal.app:

$ bash mycode.sh 

Welcome! This script will check the timeout value of this networked device.
The expected output should be "10". Let's get started!!

5.....
4....
3...
2..
1.

Trying x.x.x.x...
Connected to x.x.x.x.
Escape character is '^]'.
Connection closed by foreign host.
[user@server ~]$ 

x.x.x.x is an IP placeholder just to add.

In theory, after the Escape character is '^]'. line, the script should have ran the ! U1 getvar "internal_wired.ip.timeout.value\" command.

Also, we should have had an expected output of "10".

When I first wrote this script, I initially did not have the END_SSH command in it. A colleague introduced that to me and said to wrap the telnet commands in the END_SSH because of how Terminal technically jumps out of SSH when you are in telnet. I've tried utilizing END_SSH, but am not successful.

How do I get the telnet command to run successfully and get the expected output value?


Solution

  • You misunderstand what "END_SSH" is. It's not a "command" - it's what's called "Here-document" in bash.

    Essentially the text between the <<END_SSH and the END_SSH is a "here-document" that is piped into stdin of telnet ${i} 9100. So, the sleep 5 commands are never actually executed and the input reaches EOF before the connection is even established.

    I don't know what exactly you are trying to accomplish, but I would guess that the following will work better. Oh, and what's with that weird IP=(x.x.x.x) declaration? Is that supposed to be an array?

    #!/bin/bash
    
    declare -a IP=(1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4)
    
    for i in "${IP[@]}"; do
        echo " "
        echo "Welcome! This script will check the timeout value of this networked device."
        echo "The expected output should be \"10\". Let's get started!!"
        sleep 4
        for j in {5..1}; do
            echo $j
            sleep 1
        done
    
        { sleep 5; echo -n $'! U1 getvar "internal_wired.ip_timeout.value"\n'; sleep 5; } | telnet ${i} 9100
    done