Reputation: 413
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.
Upvotes: 1
Views: 905
Reputation: 246807
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
Upvotes: 1