2

我想cat ~/.ssh/id_rsa.pub | ssh root@host 'cat >> .ssh/authorized_keys'expect. 使用spawncommand 执行命令时,

spawn cat ~/.ssh/id_rsa.pub | ssh root@host 'cat >> .ssh/authorized_keys' 

它抛出错误消息,

cat |: No such file or directory
cat ssh: No such file or directory
...

我应该如何产生管道命令?

4

4 回答 4

5

因为您想在技术上一次执行多个 shell 命令(并且 spawn 不处理管道 I/O),所以您需要将它们封装在脚本中,然后使用 spawn 执行脚本。

你想要的脚本已经作为 ssh-copy-id 存在,但是如果你想要一个精简的版本,你可以动态创建一个脚本文件,然后将它传递给期望 spawn:

cat > /tmp/sshkeycopy.sh <<MYEOF
cat ~/.ssh/id_rsa.pub | ssh user@host 'mkdir -p -m 600 ~/.ssh; cat >> ~/.ssh/authorized_keys'
MYEOF

chmod u+x /tmp/sshkeycopy.sh

expect -c "
spawn /tmp/sshkeycopy.sh
expect { ... }"

在这个 bash 示例中,shell 将按预期处理管道,并且只是抛出密码提示以供期望处理。

于 2012-11-11T19:01:32.397 回答
3

是否spawn处理输入重定向?

spawn ssh root@host "cat >> .ssh/authorized_keys" < ~/.ssh/id_rsa.pub

如果您不cat使用expect.

编辑:在命令周围使用双引号cat,而不是单引号(经测试,这不起作用)

于 2012-09-20T14:26:14.700 回答
1

您可以使用此模式:

spawn bash -c "cat MEH | ssh root@host 'cat >>HMM' " 
于 2016-04-12T23:14:29.743 回答
0

这已经过测试并且可以在 Linux 上运行

#!/usr/bin/expect -f
set DOMAIN www.somedomain.tld
set TARFILE "[exec bash -c "echo $DOMAIN | cut -d'.' -f 1"].cert.tar"
puts "$TARFILE\r"
于 2018-05-08T20:59:16.027 回答