SSHコマンドはExpectスクリプトでは異なる動作をする

SSHコマンドはExpectスクリプトでは異なる動作をする

私はServer1でこのコマンドを使用しています

~# ssh root@Server2 /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

Server2 の内容をServer1id_rsa.pubに追加しますauthorized_keys

手動で実行すると動作しますが、expect スクリプトで実行すると次のようになります。

#!/usr/bin/expect

set timeout 60

spawn ssh root@[lindex $argv 0] cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

expect "yes/no" { send "yes\r" 
expect "*?assword" { send "[lindex $argv 1]\r" }
    } "*?assword" { send "[lindex $argv 1]\r" }
interact

スクリプトを使用すると、id_rsa.pubServer2 の が Server2 の の上に追加されますauthorized_keys

正しい構文は何でしょうか?

答え1

expect はシェルではないようなので、>>解釈するのではなく、引数として ssh に渡します。

試すspawn bash -c "ssh root@[lindex $argv 0] cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys"

関連情報