RAJJJA
RAJJJA

Reputation: 25

Using expect script inside started telnet session

I am using an expect script to interact with telnet session, which started earlier from bash. The script dont listen for symbols in command line. It just send commands after timeout runs out. I am sure, that i entered the right symbol to expect (), because the script works, when i run it standalone.

I have list of command (1 command per line) to send to mikrotik. My expect script puts every command to same line and then the script send them. I separated commands with ; and it worked, but just for a short command. So I decided to separate file with commands to smaller files. Then I open telnet session (in bash) and start expect script, which opens smaller files and send commands over and over. So I don´t need to restart same session over and over. Problem is, that expect script don´t send command, when it gets awaited symbol, but it sends commands after a timeout runs out.

This is part of bash script. It could be easier, to do whole script in Expect, but I didn't add loop and if condition yet and when i tried to create some while or if structure, I failed in it.

        (
        echo open "12.12.13.44"
        sleep 2
        echo "admin+t"
        sleep 2
        echo "admin"
        sleep 2
        /usr/bin/expect /home/toor/expect2.sh
        ) | telnet

Here is expected symbol

expect "> "

and here is the part of expect script, which puts all commands to same line (without sleep 1 command, script sends quit in the middle of reading from file)

set timeout 1
set fid [open /home/toor/file.txt]
set content [read $fid]
close $fid

set records [split $content "\r"]

foreach record $records {
   lassign $records \
         commands
   expect "> "
   send  "$commands\r"
}
sleep 1
expect "> "
send "quit\r"

I will be grateful for any advice regarding my problem with the expecting for symbol, as well as with writing to same line.

Upvotes: 1

Views: 795

Answers (1)

tripleee
tripleee

Reputation: 189397

You are piping the output of expect into telnet; there is no way for expect to receive information back from the telnet process in this scenario.

I would approach this as a chain of expect scripts instead - write one which starts the session, then hands over control to your existing script.

Upvotes: 2

Related Questions