programing in expect. What is similar or usage to goto - expect

Here's the code that I have, I explain how I want it to work at the bottom.
set 11 "10.0.0.101"
set 12 "10.0.0.12"
set timeout 20
spawn telnet $11
expect {
"Unable to connect to remote host:" {exit}
"login:"
}
send "root\r"
expect "Password:"
send "root\r"
expect "#"
send "shutdown -r now\r"
expect "#"
send "exit\r"
expect "Connection closed by foreign host."
set timeout 20
spawn telnet $12
expect "login:"
send "root\r"
expect "Password:"
send "root\r"
expect "#"
send "shutdown -r now\r"
expect "#"
send "exit\r"
expect "Connection closed by foreign host."
exit
As you can see, I am using this to reboot devices once a week with a crontab, but if the device is not responding or offline, the script just stops at {exit}
So I want to use like goto command or similar to run the next spawn command in the list.
Is there any easy way to do this if there is no goto command ?

Maybe you can include all your command exchange for the first server within the block of code executed after the login prompt is successfully matched?
Something like the following:
set 11 "10.0.0.101"
set 12 "10.0.0.12"
set timeout 20
spawn telnet $11
expect {
"Unable to connect to remote host:" { }
"login:" {
send "root\r"
expect "Password:"
send "root\r"
expect "#"
send "shutdown -r now\r"
expect "#"
send "exit\r"
expect "Connection closed by foreign host."
}
}
spawn telnet $12
[...]
If after attempting to connect to $11 you get an "Unable to connect...", it will execute the empty block and move on. If it matches the "login:", it will execute all commands.
You will need to check whether your telnet closes successfully after the "Unable to connect" message (I would think it does), so that you don't end up with multiple spawned sessions.

Related

Why '\n'('\r') does not work in my expect script

I want to input y+enter to reply the question while executing copy tftp:something.
The script will send y, but \n does not work. It will stay (y/n)y and keep there without exiting or doing something else. I have tried \r, and the result was the same. Does anyone know the reason?
#!bin/bash
expect -c "
set timeout -1
spawn telnet x.x.x.x
expect \"username\"
send \"user\n\"
expect \"password\"
send \"pw\n\"
expect \"model\"
send \"copy tftp:something\n\"
expect \"(y/n)\"
send \"y\n\"
expect eof
"
exit 0
I prefer using \r to "hit enter".
Second, your entire bash script is expect, so remove the outer bash layer.
#!/usr/bin/expect -f
set timeout -1
spawn telnet x.x.x.x
expect "username"
send "user\r"
expect "password"
send "pw\r"
expect "model"
send "copy tftp:something\r"
expect "(y/n)"
send "y\r"
expect eof
If you have more logic in the bash part, to avoid quoting hell use a heredoc:
#!/bin/bash
expect <<'END_EXPECT'
set timeout -1
spawn telnet x.x.x.x
expect "username"
send "user\r"
expect "password"
send "pw\r"
expect "model"
send "copy tftp:something\r"
expect "(y/n)"
send "y\r"
expect eof
END_EXPECT
exit 0

How to save output expect in bash

I am trying to make a script to save a output of the command show version in cisco.
I need made a connection to one server for that the ssh connection, and then i have connection with the device.
In the file out.txt, I have the output of the first connection, the ssh connection, but i dont know how to save the output of the show version
#!/usr/bin/expect -f
#!/bin/sh
spawn ssh -l user x.x.x.x
expect "login as:"
expect "password:"
send "password\r"
expect "$\r"
send "telnet nemonic\r"
expect "$\r"
expect "login:"
send "user\r"
expect "password:"
send "password\r"
expect "*>"
send "terminal length 0\r"
send "show version \r"
expect "*>"
set results $expect_out(buffer)
set config [open out.txt w]
puts $config $results
close $config
send "exit\r"
expect eof
send "\r"
send "exit\r"
Could you help me?
Best regards
I solve with
log_file -noappend status.txt
after the command show version
Thank you && Best Regards

Expect script failing to continue

I have an expect script which is logging onto a device and sending a command, however the script completes without finishing, its like the last expect isn't being picked up.
How do I enable debugging to watch the script progress so I can identify where the issues is?
My script looks like this...
#!/usr/bin/expect
set hostname [lindex $argv 0]
set username "a.user"
set password "a.pass"
spawn telnet $hostname
expect "Username:" {
send "$username\r"
expect "Password:"
send "$password\r"
}
expect "#" {
send "sh ver\r"
}
It stops processing at the below line;
expect "#" {
however I can clearly see the # in the last output.
Thanks

bash script which downloads a file with sftp

I need to make a sftp connection with a password and download a file. There is an ip restriction so first of all i should make a ssh connection. I wrote a script but it stucks after connecting with ssh.
Note: i also tried doing it with an expect script but it also didn't work.
#!/usr/local/bin/
ssh test#test1.t.com
lftp sftp://test2:123456#test2.com
get "file.xls"
Edit: You can also see my expect code here.
#!/usr/local/bin/expect -f
expect -c "
spawn ssh test#test1.t.com
expect \"test\#test1\:\~\$\"
spawn sftp test2#test2.com
expect \"*assword:\"
send \"123456\r\"
expect \"sftp\>\"
send \"get file.xls\r\"
expect \"sftp\>\"
exit 1
";
I'm not sure exactly what you're trying to accomplish here. First, I'll address the problems in your expect script. Since your shebang line invokes expect, you don't need to wrap the expect body in a call to expect. That gets rid of all the backslashes. Next, you have 2 spawn calls, which raises questions about you're intent. I'm going to assume that you want to ssh to test1, then grab the file from test2 so the file exists on test1. This assumption changes the 2nd spawn to a plain send command.
#!/usr/local/bin/expect -f
set shell_prompt "test#test1:~$"
set sftp_prompt "sftp>"
spawn ssh test#test1
expect $shell_prompt
send "sftp test2#test2\r"
expect "*assword:"
send "123456\r"
expect $sftp_prompt
send "get file.xls\r"
expect $sftp_prompt
send "exit\r"
expect $shell_prompt
send "exit\r"
expect eof
Now, you can scp the file to your local machine. Let's put those 2 steps into one shell script:
#!/bin/sh
expect <<'EXPECT_SCRIPT'
set shell_prompt "test#test1:~$"
set sftp_prompt "sftp>"
spawn ssh test#test1
expect $shell_prompt
send "sftp test2#test2\r"
expect "*assword:"
send "123456\r"
expect $sftp_prompt
send "get file.xls\r"
expect $sftp_prompt
send "exit\r"
expect $shell_prompt
send "exit\r"
expect eof
EXPECT_SCRIPT
scp test#test1:file.xls .

Automatic disconnect telnet in bash scripting

When i run the script below, it connects the server but it disconnect before sending a command. How can i solve this problem.
#!/bin/bash
/usr/bin/expect << EOD
spawn telnet 31.168.109.31
sleep 2
expect ">"
sleep 1
send "my_password"
send "\n"
sleep 1
interact
As per your code you expect > before password. I am not getting you what is purpose of it but as for some example script look here. It's might be not an answer but probably help you.
#!/bin/bash
/usr/bin/expect << EOD
set timeout 200
spawn telnet 31.168.109.31
expect "login:"
send "root\r"
expect "password"
send "mypassword\r"
expect "root#"
#rest of your logic what you want.

Resources