简体   繁体   中英

Expect not working inside my Bash script

I am trying to execute expect command inside by small bash script to login into servers using key authentication method. My script is as follows:

#!/bin/bash
HOST=$1
/usr/bin/expect -c "
                    spawn ssh -i /root/.ssh/id_rsa root@$HOST  
                    expect -exact "Enter passphrase for key '/root/.ssh/id_rsa': " ;
                    send "PASSPHRASE\n" ; 
                    interact
                   "

Output with error is:

spawn ssh -i /root/.ssh/id_rsa root@server.domain.com
couldn't read file "passphrase": no such file or directory

Can you help to correct this?

There is a quoting issue in your code. Rather than trying to pass commands to expect on the command line save your code as an Expect script. You can then run it from a shell script or otherwise.

script.exp

#!/usr/bin/expect
# usage: ./script.exp host
set HOST [lindex $argv 0]
spawn ssh -i /root/.ssh/id_rsa root@$HOST  
expect -exact "Enter passphrase for key '/root/.ssh/id_rsa': "
send "PASSPHRASE\n"
interact

However, if that's all you're doing with Expect in this case I second the suggestion to use ssh-agent instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM