expect 分发ssh key脚本

 1 #!/usr/bin/expect
 2 if {$argc != 1} {
 3   send_user "USAGE:expect_sshkey.exp host"
 4   exit
 5 }
 6 
 7 #define var
 8 set host [lindex $argv 0]
 9 set password "123"
10 
11 #spawn
12 spawn ssh-copy-id -i /home/jp/.ssh/id_dsa.pub -p 52113 jp@$host
13 
14 #expect
15 expect {
16   "yes/no" {send "yes\r";exp_continue}
17   "*password" {send "$password\r";}
18 }
19 
20 expect eof
21 
22 exit -onexit {
23   send_user "Good to you!\n"
24 }

 生成ssh key

 1 #!/usr/bin/expect
 2 #spawn
 3 spawn ssh-keygen -t dsa
 4 expect {
 5   "*key" {send "\r";exp_continue}
 6   "*passphrase" {send "\r";exp_continue}
 7   "*again" {send "\r"}
 8 }
 9 
10 expect eof
11 exit -onexit {
12   send_user "Done!\n"}

 

posted @ 2019-04-26 10:17  JPinsz  阅读(354)  评论(0编辑  收藏  举报