从一个bash脚本:
source ./expect.sh
我包括一个期望代码:
#!/bin/bash /usr/bin/expect <<EOL spawn ssh-copy-id -i /home/user/.ssh/id_rsa.pub 111.111.111 expect '*?assword*' send 'thepassword' interact EOL
我得到这个:
spawn ssh-copy-id -i /home/user/.ssh/id_rsa.pub 111.111.111.111 /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys [email protected]'s password:
然后,我尝试连接,并提示input密码…
检查服务器,我确定没有密钥上传,因为我希望列出“authorized_keys”文件:
root@server: ls /home/user/.ssh/ known_hosts
我究竟做错了什么?
问题是,SSH客户端直接从terminal读取密码,而不是从标准input。
我所知道的最简单的方法是安装'sshpass',然后使用这个(没有期望):
sshpass -p "thepassword" ssh-copy-id -i /home/user/.ssh/id_rsa.pub [email protected]
您正在将密钥复制到/root/.ssh/authorized_keys而不是用户帐户。 请注意: [email protected]'s password:
下面的脚本也应该这样做
#!/usr/bin/expect -f # # Install RSA SSH KEY with no passphrase # set user [lindex $argv 0] set host [lindex $argv 1] set password [lindex $argv 2] spawn ssh-copy-id -i /path/to/your/.ssh/id_rsa.pub $user@$host expect { "continue" { send "yes\n"; exp_continue } "assword:" { send "$password\n"; } }
您需要使其可执行,并调用它如下所示:
./ssh-copy-id.exp <user> <host> <password>
在你的情况下:
./ssh-copy-id.exp root 111.111.111.111 thepassword