#!/bin/bash cd /root cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys for i in 'cat ip.txt' do ip=$(echo "$i" | cut -f1 -d":") password=$(echo "$i" | cut -f2 -d":") expect -c " spawn scp /root/.ssh/authorized_keys /root/remote_operate.sh root@$ip:/tmp/ expect{ \"*yes/no*\"{send \"yes\r\";exp_continue} \"*password*\"{send\"$password\r\";exp_continue} \"*Password*\"{send\"$password\r\";} } " expect -c " spawn ssh root@$ip "/tmp/remote_operate.sh expect{ \"*yes/no*\"{send \"yes\r\";exp_continue} \"*password*\"{send \"$password\r\"; exp_continue} \"*Password*\"{send \"$password\r\";} } " done
batch_sshkey.sh 该脚本会调用ip.txt文件,作ssh私钥认证。bash
按下面的格式把客户端机器ip写入ip.txt文件里:ssh
ip.txt(前面是IP,后面是密码,用冒号分割。)ide
!#/bin/bash if [! -d /root/.ssh ];then mkdir /root/.ssh fi cp /tmp/authorized_keys /root/.ssh/
remote_operate.sh这个是用来把认证文件复制到客户端里的。最终实现远程登陆不用户手工输入密码。
spa