-   Programming (
-   -   My "expect" script is not working... Won't "send" commands... (

edomingox 03-31-2009 02:45 PM

My "expect" script is not working... Won't "send" commands...
I've been working on this script for hours trying to figure out why it's not working. The following code is a portion of the whole script. This code is actually within a "for" loop. here's the code:

                set timeout 3
                spawn -noecho ssh ${account}@$ipAddress
                send_user "Configuring $equipment...\n"
                log_user 0
                expect {
                        "yes/no" {
                                send "yes\r"
                        "*?assword:*$" {
                                send "$pass\r"
                        timeout {
                                set error "$equipment $ipAddress is unreachable\n"
                                set passLog "$passLog$error"
                # LOOKING FOR THE PROMPT
                expect {
                        "#" {
                                send "ls\r"       
                                log_user 1
                                send_user "Working...\n"
                                send "exit\r"
                                send_user "Working2...\n"
                        "Permission denied" {
                                set error "Wrong Password for $equipment $ipAddress using user \"$account\".\n"
                                set passLog "$passLog$error"

The script was made to ensure a clean login is made and report any unreachable ip addresses or bad password inputs.

My greatest concern lies within the expected "#" portion. The lines with "Working..." and "Working2..." both display. So it seems to be bypassing my "send ls/r" and the "send exit/r". I don't know why it's doing that. Any help would be great.

kenoshi 03-31-2009 03:18 PM

make sure you insert:


expect eof
After you send "exit" to the remote shell. Either that or go interactive.

Hope this helps.

edomingox 04-01-2009 09:15 AM

my biggest concern is when I'm trying to do the "send ls\r" and it doesn't work. I don't really want to exit the script in there because it's in a loop. I only added the exit to see if that it actually does exit, but it doesn't. I think I may have some syntax or something wrong.

edit: I tried expect eof after the send ls and that worked. thanks for the help. i'll have to read up on why it's done this way as i wasn't really expecting it to run like that.

jlinkels 04-01-2009 10:29 PM

Did you put set exp_internal 1 at the beginning of your code?


edomingox 04-02-2009 04:25 PM

I havent tried doing that yet. But I figured out that the script was getting ahead of itself. So the expect eof helped that out. I'll test that out too. Just to see what it shows me.

All times are GMT -5. The time now is 08:35 PM.