expect script for ssh-copy-id
时间: 2023-09-13 21:07:31 浏览: 112
ssh-copy-id 脚本
Here is an example expect script for automating the `ssh-copy-id` command:
```
#!/usr/bin/expect
set timeout 10
set host [lindex $argv 0]
set password [lindex $argv 1]
set ssh_key [lindex $argv 2]
spawn ssh-copy-id -i $ssh_key $host
expect {
"*(yes/no)?" {
send "yes\r"
exp_continue
}
"*password:" {
send "$password\r"
expect eof
}
}
```
To use this script, save it as a file (e.g. `ssh-copy-id.expect`), make it executable (`chmod +x ssh-copy-id.expect`), and then run it with three arguments: the hostname or IP address of the remote server, the password for the remote user, and the path to the SSH key you want to copy over.
For example:
```
./ssh-copy-id.expect example.com password /path/to/my/key.pub
```
This script will automatically answer "yes" to any prompt about adding the remote host to the list of known hosts, and then enter the password for the remote user when prompted. Once the password is entered, `ssh-copy-id` will copy the public key to the remote server's authorized_keys file.
阅读全文