0
点赞
收藏
分享

微信扫一扫

expect批量分发密钥对

vim shell.exp

#!/usr/bin/expect

set timeout 10
set hostname [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]

spawn ssh-copy-id $username@$hostname

expect {
"Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
expect "*password:"
send "$password\r"
}

"*password:" {
send "$password\r"
}
"Now try logging into the machine" {
}
}
expect eof

一个bash搞定,完犊子

在bash中,用expect -c "" 把expect语句包起来,将expect -c "" 中的双引号加上反斜杠\

#!/bin/bash

USER=root
PASSWD=1233

# yum install -y expect

for HOST in 192.168.1.{1..10}
do
echo "------------------>" $HOST "-----------------------------"
/usr/bin/expect -c "
spawn ssh-copy-id $USER@$HOST;
expect {
\"Are you sure you want to continue connecting (yes/no)?\" {
send \"yes\r\"
expect \"*password:\"
send \"$PASSWD\r\"
}

\"*password:\" {
send \"$PASSWD\r\"
}
\"Now try logging into the machine\" {
}
}
expect eof
"
done

版权声明:本博客文章与代码均为学习时整理的笔记,文章 [均为原创] 作品,转载请 [添加出处] ,您添加出处是我创作的动力!





举报

相关推荐

0 条评论