问题
I use expect to copy my key to remote linux boxes.
copy_key
#!/usr/bin/expect
stty echo
set REMOTEhost [lindex $argv 0]
set REMOTEpass [lindex $argv 1]
spawn ssh-copy-id -i /home/myuser/mykey.pub root@$REMOTEhost
expect "password: "
send "$REMOTEpass\n"
sleep 2
This is called via ./copy_key 1.1.1.1 GENERATED_PASSWORD
I'd like to migrate all of this to a bash script.
Something like the following, which of course is something different but just to help with an idea:
#!/bin/bash
/usr/bin/expect -c 'expect "\n" { eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com; interact }'
This doesnt work
#!/bin/bash
ssh-copy-id -i /home/myuser/mykey.pub root@$REMOTEhost
How can I do that?
回答1:
This would be the 1:1 bash script:
#!/bin/bash
stty echo && expect -c "spawn ssh-copy-id -i /home/myuser/mykey.pub root@$1; expect \"password: \"; send \"$2\r\"; sleep 2;"
来源:https://stackoverflow.com/questions/30673596/linux-bash-expect