0

Can anyone please help me create a bash command to automate a telnet session?

  1. telnet into session and tee the output.
  2. input E.
  3. input B.
  4. input enter key in a loop until "End of file" is tee'd into file.
  5. input x
  6. input x
  7. input escape command "^]"
  8. input quit to quit the telnet session

Here is what i've come up with so far but its not working.

{ echo "E"; sleep 0.1; echo "B"; sleep 0.1; } | ( 
    telnet 10.10.10.4 | tee text.txt ) & 
    while sleep 0.1; do
        if grep -q "End of file" text.txt; then break; fi
    done && echo "x" && echo "x" && echo "^]" && echo "quit"

Here is the error:

telnet: Unable to connect to remote host: Connection refused
Trying 10.10.10.4...
[1]  + broken pipe  { echo "E"; sleep 0.2; echo "B"; sleep 0.2; } | 
       done         ( telnet 10.10.10.4 | tee text.txt; )
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
tefloy
  • 1
  • Why running in background with `&` ? – Gilles Quénot Feb 22 '23 at 19:07
  • 2
    Look at the `expect` utility. It will take a little time to get working, but if you need to do this sort of thing regularly, it will be worth the time spent to learn it. Good luck. – shellter Feb 22 '23 at 19:41
  • 3
    "*telnet: Unable to connect to remote host: Connection refused*" this has nothing to do with your script or commands. Fix this first. You have something blocking your connection, most probably a firewall in between or on the server itself. On another note, look into ssh. Telnet is not encrypted and very poor form a security standpoint. It should be turned off on any hardened server. – Nic3500 Feb 22 '23 at 19:59
  • Sleep can only handle integers, btw, and 1 second is the smallest unit. – tink Feb 22 '23 at 20:10
  • @tink, GNU `sleep` (the standard on Linux) can handle non-integer seconds. `sleep 0.1` sleeps for (roughly) a 10th of a second on Linux. – pjh Feb 22 '23 at 20:26
  • 1
    Ooops ... brain-fart. Sorry - culling my comment ;) @pjh – tink Feb 22 '23 at 23:53

0 Answers0