0

我尝试建立远程连接的主机系统正在 SunOS 上运行。系统上没有 ssh-copy-id 可用。经过搜索,我发现以下命令在执行时会模仿 ssh-copy-id 功能。

 cat ~/.ssh/id_rsa.pub | ssh remotehost 'cat >>~/.ssh/authorized_keys && echo "Host Key Copied"'

我正在尝试使用 expect 编写一个脚本来执行此操作,但它失败并出现以下错误。 代码:

#!/usr/bin/expect -f
#!/bin/bash

set username  [lindex $argv 0]
set password  [lindex $argv 1]
set host      [lindex $argv 2]

cat ~/.ssh/id_rsa.pub | ssh remotehost 'cat >>~/.ssh/authorized_keys && echo \"Host Key Copied\"'
expect "Password:"
send "$password\n"
expect eof

错误:

invalid command name "cat"
    while executing
"cat ~/.ssh/id_rsa.pub | ssh remotehost 'cat >>~/.ssh/authorized_keys && echo \"Host Key Copied\"'"
    (file "./remote.sh" line 8)

通过搜索,我了解到默认情况下 expect 不会接受 shell 命令。有人可以帮助解决我的问题。提前致谢。

4

1 回答 1

1

您缺少spawn命令:spawn启动您正在与之交互的进程。您还缺少以下ssh-copy-id命令:

spawn ssh-copy-id $username@$host
expect ...

但是,您所做的是大大降低了安全性。在命令行上以明文形式传递密码,它可能会存储在 shell 的历史文件中。

于 2018-07-04T15:30:34.090 回答