cancel
Showing results for 
Search instead for 
Did you mean: 

need info on expect script

SOLVED
Go to solution
Karthik_sg
Frequent Advisor

need info on expect script

hi all,
i am trying to write a expect script.which doeas not ask 4 a password.The password shd be inside the expect script.
ex:i want to connect to root@34.343.4.3 pasword=xxxxx.
how to do it inside the script.give an example.thnks in advance
3 REPLIES
Stuart Browne
Honored Contributor
Solution

Re: need info on expect script

It's pretty easy. The quick-whip-up to catch a few common things is as follows:

--- start ---
#!/usr/bin/expect

log_user 0

set my_passphrase "passphrase"
set my_password "password"

spawn "/usr/bin/ssh" "root@34.343.4.3"

while {1} {
expect {
"continue connecting (yes/no)?" {
send "yes\n"
}
"passphrase" {
send "$my_passphrase\n"
}
"password" {
send "$my_password\n"
}
-re "(#|\\\$|%) $" {
log_user 1
send_user "Shell prompt found. Dropping to shell:"
send "\n"
break
}
}
}
interact
--- end ---

This has separate variables for pass phrases and pass words. Passphrases are for local keys if used. Passwords are for remote authentication.

It also catches the havent-ssh'd-to-the-box-before thing too.

The last bit (the '-re' bit) is the line to see if it finds a shell prompt. If not, there's a built-in 10 second timeout which will dump you out.

The 'interact' at the end should be fairly self explanatory.

If you want to pass the password/passphrase, then you'll need to mess around with the '[lindex argv]' TCL variable stuff.

If all else fails, try using 'autoexpect'. It will generate a custom script for you to modify.
One long-haired git at your service...
Karthik_sg
Frequent Advisor

Re: need info on expect script

thnks the script really helped and its working.i have assigned the points.Can u explai a the above script in detail.Thnks
Stuart Browne
Honored Contributor

Re: need info on expect script

The man page for 'expect' is pretty straight forward, but I'll explain away.

log_user 0

This tells expect to not show any of the spawn's output. Makes it looks neat 'n pretty :P

set

Simple variable assignment. Strictly speaking, the quotes aren't needed for plain text strings (either here, or in the expect strings), but it makes it easier to differentiate what's what.

spawn

This forks off the command, tieing STDIN, STDERR and STDOUT to expect for it to parse/manipulate.

The 'while {1} {' loop is so we don't have to repeat the expect strings over and over.

expect {

The simple form of 'expect' is to take the match string and the command all on the one line (i.e. 'expect "passphrase" send "$my_passphrase\n"').

But as we have multiple possible strings which might match, we use the more open form of the expect command. From here, we list the possible match texts:

"continue connecting (yes/no)?"
"passphrase"
"password"
-re "(#|\\\$|%) $"

The first three should be straight forward and easy to understand. The fourth does a 'regular expression' match on the STDOUT string from the spawned process.

The 'send' command is used to send a string to the spawned process's STDIN.

The 'send_user' is to send text to the user's TTY.

The 'break' is to break out of the while loop.

And finally, we come to the:

interact

As I think I mentioned before, this throws back interactive control to your TTY for you to do things with.
One long-haired git at your service...