ssh via expect

(Crossposting note: I have already posted this article on comp.lang.tcl 6 days ago and on the tek-tips dot com forum 3 days ago. This is posted here again, because I didn't get any response on my original articles there).

I use the following script on Solaris to log into a remote host:

spawn ssh other@[exec uname -n]
expect {$}
send ". xxx/s\r"
interact

This script works well. However, I don't quite understand the output
printed by this script on stdout:

spawn ssh other@rom38
 xxx/s
 xxx/s
Last login: Tue May 31 10:01:35 2011 from rom38.muc.infin
bash-2.03$ . xxx/s
[other] rom38@~>

The last line is the prompt after logging in. However, why do I see
the line

. xxx/s

three times? I would have expected it only once (the last occurance),
but why do I see it already before twice? My guess is that this is
some timing issue (that I send the command just a bit too early),
but even then, I should see only one extra occurance.

I believe it's the terminal that is making you see what you typed ahead multiple times (and I believe it's somehow related to the amount of output you're getting while logging in). Me too, I get multiple occurrences of the sent string as far as my expect command matches the first line of a multi-line input.

I suppose you won't see those lines if you adjust your expect pattern {$} in order to match your prompt.

I believe that you need to escape the $ in order to match a literal dollar sign and not the end of the string (i.e. the braces are not sufficient in this case).

1 Like

I created my own solution for copying files to remote machines and performing commands on those machines with expect in the thread "Need expect script to copy trusted keys". Sorry they won't let me post a url because I don't have 5 posts yet.

1 Like

Thanks a lot - escaping the $ at least reduced the three occurances to two!