1. 程式人生 > >使用expect 批量分發ssh公鑰

使用expect 批量分發ssh公鑰

expect

#!/usr/bin/expect
if {$argc != 2} {  #首先註意大話號,彼此之間需要空格
  send_user "USAGE:expect_sshkey.exp file host"
  exit
}

#define var
set file [lindex $argv 0]
set host [lindex $argv 1]
set password "12345"

#spawn
spawn ssh-copy-id -i $file "-p52113 jpinsz@$host" >/dev/null 2>&1

#expect
expect { #也是要註意大話號,與首單詞之間需要空格
        "yes/no" {send "yes\r";exp_continue}
        "*password" {send "$password\r";send_user "eof/n"}
}

expect eof

根據這個腳本,當目標主機已經存在sshkey時,將不會提示“yes/no”、“*password”,因此也不會有eof,所以會有
spawn id exp4 not open這個報錯!!


使用expect 批量分發ssh公鑰