来自 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:
然后我尝试连接,系统提示我输入密码...
检查服务器,我确定没有上传密钥,因为我希望列出“authorized_keys”文件:
root@server: ls /home/user/.ssh/
known_hosts
我究竟做错了什么?
答案1
除了关于“root”的评论:
- 单引号只是expect中的普通字符。使用双引号
- 发送密码时不要忘记“按 Enter”
expect "*?assword*"
send "thepassword\r"
引用heredoc结束词通常是一个很好的做法,除非您需要在heredoc正文中插入变量或命令替换:
some command <<'END'
# .............^...^
...
END
答案2
你跑spawn ssh-copy-id -i /home/user/.ssh/id_rsa.pub 111.111.111
。 ip地址好像不对。
答案3
从密码提示中您可以看到:[email protected]
您正在远程设置 root 的密钥,即 file /root/.ssh/authorized_keys
。以 而非 root 身份运行脚本user
,或者提供显式.这允许无需密码登录,而不是 root。ssh-copy-id ... [email protected]
user