2

I'm trying to code a script where I can connect via telnet to another server and execute certain operations, but I'm having problems with the result of this process.

The objective is to connect to a server, execute one command, and save the result into a .txt file. It is desirable that the parameters or input are in an input file .txt, which is read by the script and executed. (I have not done that part yet.)

Here's what I have so far.

#!/bin/bash

{
sleep 5
echo "user"
sleep 3
echo "password"
sleep 3
echo "clear"
echo "COMMANDS" >> /ROUTE/output_file.txt
echo exit
} | telnet 172.22.89.133

In this case I'm getting the output file filled with the command, but obviously I need the result of that command. I know it is a little silly, but I've been trying in many ways and I am still getting the same result.

How can I get this done?

  • You should use Expect to control an interactive program like telnet. – Barmar Apr 7 '16 at 19:46
  • maybe use nc instead of telnet – Archemar Apr 8 '16 at 11:45
1

Move the >> /ROUTE/output_file.txt from where it is now to the telnet line, so that line becomes } | telnet 172.22.89.133 >> /ROUTE/output_file.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.