1

I am unable to run commands on remote host using expect script.It just logs in to the remote host and exits.Here is the code

#!/usr/bin/expect
set timeout 15
puts "connecting to the storage\n"
set user [lindex $argv 0]
set host [lindex $argv 1]
set pass "root123"
spawn ssh "$user\@$host"
expect {
"Password: " {
send "$pass\r"
sleep 1
expect {
"$ " {
  send "isi quota quotas list|grep ramesh\r" }
"$ " {
  send "exit\r" }

}
}
"(yes/no)? " {
send "yes\r"
expect {
"$ " { send "ls\r" }
"$ " { send "exit\r" }

"> " {}
}
}
default {
send_user "login failed\n"
exit 1
}
}

It only gets into the remote host and exits. [deep@host1:~]$ ./sshexpect user1 host2 connecting to the storage

spawn ssh user1@host2
Password:
host2$
[deep@host1:~]$

Is the syntax wrong? I am new to tcl scripting.

1 Answer 1

1

Indentation would help a lot:

expect {
    "Password: " {
        send "$pass\r"
            sleep 1
            expect {
                "$ " { send "isi quota quotas list|grep ramesh\r" }
                "$ " { send "exit\r" }
            }
    }
    "(yes/no)? " {
        send "yes\r"
            expect {
                "$ " { send "ls\r" }
                "$ " { send "exit\r" }
                "> " {}
            }
    }
    default {
        send_user "login failed\n"
            exit 1
    }
}

The problem is here:

            expect {
                "$ " { send "isi quota quotas list|grep ramesh\r" }
                "$ " { send "exit\r" }
            }

You're matching the same pattern twice: I suspect expect is ignoring the first action block, and just using the 2nd one; thus you exit immediately.

This is what you want to do:

expect {
    "(yes/no)? " { send "yes\r"; exp_continue }
    "Password: " { send "$pass\r"; exp_continue }
    timeout      { send_user "login failed\n"; exit 1 }
    -re {\$ $}
}
send "isi quota quotas list|grep ramesh\r"

expect -re {\$ $}
send "ls\r"

expect -re {\$ $}
send "exit\r"

expect eof
Sign up to request clarification or add additional context in comments.

5 Comments

I understood the mistake, but I didnt get what does the below line stands for -re {\$ $} And also why do we need to put expect eof at the end of the script
The -re option uses regular expression pattern instead of the default glob patterns. I'm specifying that the prompt is a dollar sign and a space at the end of the line. If expect sees a dollar sign and a space and then some other text, that won't match as a prompt. expect eof basically means "wait until the spawned process is closed" -- not strictly necessary here, but generally good practice.
I tried that still doesnt work for me.It shows the "login failed" and exits. It just logs into the remote box and then blank and after sometime exits with "login failed"
run your script with expect -d filename to enable debug output. expect will tell you why none of the patterns match.
I got it! Works for me now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.