Sujeet Padhi
Sujeet Padhi

Reputation: 264

Unix - Expect Not Working While Executing Commands

I am trying to execute commands on a remote UNIX host using send and expect ssh module, but even if the script logs in to the server successfully it does not execute commands.

#!/usr/bin/expect
set timeout 60
spawn ssh xxxx@xxxxxx

expect "yes/no" {
        send "yes\r"
        expect "*?assword" { send "xxxxxx\r" }
        } "*?assword" { send "xxxxxxx\r" }

expect "$ "
#sleep 5
send "ps -aef \r"

Output

[xxxxx@xxxxxx Scripts]$ ./TestExpect.sh
spawn ssh xxxxx@xxxxxx

xxxxxx@xxxxxx's password:
Last login: Wed May  9 02:05:47 2018 from xxxxxxxxx
Kickstarted on 2015-05-12
[xxxxx@xxxxx ~]$ [xxxxxx@xxxxx Scripts]$

The Prompt looks like below

[aacdd123@linprod345 ~]$

Upvotes: 0

Views: 1049

Answers (1)

Fidel
Fidel

Reputation: 1037

Issue may be because, you are not expecting anything after sending the ps -aef. Hence the expect spawn process has exited before printing the output.

Try adding few more commands after the sending ps -aef

send "ps -aef\r"
expect $prompt
send "echo hello\r"
expect $prompt

Try looking into the expect_out buffers too, which will give you the captured streams.

puts $expect_out(buffer)

Upvotes: 2

Related Questions