0

Good afternoon! Please tell me. The idea of the script is that it enters the switch, displays the values and redirects them to another file. Wrote like this:

(
echo "$username"
sleep 2
echo "$Pass"
sleep 2
echo "show ..." >> /home/...
sleep 2
echo "logout"
) | telnet 172.20...

But here, again, the problem is that it writes a command to the file, that is, echo simply displays it in text, and as a result, in the file "show .." Is it possible that it writes to the file exactly the results of the command on the switch, for example, the status of the ports

implemented via screen -L

It works, writes information to a file, but it works very unstable, so let's say, you run the script

show ports.

And he writes the result of this command (state) 2 or 3 times to the file. That is, it simply duplicates the same information many times. Although the script was run once and executed once

2
  • 1
    You should probably use an Expect script instead of piping to telnet. Commented Jan 13, 2023 at 2:11
  • While the telnet man-page does not explicitly say so, from the way it describes the input protocol, I doubt that telnet is processing standard input. If you want to feed stdin to telnet, you could write a wrapper in i.e. Perl or Ruby (both have a telnet module). Commented Jan 13, 2023 at 7:59

2 Answers 2

1

telnet is designed for interactive usage. Use netcat, ncat, nc, socat or any other tool of this family. I think, socat is the best for this case because of its script support.

Sign up to request clarification or add additional context in comments.

Comments

0

Using expect you can create a script similar to this one:

#!/usr/bin/expect -f

set timeout 5
set username "username"
set password "password"   # you should not hardcode password 

spawn telnet 172.20... 23

expect "login: "
send "$username\r"

expect "password: "
send "$password\r"

expect "$ "  # prompt
send "show ...\r"

expect "$ "  # prompt
send "logout\r"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.