user1658549
user1658549

Reputation: 39

Using expect to log in and issue a command

Can you tell me why this Expect script shows me a calendar in loop mode? I only want to see one calendar.

#!/usr/bin/expect
set fid [open /Users/john/secret]
set password [read $fid]
close $fid

spawn ssh [lindex $argv 0]@[lindex $argv 1]
expect {
-re ".*Are.*.*yes.*no.*" {
send "yes\n"
exp_continue
}

"*?assword:*" {
send $password
send "\n"
exp_continue
}

"*$*" {
send "cal\r"
exp_continue
}

"*$*" {
send "exit\r"
}

Upvotes: 1

Views: 126

Answers (1)

Michael Petch
Michael Petch

Reputation: 47573

This seems wrong to me

"*$*" {
send "cal\r"
exp_continue
}

"*$*" {
send "exit\r"
}

The check for a dollar sign is at the same level so after you send cal\r you loop and it will look for $ again and issue cal\r again not reaching the last "*$*" {

You might have been going for this?

#!/usr/bin/expect
set fid [open /Users/john/secret]
set password [read $fid]
close $fid

spawn ssh [lindex $argv 0]@[lindex $argv 1]
expect {
    -re ".*Are.*.*yes.*no.*" {
        send "yes\n"
        exp_continue
    }
    "*?assword:*" {
        send $password
        send "\n"
        exp_continue
    }
    "*$*" {
        send "cal\r"
        expect "*$*" {
            send "exit\r"
        }
    }
}

You should notice that I have nested the second check for a $ inside the check for the first. I also assume you left off a closing bracket at the bottom of your original question?

Upvotes: 2

Related Questions