I am trying to connect a device from telnet with expect command in bash script. What I would like to do is something as follows:
#!/usr/bin/expect -f
telnet_setup()
{ sleep 10
expect <<EOF
spawn telnet $1
expect "Password:"
send "mypass\n"
expect "#"
send "my command to be run\n"
expect "Last number to be captured:"
#Here the whole sentence to be captured something like:
#Last number to be captured: 213124
set mynumber expect_out(buffer,0) | awk '{print $6}'
send "my next command with number $mynumber\"
expect "Correct number $mynumber is achieved!"
send "exit\n"
EOF
}
To use the function, I use as follows:
telnet_setup 1.1.1.1 > file.txt
My goal is to use this function with $mynumber to be used in the next commands. Can you please suggest how to provide this?
You're having some trouble separating expect code from shell code. To capture the number, expect a regular expression with capturing parentheses
send "my command to be run\r"
expect -re {Last number to be captured:\s+(\d+)}
set mynumber $expect_out(1,string)