Home > Back-end >  How to redirect bash script output to another file?
How to redirect bash script output to another file?

Time:01-14

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

CodePudding user response:

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.

CodePudding user response:

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"
  • Related