Is there a way to expect and send after interact command
If I remember right, you do this:
#!/usr/bin/expect
log_user 0
spawn ssh -o PubkeyAuthentication=no [lindex $argv 0] -n [lindex $argv 1]
expect "Password:" {send "mypassword\r"}
interact {
"mykeyword" {
send "\003\177\015"
exp_continue
}
}
You pass the things to watch out for and actions to take as arguments to interact
(just like with expect
) and you tell the response script to exp_continue
at the end so that it keeps on interacting/expecting.
expect, interact and then again expect
You can read (expect_user
) the user's password by yourself and then send
it to the spawn'ed program. For example:
[STEP 101] # cat foo.exp
proc expect_prompt {} \
{
global spawn_id
expect -re {bash-[.0-9]+(#|\$)}
}
spawn ssh -t 127.0.0.1 bash --noprofile --norc
expect "password: "
stty -echo
expect_user -timeout 3600 -re "(.*)\[\r\n]"
stty echo
send "$expect_out(1,string)\r"
expect_prompt
send "exit\r"
expect eof
[STEP 102] # expect foo.exp
spawn ssh -t 127.0.0.1 bash --noprofile --norc
root@127.0.0.1's password:
bash-4.3# exit
exit
Connection to 127.0.0.1 closed.
[STEP 103] #
Can the expect script continue to execute other command after interact?
YES, processing can continue after an [interact].
Short answer: change the last {send ...} to {exec date >> login_history.log}
There are several concepts you'll want to understand to achieve the control flow you're after. First, http://www.cotse.com/dlf/man/expect/interact_cmd_desc.htm provides a succinct synopsis and example of intermediate [interact] use.
Second: why did you see the message "... spawn id ... not open ..."? Because the spawn id is not open. The script you wrote said, in effect, "interact, then, after interact is over, send a new command to the ssh process." If you've already logged out, then, of course that id for a defunct process is no longer available.
Third: how do you achieve what you want? I'm unsure what you want. It sounds as though it would be enough for you simply to transform the [send] as I've described above. How does that look to you?
How do I tell expect that I have finished the interactive mode?
Your problem is two-fold...
You should interact with an explicit
return
, and give it some way to know you've released control... in this case, I use three plus signs and hit enter.After you return control, the script will need to get the prompt again, which means the first thing you do after returning control to expect is send another
\r
. I edited for what I think you're trying to do...
Example follows...
#!/bin/bash
set timeout -1
expect -c "
spawn telnet $IP $PORT1
sleep 1
send \"\r\"
send \"\r\"
expect Prompt1>
interact +++ return
send \"\r\"
expect {
Prompt2> {send \"dir\r\" }
}
"
Interact not working from within Expect Script
interact
can't let the user enter data via stdin because you are already redirecting stdin for the here document.
Instead, you can save your here document with all expansions to a variable, and then pass that to -c
. Here's a simplified example:
script=$(cat << EOF
spawn vi
send "iHello $(hostname)"
interact
EOF
)
expect -c "$script"
Linux expect command without interact directive not working
Without interact
the Expect script will exit after the last command expect "]#"
and it'll kill the spawned process. It's just like you close the SSH client application (like PuTTY) window when the shell is still alive running.
The interact
is a long-running command which waits for the spawned process to exit.
Using two interact in a Expect script
I have resolved the problem:
#!/usr/bin/expect
set timeout -1
set arg1 [lindex $argv 0]
spawn ssh -p 24 my_username@my_linux.domain.com
expect "#"
send "go $arg1 \n"
expect "sername:"
send "my_username\n"
expect "assword:"
send "my_password\n"
expect "#"
interact timeout 5 return
send "\n"
expect "root@my_linux:~#"
send "exit\n exit\n"
interact
Explanation: I added a few lines:
# This prevents commands from timing out (default timeout is 10 seconds).
set timeout -1
# When I type something, the timeout is ignored, but when I'm not typing,
# it waits 5 seconds and then continues.
interact timeout 5 return
send "\n"
expect "root@my_linux:~#"
send "exit\n exit\n"
Related Topics
Bash Scripting - Read Single Keystroke Including Special Keys Enter and Space
Cannot --Enable-Pcregrep-Libbz2 Because Bzlib.H Was Not Found
How to Protect My Process from Being Killed
Where Is Open_Max Defined for Linux Systems
Linux Socket Using Multiple Threads to Send
How to Send Sigint (Ctrl-C) to Current Remote Process Over Ssh (Without -T Option)
How to Debug the Linux Kernel with Qemu and Kgdb
Vim: Access to System Clipboard via Ssh - Linux to Os X
Wrong Payload When Using Bluez Stack as Peripheral
Alsa Cannot Set Sample Format[Ffmpeg]
How to "Expect" and "Send" After "Interact" Command
Dhcp Server to Redirect Any Url to Landing Page
Mapping Physical Addresses to Virtual Address Linux
Force Linux to Use Only Memory Over 4G
Change Filenames to Lowercase in Ubuntu in All Subdirectories
Differencebetween Module_Init and Init_Module in a Linux Kernel Module