Linux expect_shell实现推送公钥
#!/usr/bin/bash>ip.txe pwd="Changeme123" rpm -q expect &>/dev/null if [ $? -ne 0 ];then yum -y install expect fi #判断公钥是否存在,不存咋创建 if [ ! -f ~/.ssh/id_rsa ];then ssh-keygen -P "" -f ~/.ssh.id_rsa fi for i in {2..235} do { ip=10.90.183.$i ping -c1 -W1 $ip &>/dev/null #可以ping通 执行 if [ $? -eq 0 ];then echo "$ip" >> ip.txt /usr/bin/expect spawn ssh -copy-id $ip expect{ "yes/no" { send "yes\r";exp_continue } "password:" { send "$pwd" } } expect eof EOF fi }& done wait echo "finish...."
#!/usr/bin/expect set ip [lindex $argv 0] set user [lindex $argv 0] set timeout 5 spawn ssh $user@$ip expect{ "yes/no" { send "yes\r";exp_continue } "password:" { send "$pwd" } } #interact expect "#" send "pwd\r" send "cd /home\r" send "exit" expect eof