Re: SSH login automation, get stuck at the last step.

Thank you, Y.T:)

The ssh server that I am copying the public key to is not a typical
unix box. The user "root" to login this box can not access the
directories within, but I can use another user "admin" to login to
access the directories. I have copied the key and cat the key to the
authorized_keys. Now I can login as user "admin" with only typing the
passphrase, but I need to login as "root" to execute the "run batch"
command instead of user "admin".

On Feb 20, 8:43 am, "Why Tea" <ytl...@xxxxxxxxx> wrote:

I hope you managed to create the keys with the script. It worked for

I have modified Uwe's script to handle passphrase. It should now
handle login's with user's password as well as user's passphrase for
encryption keys. For your test:

1) Use only one system in loginlist
2) Add your passphrase as an additional item into the list
3) The "interact" at the end is for you to do some manual tests

The modified script worked for me using either password or passphrase.
The systems tested were Linux and Solaris. As I mentioned, I used to
have a lot of problems getting it to work this way, but that was a few
years ago. If this fails to work for you, you can then try the ssh-
agent method. Good luck.


----------------------- Uwe's modified script -----------------------

# empty string for a well formed prompt, else fill in the expected
set loginlist {
{username} {hostname or IP} {password} {system
prompt} {passphrase}


foreach {user host passwd prompt passphrase} $loginlist {
puts stderr "Login in to $host as $user"
# or whatever is the proper prompt for your systems
if { "$prompt" == "" } {
# mind the space at the end!!
# if you leave that off you will get eratic behaviour
set prompt "${user}@${host}:~> "

spawn ssh ${user}@${host}

set success 0
expect \
user: {
# with the username given you should never be here
exp_send $user\r
puts stderr "EXP username requested and sent "
} assword: {
# in autologon you don't see this either
# and the password request neither
exp_send $passwd\r
puts stderr "EXP password requested and sent "
} -re "passphrase .*: " {
# passphrase
exp_send $passphrase\r
puts stderr "EXP password requested and sent "
} "connecting (yes/no)?" {
# never been there before, answer yes:
exp_send "yes\r"
puts stderr "EXP hostkey new!"
} $prompt {
# ok all the trash like motd, hello and last logon
# are ignored
puts stderr "EXP Loged in!, motd ignored"
set success 1
} timeout {
# nobody there, something else wrong, whatever
puts stderr "EXP Timeout, theres been an error"
if {$success} {
puts stderr "EXP Logged in to $host as $user
lappend ::INFO(hosts)
set ::INFO($host,spawn_id) $spawn_id
set ::INFO($host,user) $user
} else {
puts stderr "EXP NO Login possible bahhhh"


----------------------- Uwe's modified script -----------------------


On Feb 19, 5:09 pm, "Why Tea" <ytl...@xxxxxxxxx> wrote:


Not sure how you got stuck. Nevertheless, you can try with this

/Why Tea

On Feb 20, 2:52 am, "Schubert" <cxbest2...@xxxxxxxxx> wrote:

Thanks, but does any one can tell me how to copy the created public
key to the server?
I got the warning message saying:
blah blah blah...

I followed Why Tea's steps:

1. $ ssh-keygen -t rsa
2. $ cd $HOME/.ssh
3. scp <root@ssh_server IP>
... get stuck here...

If this problem can be solved , I will follow Why Tea's steps to do
the next...

On Feb 15, 5:39 am, "Why Tea" <ytl...@xxxxxxxxx> wrote:

On Feb 14, 12:11 am, "Schubert" <cxbest2...@xxxxxxxxx> wrote:

On Feb 13, 4:57 pm, Uwe Klein <uwe_klein_habertw...@xxxxxxxxxxx>

Schubert wrote:
But when I use Expect script above to automate the manual steps, it
will get stuck running the batch file. It simply shows the
[root@<Server Name>: <Server IP address>] prompt and then stops. Does
anybody know why this happens? Greatly appreciated.

Nothing expected, nothing shown.

[expect] the batch completion message
expect $batch_message
and then the prompt.
expect $prompt

then exit
exp_send "exit\r"
expect eof

with expecting some output it may be a good idea to expect
timeouts and errormesssages like:

exp_send $runbatchline\r
set ::JOB "not done"
expect \
$batchokmsg {
puts "batch run OK"
set ::JOB OK
# need prompt, continue will restart this expect
} $batcherrormsg {
puts "error in batch"
set ::JOB ERR
# you are still waiting for the prompt right?
} $prompt {
puts "back at the prompt JOB stat: $::JOB "
} timeout {
puts "sleeping at the job $::JOB ?"

something like the above, adapt to your case.


Thanks uwe. But that's not my case. Maybe I should make it more

When I execute my script on the top, I expect a prompt like
[root@<ServerName>@<Server IP address>] so that I can send "run batch
filename.txt" to the server. But the execution of the script stops
after the prompt [root@<ServerName>@<Server IP address>] shows, and
after several minutes, it timed out. It even didn't run the "run batch
filename.txt" command.

Does anybody know about using Expect to do ssh login and then run any


I had a chat with Uwe a week or two ago about the SSH login issue in
another thread. I remember from my own experience a few years ago, to
automatic login to as SSH with a password was unreliable. In your
case, I suspect the SSH server had not authenticated you and hence it
didn't execute what came next. What I used to do was to do the
simplest thing first in the Expect script:
1) login to SSH server
2) run xclock & (remember to use the -X switch in running ssh)
3) if xclock shows up every time in your X Window, you should be OK

I had a lot of problems getting the above to work reliably with your
(and Uwe's) way of authentication. Then I followed the howto
suggestions in
to do the following:
1) create the public/private key pair and use
passphrase for the private key and copy the
public key the the remote host (just the
standard stuff)
2) instead of spawning ssh, the script spawns ssh-add
3) expect "id_rsa: "
4) you can send in the hard code passphrase
5) you should be logged if you get an "eof", i.e. expect eof
6) now, spawn ssh and do what you have to do

Steps 2-6 are pretty much what's described in the Web page.
Assuming you have written your script as described and called
it autossh.exp, you would run it with ssh-agent, i.e.
ssh-agent autossh.exp

Another source of reference:

Hope it helps.- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -